JTable からデータベースにデータを書き込みたいのですが、今のところうまくいきません。常に mysql エラーが発生します。誰かが私に何が悪いのか教えてもらえますか?
public class Fussball extends JFrame {
private JButton save = new JButton();
private JTable jTable1 = new JTable(9, 4);
private DefaultTableModel jTable1Model = (DefaultTableModel) jTable1.getModel();
private JScrollPane jTable1ScrollPane = new JScrollPane(jTable1);
public Fussball(String title) {
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 563;
int frameHeight = 482;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
save.setBounds(200, 304, 201, 65);
save.setText("speichern");
save.setMargin(new Insets(2, 2, 2, 2));
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
save_ActionPerformed(evt);
}
});
cp.add(save);
jTable1ScrollPane.setBounds(64, 48, 465, 225);
jTable1.getColumnModel().getColumn(0).setHeaderValue("Spiel ID");
jTable1.getColumnModel().getColumn(1).setHeaderValue("Verein 1");
jTable1.getColumnModel().getColumn(2).setHeaderValue("Verein 2");
jTable1.getColumnModel().getColumn(3).setHeaderValue("Ergebnis");
cp.add(jTable1ScrollPane);
// Ende Komponenten
setResizable(false);
setVisible(true);
}
public void save_ActionPerformed(ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection dbConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/fussball", "root", "");
Statement statement = dbConn.createStatement();
String insertQuery = "INSERT INTO Spiel VALUES (?,?,?,?)";
PreparedStatement ps = dbConn.prepareStatement(insertQuery);
ps.setString(1, "idSpiel");
ps.setString(2, "Vereinone");
ps.setString(3, "Vereintwo");
ps.setString(4, "Ergebnis");
ps.executeUpdate();
//Vector vector = new Vector();
statement.close();
dbConn.close();
} catch (InstantiationException e) {
System.err.println("Error in Instantiation!");
} catch (ClassNotFoundException e) {
System.err.println("Class not found!");
} catch (IllegalAccessException e) {
System.err.println("Access denied!");
} catch (SQLException e) {
System.err.println("SQL Error!");
}
}
public static void main(String[] args) {
new Fussball("fussball");
}
}