File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes How to get error codes using java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "How to get error codes using java program " Watch "How to get error codes using java program " New topic
Author

How to get error codes using java program

kasi pandian
Greenhorn

Joined: Feb 01, 2008
Posts: 12
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

You say you are using Spring but that's not enough information. How are you interacting with the database? JdbcTemplate, iBatis, Hibernate, what?


GenRocket - Experts at Building Test Data
kasi pandian
Greenhorn

Joined: Feb 01, 2008
Posts: 12
I'm using JDBCTemplate
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Feb 01, 2008
Posts: 12
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);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get error codes using java program