gpt4 book ai didi

java - [Java] 插入 SQL 时出错,表中自动递增 - userId

转载 作者:行者123 更新时间:2023-11-30 22:34:38 26 4
gpt4 key购买 nike

所以我正在制作一个注册页面,当我输入所有字段并单击注册提交时,将调用 enterNewUser(,,,) 并将字段 userId、用户名、密码和角色插入到用户表中。我通过在 MYSQL 工作台中运行 select * from user; 来确认这一点。

然后 enterUsername(,,,) 被调用,我得到这个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(3,'Barry','Allen')' at line 1 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(3,'Barry','Allen')' at line 1

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

这是我的数据库结构。

enter image description here user_profile user

编辑:发现问题,数据库结构不正确。

CREATE TABLE user ( userId int(3) NOT NULL AUTO_INCREMENT,
username varchar(20) DEFAULT NULL, password varchar(20) DEFAULT NULL, role varchar(20) DEFAULT NULL, PRIMARY KEY (userId),
UNIQUE KEY username (username) );

CREATE TABLE user_profile ( userId int(3) NOT NULL DEFAULT '0', firstName varchar(20) DEFAULT NULL, lastName varchar(20) DEFAULT NULL, PRIMARY KEY (userId), CONSTRAINT FK FOREIGN KEY (userId) REFERENCES user (userId) );

最佳答案

不应遵循方法 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);

关于java - [Java] 插入 SQL 时出错,表中自动递增 - userId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32924741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com