I was trying to create an insert query in Java to MySQL server but the thing is it keep asking for value for column "user id" even though I've already define it as AUTO_INCREMENT. I've tried to set the value to NULL too but it said "column user id cannot be null".
From what I know in SQL you don't have to define value for auto increment type right ?
Query:
String query = "INSERT INTO
user(userId, username, password, gender, country, role)
VALUES(NULL,'"+uu+"', '"+pp+"', '"+gg+"','"+cc+"', '"+rr+"')";
Errors :
java.sql.SQLIntegrityConstraintViolationException: Column 'userId' cannot be null
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1333) at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2106) at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1243) at main.Connect.updateData(Connect.java:43) at main.Regis.actionPerformed(Regis.java:189) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Table definition :
|Column |Type |Null|key|Default|Extra|
+--------+-------------+----+---+-------+-----+
|userId |int |NO |___|NULL | |
|username|varchar(255) |NO |___|NULL | |
|password|varchar(255) |NO |___|NULL | |
|gender |varchar(255) |NO |___|NULL | |
|country |varchar(255) |NO |___|NULL | |
|role |varchar(255) |NO |___|NULL | |
Note : from the .sql
file that I get there is alter table code for table user
ALTER TABLE `user`
MODIFY `userId` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;