JDBC Connection problem

I wrote a class to connect to an ORACLE database to select employer names.

The proper drivers gets loaded but when the connection is made, I get an "array out of bounds" error message.

Here is part of the code:

import java.sql.*;

=========================================================

/**

*

* @author Phlip Pretorius

*/

public class getEmplName

{

/** Creates a new instance of getEmplName */

public getEmplName()

{

} //end getEmplName

/* C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14_g.jar

* jdbc:oracle:thin:@127.0.0.1:1521:XE

* oracle.jdbc.driver.OracleDriver

*/

public static void main(String args[])

{

//String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";

String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";

System.out.println("1xxx Dis voor Connection con = null");

Connection con = null;

try

{

System.out.println("2xxx Dis voor String driver = oracle.jdbc.driver.OracleDriver");

String driver = "oracle.jdbc.driver.OracleDriver";

System.out.println("3xxx Dis voor Class.forName(driver).newInstance()");

Class.forName(driver).newInstance();

} // end try

catch( Exception e )

{

System.out.println("Failed to load Oracle driver.");

return;

} //end catch

try

{

System.out.println("4xxx Dis voor con = DriverManager.getConnection(url, hr, hr)");

con = DriverManager.getConnection(url,"hr","hr");

System.out.println("5xxx Dis voor Statement select = con.createStatement()");

Statement select = con.createStatement();

ResultSet result = select.executeQuery

("SELECT * FROM HR.EMPLOYEES order by EMPLOYEE_ID");

=========================================================

The program getrs executed up to the point 4xxx but then get the error message

1xxx Dis voor Connection con = null

2xxx Dis voor String driver = oracle.jdbc.driver.OracleDriver

3xxx Dis voor Class.forName(driver).newInstance()

4xxx Dis voor con = DriverManager.getConnection(url, hr, hr)

java.lang.ArrayIndexOutOfBoundsException: 7

at oracle.security.o3logon.C1.r(C1)

at oracle.security.o3logon.C1.l(C1)

at oracle.security.o3logon.C0.c(C0)

at oracle.security.o3logon.O3LoginClientHelper.getEPasswd(O3LoginClientHelper)

at oracle.jdbc.ttc7.O3log.<init>(O3log.java:290)

at oracle.jdbc.ttc7.TTC7Protocol.logon(TTC7Protocol.java:251)

at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:252)

at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:365)

at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:260)

at java.sql.DriverManager.getConnection(Unknown Source)

at java.sql.DriverManager.getConnection(Unknown Source)

at getEmplName.main(getEmplName.java:51)

=========================================================

I wll appreciate any help that can resolve this issue.

Thanks in advance

Phlip

[3159 byte] By [ppp03a] at [2007-11-27 9:33:48]
# 1

My guess is the exception occurs in the 'createStatement()' statement (line with 5xxx is not executed completely, throws an error).

That statement calls many layers of supporting objects the vendor of createStatement uses to carry out that statement. One of them has a problem.

System.out.println("5xxx Dis voor Statement select = con.createStatement()");

Try passing the sql directly into the statement as in:

con.createStatement("SELECT * FROM HR.EMPLOYEES order by EMPLOYEE_ID")

then call the following with no argument.

ResultSet result = select.executeQuery();

******************

Here is a cleaner way of getting and using a connection (with proper use of closing the connection). Note you never close the database, 'static means its only instansiated once no matter how many functions get a connection.

Note connection is a local variable in the function, therefore its threadsafe.

private static DataSource dataSource= DriverManager.getConnection(url, hr, hr)");

public ArrayList myFunction(){

Connection conn=null;

PreparedStatement pstmt1= null;

ResultSet resultSet=null;

ArrayList list1;

try{

list1=new ArrayList();

conn= dataSource.getConnection();

conn.setAutoCommit(false);

pstmt1= conn.prepareStatement();

resultSet= pstmt1.executeUpdate();

while(resultSet.next()){

arrayList.add(resultSet.getString("lastName");

}

conn.commit();

return arrayList;

} catch (SqlException e){

if(conn!=null)

conn.rollback();

e.printStackTrace();

} finally {

if(conn!=null)

conn.setAutoCommit(true);//must ensure this is done first because

//following lines might throw an error

if(rsultSet!=null)

resultSet.close();

if(pstmt1!=null)

pstmt1.close();

if(conn!=null)

conn.close();

}

}

}

George123a at 2007-7-12 22:56:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Hi George,Thanks for the feedback.The suggestion "Try passing the sql directly into the statement as in:con.createStatement("SELECT * FROM HR.EMPLOYEES order by EMPLOYEE_ID")" does not compile. I have not tried your second suggestion yet.Phlip
ppp03a at 2007-7-12 22:56:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...