Wednesday, July 30, 2008

JDBC connectivity to informix

To Run A JDBC program , following steps should be followed
1. Install the appropriate JDBC libraries according to ur Operating System/System configuration
2. Need to finalize the URL to connect to the Database
3. need to include the Directory of the JDBC library to the "CLASSPATH" environment varaiable.
4. Run the Program.

We can get appropriate JDBC driver from different Vendors.. following link will give the Details about the which Driver to Choose and all
http://developers.sun.com/product/jdbc/drivers
Download the appropriate Driver and install.

The main thing in running a JDBC program is deciding the Conncetion URL.

The URL string looks like this:
"jdbc:informix-sqli://hostname:port_num/database_name:informixserver=server;user=user;password=pwd"

where jdbc is the main protocol and informix-sqli is the sub protocol
and all other fields are self explanatory

example link :
jdbc:informix-sqli://scsibox:1526/my_data:informixserver=ol_scsibox;user=informix;password=Informix

use the Driver name as "com.informix.jdbc.IfxDriver"

so remaining all we can find from SUN help... i.e.. different classess and all..


one sample program:

import java.sql.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import sqlj.runtime.ref.DefaultContext;

public class ConnectionManager
{
String DRIVER = null ; //JDBC Driver class
String DBURL = null ; //Database URL
String PWD = null, UID = null ; //Password for database account
DefaultContext ctx;
Statement stmt;
public Connection conn;
public Driver d;
String Query = null;
ResultSet rs = null;
int rc = -1;

//This block has to be changed and ConnectionManager.java recompiled

//Assign Default values for Driver loading...
public ConnectionManager(){
DRIVER = "com.informix.jdbc.IfxDriver";
DBURL = "jdbc:informix-sqli://10.128.96.66:5030/omcdb:informixserver=omc_sys1;"+
"username=informix;password=informix";
UID = "informix";
PWD = "informix";

//Establish connection
newConnection();
}

public ConnectionManager(String sDriver, String sDBUrl,String sUID,String sPWD){
DRIVER = sDriver;
DBURL = sDBUrl;
UID = sUID;
PWD = sPWD;

//Establish connection
newConnection();
}

public void newConnection()
{
try
{
d = (Driver)(Class.forName( DRIVER ).newInstance());
DriverManager.registerDriver(d);
System.out.println("Informix Driver Successfully Created....");
}
catch (Exception e)
{
System.err.println( "Could not load driver: " + DRIVER ) ;
System.err.println(e) ;
System.exit(1) ;
}

try
{
conn = DriverManager.getConnection (DBURL, UID, PWD);
//conn.setAutoCommit(false); // Turn AutoCommit off
System.out.println("Created Connection successfully to the Database");
}
catch (SQLException exception)
{
System.out.println("Error: could not get a connection");
System.err.println(exception) ;
exception.printStackTrace();
System.exit(1);
}
}


public void setSQLQuery(String query){
Query = query;
}

//This Method is only to execute statments which dont return Result set...
public int executeQuery(){
rc = -1;
try{
stmt = conn.createStatement();
rc = stmt.executeUpdate(Query);
System.out.println("Transaction successful .. Updated Rows : " + rc);
}catch(SQLException e){
System.out.println("FAILED: execution failed - statement: " + Query);
System.out.println("ERROR: " + e.getMessage());
e.printStackTrace();
}

try{
stmt.close();
}catch(SQLException e){
}

return rc;
}

//This shoulb be used with select like queries... which result Resultset...
public ResultSet fetchResultset(){
rs = null;
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(Query);
System.out.println("Fetched Result Set Successfully");
}catch(SQLException e){
System.out.println("FAILED: execution failed - statement: " + Query);
System.out.println("ERROR: " + e.getMessage());
e.printStackTrace();
}

try{
stmt.close();
}catch(SQLException e){
}

return rs;
}

public void closeResultset(){
try{
rs.close();
rs = null;
}catch(SQLException e){
}

}

public DefaultContext initContext(){
ctx = DefaultContext.getDefaultContext();

if (ctx == null) {
try {
newConnection();
ctx = new DefaultContext(conn);
}
catch (SQLException e) {
System.out.println("Error: could not get a default context");
System.err.println(e) ;
System.exit(1);
}
DefaultContext.setDefaultContext(ctx);
}
return ctx;
}
}

No comments: