This article mainly introduces the process of JDBC connecting to Informix IDs, mainly including environment description, JDBC Configuration and demo code.
1. Environmental description
OS: Windows XP
Informix: IDS V10.00.TC1
JDBC: Informix JDBC Embedded SQLJ V2.20JC2
2.JDBC Configuration
After installing Informix JDBC, add the Ifxjdbc.jar path to the CLASSPATH environment variable, such as
Classpath=c:\ifxjava_home\lib\ifxjdbc.jar;
After the installation of the directory has a doc directory, which has a detailed document description. Also has the demo directory, Inside has can refer to the source code
3.DEMO Code
The following source code, is the reference (\doc\release\oij_jdbc_migration.html)
Using JDBC to connect to IDs via Java V10.0
Import java.sql.*;
Import java.util.*;
public class Ifx_con
{
public static void Main (string[] args)
{
Connection Conn;
String url = "Jdbc:informix-sqli://ibm-henry:
1526/sample:informixserver=
Ol_henry;user=henry;password=happyday ";
System.out.println ("Informix JDBC Connect Test.");
Try
{
Load the Informix JDBC Driver
Drivermanager.registerdriver ((Driver) class.forname
("Com.informix.jdbc.IfxDriver"). newinstance ());
Class.forName ("Com.informix.jdbc.IfxDriver");
Create and open a server/database connection
conn = drivermanager.getconnection (URL);
System.out.println ("JDBC Driver name:"
+ Conn.getmetadata (). Getdrivername ());
Queries ' return to more than one row
Statement query = null;
ResultSet rs = null;
String st = new String ();
Try
{
query = Conn.createstatement ();
rs = Query.executequery ("SELECT * from Customer");
while (Rs.next ())
{
System.out.println (rs.getstring (2));
}
Rs.close ();
Query.close ();
}
catch (SQLException exce)
{
System.out.println ("Caught:" + exce.geterrorcode ());
}
Conn.close ();
}
catch (ClassNotFoundException Drvex)
{
System.err.println ("Could not load JDBC driver");
System.out.println ("Exception:" + Drvex);
Drvex.printstacktrace ();
}
catch (SQLException Sqlex)
{
while (Sqlex!= null)
{
SYSTEM.ERR.PRINTLN ("SQLException information");
System.err.println ("Error msg:" + sqlex.getmessage ());
System.err.println ("SQLSTATE:" + sqlex.getsqlstate ());
System.err.println ("Error code:" + Sqlex.geterrorcode ());
Sqlex.printstacktrace ();
Sqlex=sqlex.getnextexception ();
}
}
}
}