我无法执行更新语句。我的更新声明似乎有些问题。
我现在正在做的是,我尝试更新重复的电子邮件地址,并用非重复的电子邮件替换(更新)。因此,为了更新,我的SQL语句将根据输入检查我的数据库,如果((firstname && lastname && dateofbirth) || (phoneNo && address)
匹配用户输入,它将更新数据库;即:使用非重复的电子邮件更新重复的电子邮件地址,然后在执行更新后删除所有重复的电子邮件。
然而,我无法弄清楚错误是什么。这是我的更新声明:
try {
System.out.println("it came here where filepart!=null");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
// connects to the database
conn = getConnection();
// constructs SQL statement
stmt = conn.createStatement();
//sqll should be update
String sql1 ="UPDATE registration SET emailAddress = ? where ((firstName = ? && lastName= ? && dateOfBirth= ?) || (phoneNo= ? && address= ?))" ;
//Using a PreparedStatement to save the file
PreparedStatement statement1 = conn.prepareStatement(sql1);
System.out.println(firstName+"firstname");
statement1.setString(1, firstName);
statement1.setString(2, lastName);
statement1.setString(3, dateOfBirth);
statement1.setString(4, phoneNo);
statement1.setString(5, address);
statement1.executeUpdate();
statement1.close();
String sql2="delete registration from registration inner join (select min(userId) as lastId, emailAddress from registration where emailAddress in ( select emailAddress from registration group by emailAddress having count(*) > 1) group by emailAddress) duplic on duplic.emailAddress = registration.emailAddress where registration.userId > duplic.lastId";
stmt.executeUpdate(sql2);
//sends the statement to the database server
// if (row > 0) {
// getServletContext().getRequestDispatcher("/Message.jsp").include(request, response);
// message = "You have successfully registered.";
//}
} catch (SQLException ex) {
// message = "You have failed to registered. Please try again.";
// ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
// ex.printStackTrace();
//silent
//message="You have failed to log in";
getServletContext().getRequestDispatcher("/FailedMsg.jsp").include(request, response);
}
}
rs.close();
ps.close();
conn.close();
}}
}
答案 0 :(得分:0)
问题可能出在where子句。尝试将Trim()
,ToUpper()
或ToLower()
函数应用于字符串数据列和输入值。首先尝试匹配firstname
,lastname
和dateofbirth
。如果有效,请尝试匹配电话和地址数据。在匹配之前不要忘记清理/标准化数据。
答案 1 :(得分:0)
首先查看你的sql字符串:
String sql1 ="UPDATE registration SET emailAddress = ? where ((firstName = ? && lastName= ? && dateOfBirth= ?) || (phoneNo= ? && address= ?))" ;
在上面的字符串中,您有6个问号(占位符)。但是你只设置了其中的五个而且确实是错误的。您正在做的是设置emailAddress
的名字,firstName
的姓氏,依此类推。第一个占位符(?)用于emailAddress
,因此您必须执行以下操作:
statement1.setString(1, emailAddress);
statement1.setString(2, firstName);
statement1.setString(3, lastName);
statement1.setString(4, dateOfBirth);
statement1.setString(5, phoneNo);
statement1.setString(6, address);
这可能是您无法正确更新的原因吗?你可能也会遇到一些例外。你应该看看RAD控制台。