请问如何得到Oracle SQLException的errorCode, 用SQLException.getErrorcode() 总是返回0例如有如下Oracle Exception:
org.jboss.util.NestedSQLException: Could not create connection; - nested throwable: (java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
pacwas1:1521:testdb
); - nested throwable: (org.jboss.resource.JBossResourceException: Could not create connection; - nested throwable: (java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
xxx:1521:xxx
))当Catch到这个SQLException并且使用getErrorcode() 总是返回0,但我期待的是ORA-12505,请问有好的方法吗?

解决方案 »

  1.   

    try{...}
    catch(SQLException e) {System.out.println(e.getMessage);}
      

  2.   

    我的问题是,各位有没有遇到同样的SQLException.getErrorcode()无法返回正确errorcode的情况,如何解决的?
      

  3.   

    getErrorcode() 返回的是int正常情况下直接getMessage记录错误原因或者返回就行了如果非要getErrorcode()那就自己包装一个SQLException类,继承原来的,重载这个方法,在getErrorcode返回0的情况下,继续判断getMessage是否有ORA-xxxx这样的错误信息,如果有,就返回。
      

  4.   

     int errorCode = ((SQLException) ex.getCause()).getErrorCode();
      

  5.   

    用SQLException.getCause().getMessage()就可以得到
    ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
    要只得到ORA-12505的话,就再自己截字符串的前9位吧