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