Common Exception in Java and their workarounds

Hi All,

I am going to list few Exceptions which I have faced during my developments and their workarounds. Hope you will find this post useful. KEEP CHECKING THIS POST.

1. java.sql.SQLException:Stored procedure call failed

If this error comes, just check in the stored procedure as it simply indicates some internal call to some other procedure is getting failed in Stored procedure which you are invoking from Java class.

2. PLS-00306: wrong number or types of arguments in call to ‘PROC_NAME’

This error indicates that mismatch in stored procedure arguments with the one which we are passing in java class method. Quickest workaround is to check number of input and output parameter in the procedure and set the same in Java class method.

3. java.sql.SQLException: Cursor is closed.
at oracle.jdbc.driver.T4CResultSetAccessor.getCursor(T4CResultSetAccessor.java:332)

This exception raised if your procedure has returned a closed cursor, the quickest workaround is to put a null check for ResultSet object. e.g
ResultSet rs=(ResultSet)callableStmt.getObject(2);
if(rs!=null){ // put this check before invoking the resultset.next;
while(rs.next){
}
}

4. java.sql.SQLException: ORA-06550: line 1, column 7:  PLS-00201: identifier ‘DB_PKG.MY_PROC_NAME’ must be declared ORA-06550: line 1, column 7:

This error indicates that the DB object is missing or having some compilation problems. Just check in the corresponding DB package for the same.

5.java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:112)

This is perhaps the most easiest error to catch in JDBC, This simply states that column name which are trying to read from result set is missing in the output of the query or missing in the Cursor output(Stored procedure)
The workaround is to cross check all the column name in the query or cursor.

6. java.sql.SQLException: Inconsistent java and sql object types
This error comes when we use SQL collection in JDBC, As SQL collection is a kind of Associative array, so while creating a Array Descriptor we need to make sure the data should match the length of SQL Collection.
if some mismatch left then this is exception which will appear in the logs/console.

7.Exception in thread “main” java.lang.Error: Unresolved compilation problem:
If you face this error in your stand alone java program check if there are any syntax errors or any reference errors. Fix those and run the program again.

8.java.sql.SQLException: Missing IN or OUT parameter at index:: <<any_number>>
This error simply indicates that some of the IN or OUT parameter has been missed while invoking the procedure or prepared statement.

9. java.sql.SQLException: ORA-00900: invalid SQL statement
This error usually indicates a typo mistake while calling a stored procedure. as we always mentioned the keyword “call” in the procedure string in prepareCall method of CallableStatement. Ig you miss to specify the same you will face this exception.

Wrong
cstmt = (OracleCallableStatement) getConnection1().prepareCall(“{DB_PKG.PROC_NAME(?,?,?,?,?,?)}”);

Right
cstmt = (OracleCallableStatement) getConnection1().prepareCall(“{call DB_PKG.PROC_NAME(?,?,?,?,?,?)}”);

10.java.sql.SQLException: ORA-01033: ORACLE initialization or shutdown in progress
The “ORA-01033: ORACLE initialization or shutdown in progress.” error can also happen when Oracle is attempting startup or shutdown and is “hanging” on a resource such as a failed disk, writing to redo, etc.

11. java.sql.SQLException: ORA-06576: not a valid function or procedure name
This error occurred if you some type mistake in the procedure or function name while calling from java. Simply cross check the names and run again.

12.java.sql.SQLException:ORA-01034: ORACLE not available
Check if database is running. This happens when DB goes down or shutdown.


14. java.sql.SQLRecoverableException: No more data to read from socket

This is communication break with the weblogic server and Database.

15. Caused by: com.myapp.exception.SysException: java.sql.SQLException: Invalid argument(s) in call.
Make sure you are not sending any null values to any Object(like SQL Aarry)set any input parameter in DB Stored procedure.
If in case your object remains null before sending to the CallableStatament, just use the setNull method to set such objects null.
e.g
callableStatement.setNull(1,OracleTypes.ARRAY,”NAME_OF_TABLE_TYPE”);

16  java.lang.ClassCastException: oracle.sql.StructDescriptor cannot be cast to oracle.sql.ArrayDescriptor
at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:191)
at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:165)

To Fix this problem check the Collection Name given. It should be Table Type name not the Object Name.
e.g Lets say you have created a Object MY_SCHEMA_OBJ
and you have Table Type defined for the same MY_SCHEMA_OBJ_TBL

And while creating the ArrayDescriptor if you pass the Object then it cause this problem

Wrong

ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor(“MY_SCHEMA_OBJ”,con);
Correct
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor(“MY_SCHEMA_OBJ_TBL”,con);


Thanks
R Vashi

Advertisements

One thought on “Common Exception in Java and their workarounds

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s