Connection con;
PreparedStatement ps,p2;
try
{
sqlquery = "...";
ps=con.prepareStatement(sqlquery);
ps.executeUpdate();
// using ps2...some similar process.......
}
catch(SQLException sqlex)
{
System.out.print(sqlex.getMessage());
}我怀疑是否是
Is it possible to find out the cause of the specific error made out by a query or update such that i can handle the situation other than just displaying the error message at output console (as here) displaying the cause ?
例如:如果,如果ps preparedStatement由于重复输入而出错,或者ps由于某些外键问题而出错,那么preparedStatement应该能够分析sql错误,特别是 using SQLException object ,这样我就可以相应地处理,而不仅仅是在输出控制台中显示错误消息。
我之所以需要这样做,是因为我希望显示通过GUI场景工作的用户所犯的错误(例如:这里是java swing),他通过分析SQLException实例以交互方式创建的任何错误条目.
In short , how to classify errors using a single SQLException instance
对不起,如果这个问题是矛盾的或愚蠢的…!
发布于 2012-07-07 11:21:28
如果我正确理解,您需要类getSQLState和getErrorCode的方法SQLException。
有关更多信息,请参阅这里。
我希望这能帮到你!
发布于 2012-07-07 11:45:22
当抛出SQLException时,可以使用来自SQLException实例的相关方法捕获错误状态、错误代码和特定消息。
getErrorCode(),getSQLState(),getMessage()。MySQL记录了各种常见的错误和问题
附录C.错误、错误代码和常见问题,您需要在java代码中使用它并相应地处理它。
正如Vincenzo所提到的,在他的回答中,您的can 处理SQL异常基于错误状态和代码。
请注意,在不同的数据库实现中,这些错误状态、代码可能并不相同。
各种甲骨文错误代码,ORA前缀,可以找到这里。
发布于 2012-07-07 11:22:35
我认为解决这个问题的最简单的方法是使用getErrorCode()获取错误代码(例如: 1045),并基于一些预定义的代码映射到您预先定义的用户可读消息(在文件或硬编码中),以返回容易理解的消息。
一个通用的解决方案是解析getMessage()返回的消息,但我并不认为DBMS抛出的异常有一个定义良好的语法,因此这样的解决方案是不可行的/不可实现的。
https://stackoverflow.com/questions/11374516
复制相似问题