Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get error codes using java program

 
kasi pandian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,

My requirement is to get the SQL error code through my program whenever the exception thrown. I've to show this error code in JSP. Now I'm getting a error codes with messages. I'm using spring frame work. So If there any database related exceptions I'm getting DataAccessException. I don't know how to get the error code from DataAccessException to . In SQLException we have a method getErrorCode(). However I'm not getting any SQLException, So I cannot use this. So please help me to get the error code from DataAccessException. Any help would be appreciated.

Thanks
Kasi.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say you are using Spring but that's not enough information. How are you interacting with the database? JdbcTemplate, iBatis, Hibernate, what?
 
kasi pandian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using JDBCTemplate
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kasi pandian:
I'm using JDBCTemplate


Ok, so give me a code example. Generally when using JdbcTemplate you end up throwing SQLException's. At that point you catch it and then do something with it. Possible re-throw a more specific error message once you know the error code.
 
kasi pandian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg
Thanks for your response. I'm throwing the exception from DAO layer and catching the exception in Action class. I've put the SQLException also in my catch block. However I'm getting only DataAccessException. So I don't how to convert this as a error code. Here I put my DAO layer code.Please help me how to get the error code from this? VendorPaymentException is my customised exception.In action class I'm just printing the exception. I'm getting string messages like
"DataAccessException occured in InvoiceDao filterList() rg.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.ibm.db2.jcc.a.DisconnectException: java.net.ConnectException : Error opening socket to server acftest/10.1.5.194 on port 446 with message : Connection timed out: connect DB2ConnectionCorrelator: null
"
I would like to get the error code from this exception. please help me.

Sample code:

public List getInvoiceDetails(String newinvcId) throws VendorPaymentException,SQLException {
String selectSql = "Select INVC_ID_NAM, PO_ID_NAM, BRAND_GID, VM_IP_STTS_GID from " + schemaName + "vm_ip_invc " +
" where INVC_ID_NAM=?";
List invoiceDetailsList = null;
try {
invoiceDetailsList = getCommonJdbcTemplate().queryForList(selectSql, new Object[]{newinvcId});
return invoiceDetailsList;
} catch (SQLException e) {
throw new SQLException("SQLException occured in InvoiceDao getInvoiceDetails()" + e);
} catch (DataAccessException e) {
throw new VendorPaymentException("DataAccessException occured in InvoiceDao getInvoiceDetails()" + e);
} catch (Exception e) {
throw new VendorPaymentException("Exception occured in InvoiceDao getInvoiceDetails()" + e);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic