Saturday, May 2, 2009

Establishing a connection with ORACLE EXPRESS(XP) in Java:

Assuming the Oracle Express Server is installed on a remote machine and you want your web application to connect to it.

The Oracle express client does not creates a tnsnames.ora on the client machine. This means, the oracle client folder does not contain the /ADMIN/NETWORK folder structure at all. So, create a new DNS giving username, pwd and check whether the connection is successful. If the connection is successful then with the following steps in your Java code.

Problem:
If the exception you are getting is similar to the one I got, then go for the checklist mentioned below:
  • Exception in thread "main" java.sql.SQLException: ORA-01034: ORACLE not available.
ORA-27101: shared memory realm does not exist
  • Exception in thread "main" java.sql.SQLException: Io exception: The Network Adapter could not establish the connection.
Resolution/ Checklist:
1. Check whether you are actually connected to the database. Try creating a DSN on client machine and checkwhether the connection is successful.
2. Set the Username and Password correctly.
3. Check for the URL in connection string, check for the machine name, IP and SID you mentioned in the setURL();

SID: By default it is XE,
URL:ocpds.setURL("jdbc:oracle:thin:@192.168.91.210:1521:XE");

Sample Code:
private void init()throws SQLException {
try {
OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();
java.util.Properties p = new java.util.Properties();
p.setProperty("oracle.jdbc.V8Compatible", "true");
ocpds.setConnectionProperties(p);
ocpds.setURL("jdbc:oracle:thin:@192.168.91.210:1521:XE");
ocpds.setUser("scott");
ocpds.setPassword("manager");
pool = ocpds.getPooledConnection();
}catch(SQLException ex){
ex.printStackTrace();
throw ex;
}
}

Cheers,
-Ibu