簡體   English   中英

[Java]插入SQL時發生錯誤,表格中的數據會自動增加-userId

[英][Java]Error when Inserting SQL, auto increment in the table - userId

因此,我正在制作一個注冊頁面,當我輸入所有字段並單擊注冊進行提交時,將調用enterNewUser(,,,),並將字段userId,用戶名,密碼和角色插入表User中。 我通過運行用戶選擇*來確認這一點 進入MYSQL工作台。

然后調用enterUsername(,,,),我得到這個錯誤:

您的SQL語法有誤; 在com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException第1行中,檢查與您的MySQL服務器版本相對應的手冊,以在'(3,'Barry','Allen')'附近使用正確的語法。您的SQL語法; 檢查與您的MySQL服務器版本相對應的手冊以獲取在第1行的'(3,'Barry','Allen')'附近使用的正確語法

public static int enterNewUser(String username,String password, String role){
    //int userId = -1;
    int ID = 0;
    //int ID=-1;
    try{
        if(checkUserNameAvailable(username)==true){
            Class.forName("com.mysql.jdbc.Driver");
            cn = DriverManager.getConnection("jdbc:mysql://localhost/log", "root", "root");

            String q0 = "Select userId from user ORDER BY userId DESC LIMIT 1"; //get ID of last
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(q0);

            if(rs.next()){

                ID = rs.getInt("userId");
                ID++;
            }
            else
                ID=1; // Empty Table, so start with ID 1

            rs.close();
            st.close();

            String q1="insert into user values(?,?,?)";

            PreparedStatement ps = cn.prepareStatement(q1);
            //ps.setInt(1,ID);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,role); 
            ps.executeUpdate();
            ps.close();

        }
    }catch(Exception e){
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
    DB_close();
    //if(userId!=-1)
    //  return userId;
    return -1;      
}
public static boolean enterUsername(int userId, String firstname, String lastname){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        cn = DriverManager.getConnection("jdbc:mysql://localhost/log", "root", "root");

        //String q1="INSERT INTO user_profile values(?,?,?)";
        String q1 = "INSERT into user_profile (userId, firstname, lastname) VALUES (?,?,?)";

        PreparedStatement ps = cn.prepareStatement(q1);
        ps.setInt(1, userId);
        ps.setString (1, firstname);
        ps.setString (2, lastname);
        ps.executeUpdate();
        ps.close();
        return true;
    }catch(Exception e){
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
    DB_close();
    return false;
}

這是我的數據庫結構。

在此處輸入圖片說明 用戶資料 用戶

編輯:發現問題,數據庫結構不正確。

創建表useruserId int(3)NOT NULL AUTO_INCREMENT,
username varchar(20)缺省NULL, password varchar(20)缺省NULL, role varchar(20)缺省NULL,PRIMARY KEY( userId ),
UNIQUE KEY usernameusername ));

創建表user_profileuserId int(3)NOT NULL DEFAULT'0', firstName varchar(20)DEFAULT NULL, lastName varchar(20)DEFAULT NULL,PRIMARY KEY( userId ),CONSTRAINT FK FOREIGN KEY( userId )參考useruserId ) );

采用

String q1 = "INSERT into user_profile (firstname, lastname) VALUES (?,?)";

因為您的第一個字段是auto increment ..所以它會在插入值時自動遞增值。

我建議這樣

刪除當前表並創建一個新表

   id-->int(30) (AUTO INCREMENT) NOTNULL  //Dont need to take care of this field

   USER_ID-->int(30) NOT NULL  //you should create your own ID and increment it before adding a new person

   username-->varchar(100)

   password-->varchar(100)

   role-->varchar(100)

通常,調用userId就像您的代碼一樣,

String q0 = "Select userId from user ORDER BY USER_ID DESC LIMIT 1"; //get ID of last
        Statement st = cn.createStatement();
        ResultSet rs = st.executeQuery(q0);

        if(rs.next()){

            ID = rs.getInt("USER_ID ");
            ID++;
        }

不應遵循方法enterUsername中的部分

ps.setInt(1, userId);
ps.setString (1, firstname);
ps.setString (2, lastname);

像這樣

 ps.setInt(1, userId);
 ps.setString (2, firstname);
 ps.setString (3, lastname);

我看不到您發布的錯誤消息的原因。

但是我看到其他一些看起來像問題的東西:

 ps.setInt(1, userId); ps.setString (1, firstname); ps.setString (2, lastname); 

索引是錯誤的:而不是1、1、2,而應該是1、2、3。(坦率地說,我看不出代碼如何按發布方式工作。)

順便說一句,在另一種方法中這也看起來是錯誤的:

 insert into user values(?,?,?) 

由於表有3列以上,因此您需要指定它們的名稱,就像在enterUsername所做的enterUsername

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM