覚書

JDBCDB2に接続する例。すぐ忘れるので...


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Statement;


public class JDBC_DB2_Test01 {
static Driver driver = null;
static final String JDBC12 = "IBM DB2 JDBC 1.2";
static final String JDBC20 = "IBM DB2 JDBC 2.0";
static final String url = "jdbc:db2:";
static Method mCreateStatement = null;
static {
try {
driver = (Driver)Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
} catch (Exception e) {
}
}

public static void main(String argv[])
{
if (null == driver)
System.exit(-1);

Connection con = null;

try
{
if (argv.length == 0)
{
// Connect with default database-alias (SAMPLE), username and
// password
con = DriverManager.getConnection(url + "SAMPLE");
}
else if (argv.length == 1)
{
// Connect with user-provided database-alias; default username and
// password
con = DriverManager.getConnection(url + argv[0]);
}
else if (argv.length == 2)
{
// Connect with default database-alias (SAMPLE); user-provided
// username and password
con = DriverManager.getConnection(url + "SAMPLE", argv[0], argv[1]);
}
else if (argv.length == 3)
{
// Connect with user-provided database-alias, username and password
con = DriverManager.getConnection(url + argv[0], argv[1], argv[2]);
}
else
{
System.out.println(
"\nUsage: java db2JDBCVersion [database-alias | " +
"[database-alias] username password]\n");
System.exit(-1);
}

// getDriverName indicates JDBC version as of:
// DB2 v6.1 FixPak 8
// DB2 v7.1 FixPak 2a
DatabaseMetaData dbmd = con.getMetaData();
String JDBCVersion = dbmd.getDriverName();

if (JDBCVersion.indexOf("1.2") >= 0)
{
JDBCVersion = JDBC12;
}
else if (JDBCVersion.indexOf("2.0") >= 0)
{
JDBCVersion = JDBC20;
}
// Old version of DB2, use alternate method of determining JDBC version
// i.e. check if method Connection.createStatement(int, int) exists
else if (getJDBC20CreateStatementMethod() == null)
{
JDBCVersion = JDBC12;
}
else
{
JDBCVersion = JDBC20;
}

// If class files are JDBC 2.0 version, check that the
// native libraries are too
if (JDBCVersion == JDBC20 && !verifyJDBC20(con))
{
JDBCVersion = null;
}

con.close();

if (null == JDBCVersion)
{
printBadEnvironmentMessage();
System.exit(-1);
}
else
{
System.out.println(JDBCVersion);
}
}
catch (Exception e)
{
e.printStackTrace();
System.exit(-1);
}
}

synchronized static private Method getJDBC20CreateStatementMethod()
{
if (null == mCreateStatement)
{
try
{
Class c = Class.forName("COM.ibm.db2.jdbc.app.DB2Connection");
Class params[] = new Class[]{ Integer.TYPE, Integer.TYPE };
mCreateStatement = c.getDeclaredMethod("createStatement", params);
}
catch (Exception e)
{
printBadEnvironmentMessage();
}
}

return mCreateStatement;
}

static private void printBadEnvironmentMessage()
{
System.out.println(
"Bad DB2 JDBC environment; please run usejdbc1 or usejdbc2 again " +
"and check for errors.");
}

static private boolean verifyJDBC20(Connection con)
{
Method m = getJDBC20CreateStatementMethod();

if (null == m)
{
return false;
}

Object params[] = null;
Statement stmt = null;

try
{
params = new Object[]{
new Integer(1004), // ResultSet.TYPE_SCROLL_INSENSITIVE
new Integer(1007) }; // ResultSet.CONCUR_READ_ONLY

stmt = (Statement)m.invoke(con, params);
}
catch (Exception e)
{
return false;
}

// Attempt to execute a batch statement, if the native library is
// really the JDBC 1.2 version, then the following exception will
// be thrown:
// InvocationTargetException->UnsatisfiedLinkError
try
{
Class c[] = new Class[]{ Class.forName("java.lang.String") };
m = stmt.getClass().getDeclaredMethod("addBatch", c);

params = new Object[]{ "insert into dummy values (1)" };
m.invoke(stmt, params);

m = stmt.getClass().getDeclaredMethod("executeBatch", null);
m.invoke(stmt, null);
}
catch (InvocationTargetException e)
{
// Should get here if JDBC 2.0 is working properly
Throwable t = e.getTargetException();
if (t instanceof UnsatisfiedLinkError)
{
return false; // Indicates wrong DB2 JDBC library
}
}
catch (Exception e)
{
return false;
}
finally
{
try
{
stmt.close();
}
catch (Exception e)
{
}

try
{
con.rollback();
}
catch (Exception e)
{
}
}

return true;
}

}