如何捕获结果集循环内的约束违规?
我正在开发一个 servlet,它将生成唯一的代码并在 mySQL 数据库中更新该代码。 现在,我想捕获 mySQL 表中已存在唯一代码时引发的任何异常,并生成新代码并尝试更新数据库。问题是我想在 for 循环本身中执行此操作。代码如下:
try
{
connection = datasource.getConnection();
SQLUpdate = "INSERT INTO Voucher_dump VALUES( '"+unique_code+"','08-10-2011 04:48:48','0')";
PreparedStatement ps1 = connection.prepareStatement(SQLUpdate);
ps1.executeUpdate();
ResultSet r = ps1.getResultSet(); // this is where I'm checking if it's a duplicate
if(r==null)
out.println("This is a duplicate");
else out.println("Updated");
trial12= "08-10-2011 04:48:480.03999855056924717a";
SQLUpdate = "INSERT INTO Voucher_dump VALUES( '"+trial12+"','08-10-2011 04:48:48','0')";
ps1 = connection.prepareStatement(SQLUpdate);
ps1.executeUpdate();
r = ps1.getResultSet();
if(r==null)
out.println("This is a duplicate");
else out.println("Updated");
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
我不想等到整个循环结束才捕获SQLException(我已经在mySQL中将这个键定义为primary)。此时,结果作为重复条目返回,我想重新生成此密钥并再次尝试更新。我的输出页面上此特定代码的输出为空白(所有其他参数均正确显示)。既不显示“这是重复项”,也不显示“已更新”。也许,ResultSet 不是最好的方法。你们能给我一些关于什么是最好的前进方向的建议吗?
I was working on a servlet that will generate a unique code and update that in a mySQL database.
Now, in that, I want to catch any exception thrown in case that unique code already exists in the mySQL table and generate a new code and try updating the database. The problem is I want to do this WITHIN the for loop itself. The code is as follows:
try
{
connection = datasource.getConnection();
SQLUpdate = "INSERT INTO Voucher_dump VALUES( '"+unique_code+"','08-10-2011 04:48:48','0')";
PreparedStatement ps1 = connection.prepareStatement(SQLUpdate);
ps1.executeUpdate();
ResultSet r = ps1.getResultSet(); // this is where I'm checking if it's a duplicate
if(r==null)
out.println("This is a duplicate");
else out.println("Updated");
trial12= "08-10-2011 04:48:480.03999855056924717a";
SQLUpdate = "INSERT INTO Voucher_dump VALUES( '"+trial12+"','08-10-2011 04:48:48','0')";
ps1 = connection.prepareStatement(SQLUpdate);
ps1.executeUpdate();
r = ps1.getResultSet();
if(r==null)
out.println("This is a duplicate");
else out.println("Updated");
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
I don't want to wait till the end of the entire loop to catch the SQLException (I have already defined this key in mySQL as primary). The moment, the result comes back as a duplicate entry, I want to re-generate this key and attempt the update again.My output for this particular code is coming blank on my output page (all other parameters are showing correctly). Neither is "This is a duplicate" displayed nor is "Updated". Maybe, ResultSet is not the best way to do it. Could you guys give me some advice on what would be the best way forward ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些建议(排名不分先后):
由于 #6,我不知道
out
是什么,但我怀疑您没有看到任何内容的原因是您在第一个语句中插入了重复的值,这将导致SQLException 来自该行,而不是您期望的getResultSet()
处。由于错误已写入 stdout,因此它会显示在服务器日志中的某个位置,但不会向out
写入任何内容。我不确定为什么您认为getResultSet()
将返回 null 或非 null,具体取决于是否存在约束违规。查看 javadoc该方法。更新: 7. 正如 BalusC 指出的,永远、永远将字符串直接连接到 JDBC 语句中。使用PreparedStatment的占位符和
set*
方法。有关 SQL 注入的信息,请参阅 Wikipedia 和 XKCD.Some advice in no particular order:
Due to #6, I don't know what
out
is, but I suspect the reason you're not seeing anything is that you're inserting a duplicate value with the first statement, which will cause a SQLException from that line, not atgetResultSet()
, where you seem to expect it. Since the error is written to stdout, it'll show up in your server logs somewhere, but nothing will be written toout
. I'm not sure why you thinkgetResultSet()
will return null or not null depending on whether there was a constraint violation. Take a look at the javadoc for that method.Update: 7. As BalusC points out, never, ever concatenate a string directly into a JDBC Statment. Use PreparedStatment's placeholders and
set*
methods. For info on SQL injection, see Wikipedia and XKCD.这段代码怎么样?
How about this code?