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