【发布时间】:2016-07-21 15:13:09
【问题描述】:
我无法执行更新语句。我的更新声明似乎有一些问题。
我现在正在做的是尝试更新重复的电子邮件地址并用非重复的电子邮件替换(更新)。因此,为了更新,我的 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();
}}
}
【问题讨论】:
-
这到底是怎么回事? “一些问题”不能解释任何事情。
-
@marB 更新似乎没有执行。我在我的 sql 数据库中没有看到任何更新,而是执行了删除 sql 语句。
-
您是否调试过您的 servlet 以准确查看 UPDATE 语句的样子。然后复制粘贴到mysql,看看是否执行?
-
@RickS 我没有调试,因为我不确定调试在合理的应用程序开发中是如何工作的。但是,我确实在 mysql 数据库中尝试了 sql 语句。它工作得很好。我能够在 mysql 数据库中实现我想要的。
-
如果您不调试或在某处记录它,您如何知道您在代码中构建的 UPDATE 语句是什么样的?