gpt4 book ai didi

java - PrepareStatement 卡在 executeUpdate 函数上

转载 作者:行者123 更新时间:2023-11-29 02:32:22 26 4
gpt4 key购买 nike

我的数据库添加功能出现问题。下面的代码卡在 ExecuteUpdate 语句上,它没有抛出异常,所以我不知道哪里出了问题。

public static boolean addUser(User userToAdd) throws Exception {
boolean isAdded = false;

if (checkConnection()) {
try {
if (isUnique(userToAdd.getIdCardNumber())) {
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO users SET "
+ "idCardNr = ?,nationality = ?,name = ?,"
+ "address = ?,photo = ?,status = ?,gender = ?,"
+ "nationalNr = ?,birthDate = ?,birthPlace = ?,"
+ "created = ?, country = ?");

pstmt.setString(1, userToAdd.getIdCardNumber());
pstmt.setString(2, userToAdd.getNationality());
pstmt.setString(3, userToAdd.getFullName());
pstmt.setString(4, userToAdd.getAddress());
pstmt.setString(5, userToAdd.getPhotoPath());
pstmt.setString(6, userToAdd.getStatus());
pstmt.setString(7, userToAdd.getGender());
pstmt.setString(8, userToAdd.getRegisterNumber());
pstmt.setString(9, userToAdd.getBirthday());
pstmt.setString(10, userToAdd.getBirthPlace());
java.sql.Timestamp current = new java.sql.Timestamp(System.currentTimeMillis());
userToAdd.setCreated(current);
pstmt.setTimestamp(11, userToAdd.getCreated());
pstmt.setString(12, userToAdd.getCountry());


int rowsAffected;
try {
rowsAffected = pstmt.executeUpdate();
} catch (SQLException e) {
throw new Exception("Exception while executing update: " + e.getMessage());
}


if (rowsAffected != 0) {
isAdded = true;
pstmt.close();
}

} else {
System.out.println("User already in database");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return isAdded;
}

谁能帮我调试一下?

最佳答案

您的 SQL 格式错误,您正在尝试使用 UPDATE 的语法进行 INSERT。

这是标准 INSERT 的样子

INSERT INTO users (idCardNr,nationality,name,address,photo,status,gender,nationalNr,birthDate,birthPlace,created,country) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)

在将 SQL 放入代码之前手动测试它始终是个好主意。

关于java - PrepareStatement 卡在 executeUpdate 函数上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11187413/

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