V
V
Vladimir Gunaev2019-07-24 13:52:49
Java
Vladimir Gunaev, 2019-07-24 13:52:49

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);
       }
    }

Errors
Based on Java.SQL.SQLException translation: field 'pass' has no default value
What's wrong, where to look?
java.sql.SQLException: Field 'pass' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978 )
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java: 2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2491)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2449)
at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:845)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:745)
at javaapp.DB.jButton1ActionPerformed(DB.java:99)
at javaapp. DB.access$000(DB.java:19)
at javaapp.DB$1.actionPerformed(DB.java:65)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased( BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt. Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component .java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt. EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController. doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue .java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread java.awt.EventDispatchThread.pumpEventsForFilter
(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpDispEvents(EventDispatchThread.pumpEvents java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
ASSEMBLY COMPLETED SUCCESSFULLY (total time: 4 seconds)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-07-24
@sergey-gornostaev

When inserting into the USER table, it is mandatory to pass a value for the pass field.

V
Vladimir Gunaev, 2019-07-24
@gunaevart

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 question

Ask a Question

731 491 924 answers to any question