繁体   English   中英

如何在 Java 中为自动增量表创建查询?

[英]How do I create a query in Java for auto increment table?

你好,所以我试图在 java 中创建一个插入查询到我的 sql 服务器,但问题是即使我已经定义了 AUTO_INCREMENT,它仍然会要求字段用户 ID 的值,我试图将值设置为 Z26C3E2236B4D078它说“列用户ID不能为空”

嗯,据我所知,sql 你不必为自动增量类型定义值吗?

询问:

String query = "INSERT INTO 
user(userId,username,password,gender,country,role) 
VALUES(NULL,'"+uu+"', '"+pp+"', '"+gg+"','"+cc+"', '"+rr+"')";

错误:

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)

表定义:

|Field   |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   |     |

注意:从我得到的 .sql 文件中,有表用户的更改表代码

ALTER TABLE `user`
MODIFY `userId` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;

除非它有一个键(即索引),否则你不能创建一个列 AUTO_INCREMENT。 最好是 PRIMARY KEY 或 UNIQUE KEY。

但是我在您的描述表示例中看到,该列不是键:

|Field   |Type         |Null|key|Default|Extra|
|--------|-------------|----|---|-------|-----|
|userId  |int          |NO  |___|NULL   |     |

                             ^^^ ideally this should say "PRI"

所以你的 ALTER TABLE 使列 AUTO_INCREMENT 可能失败。 您可以确认这一点:

SHOW CREATE TABLE `user`\G

您是否看到userId列旁边的AUTO_INCREMENT选项? 我不认为你会。

您可以再次尝试将该列设为主键并将其设为 AUTO_INCREMENT:

ALTER TABLE `user`
 ADD PRIMARY KEY (`userId`),
 MODIFY `userId` INT AUTO_INCREMENT;

不要打扰INT(255) integer 的长度参数是一个常见的混淆来源。 它几乎没有任何目的或效果,并且在 MySQL 8.0 中已弃用

不要费心制作列NOT NULL 当您添加 PRIMARY KEY 约束时,这将自动发生。

不要费心设置AUTO_INCREMENT=8 下一个 AI 值将自动设置为该列中的最大值加一。 它永远不能小于该列中的最大值。

试试这个代码,如果你已经定义了 userId 是一个 auto_increament 就必须工作。

String query = "INSERT INTO user(username,password,gender,country,role) VALUES('"+uu+"', '"+pp+"', '"+gg+"','"+cc+"', '" +rr+"')";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM