Answer the question
In order to leave comments, you need to log in
Error while adding record to mysql java jdbc db?
Hello everyone, I'm a beginner studying java, I encountered such a problem, namely, when adding a record to mysql, an error occurs.
The code itself
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String name = jTextField1.getText();
Statement statement = con.createStatement();
String query = "INSERT INTO USER (login) VALUES ('" + name + "')";
statement.execute(query);
statement.close();
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
}
Answer the question
In order to leave comments, you need to log in
When inserting into the USER table, it is mandatory to pass a value for the pass field.
The solution to the question, the first thing you need to do is to pass the value for the pass field, as written above.
The second popped up Error java.sql.SQLException: Column count doesn't match value count at row 1
turned out to be the fact that you had to add quotes, something like this.
String query = "INSERT INTO `user` (login, pass) VALUES ('" + name + "','" + pass+ "')";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question