Java connection to db2 data code
First: at present, ibm has never provided a jdbc driver for type 1.
Type 2 driver: com.ibm.db2.jdbc.app.db2driver. the driver is also in the package db2java.zip. jdk must be able to access db2's db2jdbc. dll and so on.
Usage:
Class. forname ("com. ibm. db2.jdbc. app. db2driver "). newinstance (); string url = "jdbc: db2: sample"; connection con = drivermanager. the getconnection (url, user, password); type 2 driver enables java applications to call db2through jdbc. The call to db2 jdbc type 2 driver is converted to a java local method. Java applications using such drivers must run on a db2 client. The jdbc request is forwarded to the db2 server through this client.
Before using the db2 jdbc application driver to access the data source in the db2 udb for iseries or db2 for OS/390 or z/OS environment, you must install db2 connect version 8. db2 jdbc type 2 driver supports most of the jdbc and sqlj functions described in the jdbc 1.2 specification, and supports some functions described in the jdbc 2.0 specification.
Type 3 driver: com.ibm.db2.jdbc.net. db2driver, which is located in the package db2java.zip.
Usage:
Class. forname ("com.ibm.db2.jdbc.net. db2driver "). newinstance (); the target connection db2 system listens to this service on the default port 6789 // you need to start the db2jstrt command on the database tutorial server to enable the network port.
String url = "jdbc: db2: // host: 6789: sample"; connection con = drivermanager. getconnection (url, user, password); type 4 driver: com. ibm. db2.jcc. db2driver, which is located in the package db2jcc. jar.
In this case, the database encoding is utf-8. Otherwise, an error is returned!
Usage:
Class. forname ("com. ibm. db2.jcc. db2driver "). newinstance (); string url = "jdbc: db2: // host: 50000/sample"; connection con = drivermanager. getconnection (url, user, password );
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Import java. util. arraylist;
Import java. util. list;
Import java. util. properties;
Public class conn {
Public static connection createconn ()
{
Connection conn = null;
Try {
// Class. forname ("com. mysql tutorial. jdbc. driver ");
Class. forname ("com. ibm. db2.jcc. db2driver"). newinstance ();
String url = "jdbc: db2: // 127.0.0.1: 50000/sample ";
Properties props tutorial = new properties ();
Props. setproperty ("user", "db2admin ");
Props. setproperty ("password", "ibmdb2 ");
Conn = drivermanager. getconnection (url, props );
} Catch (classnotfoundexception e ){
E. printstacktrace ();
} Catch (instantiationexception e ){
E. printstacktrace ();
} Catch (illegalaccessexception e ){
E. printstacktrace ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return conn;
}
Public static preparedstatement prepare (connection conn, string SQL)
{
Preparedstatement stat = null;
Try {
Stat = conn. preparestatement (SQL );
} Catch (sqlexception e ){
// Todo auto-generated catch block
E. printstacktrace ();
}
Return stat;
}
Public static void close (connection conn)
{
If (conn = null) return;
Try {
Conn. close ();
Conn = null;
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
Public static void close (statement stat)
{
If (stat = null) return;
Try {
Stat. close ();
Stat = null;
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
Public static void close (resultset rs)
{
If (rs = null) return;
Try {
Rs. close ();
Rs = null;
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
Public static void main (string [] args ){
Connection conn = createconn ();
System. out. println (conn );
String SQL = "(select * from db2admin. user where id not in (select id from db2admin. user order by id asc fetch first 10 rows only) order by id asc) fetch first 10 rows only ";
Preparedstatement ps;
Try {
Ps = conn. preparestatement (SQL );
Try {
Resultset rsw.ps.exe cutequery ();
While (rs. next ())
{
// System. out. println (rs. getstring ("usercount"); // (1); //. getstring ("name "));
System. out. println (rs. getstring ("id "));
System. out. println (rs. getstring ("name "));
System. out. println (rs. getstring ("password "));
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
Conn. close ();
Ps. close ();
} Catch (sqlexception e1 ){
// Todo auto-generated catch block
E1.printstacktrace ();
}
}
}
Java common database string connection
1. mysql (http://www.mysql.com) mm. Mysql-2.0.2-bin.jar
Class. forname ("org. gjt. mm. mysql. driver ");
Cn = drivermanager. getconnection ("jdbc: mysql: // mydbcomputernameorip: 3306/mydatabasename", susr, spwd );
2. postgresql (http://www.de.postgresql.org) pgjdbc2.jar
Class. forname ("org. postgresql. driver ");
Cn = drivermanager. getconnection ("jdbc: postgresql: // mydbcomputernameorip/mydatabasename", susr, spwd );