Скопируйте и вставьте этот код в ваш проект, это 100% работа
метод вставки данных:
public void insert()
{
try {
// connection string
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager
.getConnection("jdbc:mysql://localhost:3306/Project?user=root&password=root");
Statement st = con.createStatement();
st.executeUpdate("insert into Register VALUES('"
+ tf1.getText() + "','" + tf2.getText() + "','"
+ tf3.getText() + "','" + cb1.getSelectedItem()
+ "','" + tf4.getText() + "','" + tf5.getText()
+ "','" + tf6.getText() + "','" + tf7.getText()
+ "','" + tf8.getText() + "','" + tf9.getText()
+ "'," + "'" + tf10.getText() + "','"
+ tf11.getText() + "','" + tf12.getText() + "','"
+ tf13.getText() + "','" + tf14.getText() + "',"
+ "'" + tf15.getText() + "','" +tf16.getText()
+ "','" + tf17.getText() + "','" + tf18.getText()
+ "'," + "'" + tf19.getText() + "','"
+ p1.getText() + "','" + p2.getText() + "')");
JOptionPane.showConfirmDialog(null, "Your Data Has been Inserted",
"Result", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE);
st.close();
con.close();
}
catch (Exception e1)
{
System.out.println("Exception:" + e1);
}
}
Затем вызовите метод с помощью кнопки:
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
insert();
}
});