0
我真的需要你的幫助,我試圖解決這個問題超過一個星期,但還沒有找到解決方案。數據庫插入後JTable不刷新
我的目標:我想創建一個表,它可以讀取數據庫中的數據。我也可以通過將數據插入到我的程序中來將數據添加到數據庫中。
我的問題:插入數據後,我希望數據庫刷新,所以這也說明我的新記錄集。但不管我嘗試了什麼,它都不起作用。
我的代碼:這裏是我的大型機級別:
public class Gui_Test extends JFrame {
JButton addMovieButton;
JFrame frame = new JFrame("Movie Database");
JPanel panel;
JMenuBar menubar;
JMenu fileMenu;
JLabel label;
JTable table = new JTable();
MovieTableModel mtm;
public static void main(String[] args) {
Gui_Test test = new Gui_Test();
test.run();
}
public void run() {
// Gui ///
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
panel = new JPanel();
panel.setVisible(true);
panel.setBackground(Color.black);
// Add - Movie Button ///
addMovieButton = new JButton("Add Movie");
addMovieButton.addActionListener(new addMovieButtonListener());
panel.add(addMovieButton);
// Table select ///
mtm = new MovieTableModel();
table.setModel(mtm);
JScrollPane pane = new JScrollPane(table);
frame.getContentPane().add(pane);
frame.getContentPane().add(BorderLayout.WEST, panel);
}
class addMovieButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
AddMoviePanel addmoviepanel = new AddMoviePanel();
addmoviepanel.moviepanel(mtm);
}
}}
這裏是我的TableModel:
public class MovieTableModel extends AbstractTableModel {
Connection con = null;
Vector columnNames = new Vector();
Vector data = new Vector();
ResultSet rs;
ResultSetMetaData meta;
public MovieTableModel() {
showResult();
}
void showResult() {
Connection con;
try {
con = DriverManager.getConnection(
"jdbc:hsqldb:file:C:/Users/...", "sa",
"");
java.sql.Statement stmt = con.createStatement();
String query = "SELECT * FROM movies ORDER BY id DESC";
ResultSet rs = stmt.executeQuery(query);
meta = rs.getMetaData();
int columns = meta.getColumnCount();
// get column names
for (int i = 1; i <= columns; i++) {
columnNames.addElement(meta.getColumnName(i));
}
// get row data
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
if (con != null)
try {
rs.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
@Override
public String getColumnName(int column) {
return columnNames.get(column).toString();
}
@Override
public int getColumnCount() {
try {
return meta.getColumnCount();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public Class getColumnClass(int column) {
// TODO Auto-generated method stub
return getValueAt(0, column).getClass();
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getValueAt(int row, int column) {
return ((Vector) data.get(row)).get(column);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
void addRow(final String value1, final String value2, final String value3,
final String value4, final String value5, final String value6,
final String value7) {
try {
Connection con = DriverManager.getConnection(
"jdbc:hsqldb:file:C:/Users/Jonas/workspace/movieDB", "sa",
"");
try {
final java.sql.Statement state = con.createStatement();
try {
state.addBatch("INSERT INTO movies VALUES (DEFAULT, '"
+ value1 + "', '" + value2 + "'," + value3 + ", '"
+ value4 + "', " + value5 + ", '" + value6 + "', '"
+ value7 + "')");
state.executeBatch();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} finally {
if (con != null)
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}}
這裏是我的addMovieFrame,打開添加新的電影:
public class AddMoviePanel {
MovieTableModel mtm;
JPanel addMoviePanel;
JFrame addMovieFrame;
JTextField value1Input;
JTextField value2Input;
// ... value3 - value7
Connection con = null;
public void moviepanel(MovieTableModel mtm) {
this.mtm = mtm;
addMovieFrame = new JFrame("Add Movie");
addMovieFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMovieFrame.setVisible(true);
addMovieFrame.setSize(550, 300);
addMoviePanel = new JPanel();
GroupLayout layout = new GroupLayout(addMoviePanel);
addMoviePanel.setLayout(layout);
JLabel label1 = new JLabel("label1:");
JLabel label2 = new JLabel("label2");
// ...JLabel 3-7 same as Label 1&2
addMoviePanel.add(label1);
addMoviePanel.add(label2);
// ...add Label 3-7
value1Input = new JTextField();
value2Input = new JTextField();
// ... value3- value7 Input
addMoviePanel.add(value1Input);
addMoviePanel.add(value2Input);
// ... add value3Input - value7Input
JButton ok = new JButton("Ok");
ok.addActionListener(new okActionListener());
addMovieFrame.add(ok);
addMovieFrame.getContentPane().add(addMoviePanel);
// here was just Layout Stuff //
}
class okActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
mtm.addRow(value1Input.getText(), value2Input.getText()
// ... value3Input.getText() - value7Input.getText()
);
mtm.fireTableDataChanged();
addMovieFrame.dispose();
}
}}
我已經嘗試過幾種類型和位置的fir eXXX-Methods ...
也許有人有一個想法,如何刷新我的jtable,當擊中我的addMovie-Frame中的OK按鈕? :)