简体   繁体   English

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

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

Hello so i was trying to create an insert query in java to my sql server but the thing is it keep asking for value for field user id even tho i've already define AUTO_INCREMENT, i've tried to set the value to NULL too but it said "coloumn user id cannot be null"你好,所以我试图在 java 中创建一个插入查询到我的 sql 服务器,但问题是即使我已经定义了 AUTO_INCREMENT,它仍然会要求字段用户 ID 的值,我试图将值设置为 Z26C3E2236B4D078它说“列用户ID不能为空”

uh from what i know in sql you dont have to define value for auto increment type right?嗯,据我所知,sql 你不必为自动增量类型定义值吗?

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:表定义:

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

Note: from the.sql file that i get there is alter table code for table user注意:从我得到的 .sql 文件中,有表用户的更改表代码

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

You can't make a column AUTO_INCREMENT unless it's has a key (that is, index) on it.除非它有一个键(即索引),否则你不能创建一个列 AUTO_INCREMENT。 Best if it's a PRIMARY KEY or UNIQUE KEY.最好是 PRIMARY KEY 或 UNIQUE KEY。

But I see in your example of describe table, the column is not a key:但是我在您的描述表示例中看到,该列不是键:

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

                             ^^^ ideally this should say "PRI"

So your ALTER TABLE to make the column AUTO_INCREMENT probably failed.所以你的 ALTER TABLE 使列 AUTO_INCREMENT 可能失败。 You can confirm this:您可以确认这一点:

SHOW CREATE TABLE `user`\G

Do you see the AUTO_INCREMENT option next to the userId column?您是否看到userId列旁边的AUTO_INCREMENT选项? I don't think you will.我不认为你会。

You can try again to make the column a primary key and make it AUTO_INCREMENT:您可以再次尝试将该列设为主键并将其设为 AUTO_INCREMENT:

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

Don't bother with INT(255) .不要打扰INT(255) The length argument for an integer is a common source of confusion. integer 的长度参数是一个常见的混淆来源。 It has almost no purpose or effect , and it's deprecated in MySQL 8.0 . 它几乎没有任何目的或效果,并且在 MySQL 8.0 中已弃用

Don't bother making the column NOT NULL .不要费心制作列NOT NULL That will happen automatically as you add the PRIMARY KEY constraint.当您添加 PRIMARY KEY 约束时,这将自动发生。

Don't bother setting the AUTO_INCREMENT=8 .不要费心设置AUTO_INCREMENT=8 The next AI value will automatically be set to the highest value in that column, plus one.下一个 AI 值将自动设置为该列中的最大值加一。 It can never be less than the greatest value in that column.它永远不能小于该列中的最大值。

Try this code, must work if you already defined userId is an auto_increament.试试这个代码,如果你已经定义了 userId 是一个 auto_increament 就必须工作。

String query = "INSERT INTO user(username,password,gender,country,role) VALUES('"+uu+"', '"+pp+"', '"+gg+"','"+cc+"', '"+rr+"')"; 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