在odbc数据源设置好后采用如下方式访问,数据源名为test
String driver;
try
{
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver).newInstance();
}
catch (Exception e)
{
System.out.println(e);
System.exit(1);
}
try
{
sql = DriverManager.getConnection"jdbc:odbc:model", "user", "pass");
}
catch(SQLException e)
{
System.out.println("Database connection failed!");
System.out.println(e.getMessage());
System.exit(1);
}
String driver;
try
{
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver).newInstance();
}
catch (Exception e)
{
System.out.println(e);
System.exit(1);
}
try
{
sql = DriverManager.getConnection"jdbc:odbc:model", "user", "pass");
}
catch(SQLException e)
{
System.out.println("Database connection failed!");
System.out.println(e.getMessage());
System.exit(1);
}
DB2 对Java支持得很好。
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();//创建驱动程序实例
Connection connection = DriverManager.getConnection(getDbUrl(), getDbUser(), getDbPassword());//建立与数据库的连接(db位置,user,pass)
CallableStatement callableStatement1; //
String sql="(call BBTJ_PS_SJJY(?,?,?,?))";
callableStatement1 = connection.prepareCall (sql);
callableStatement1.registerOutParameter(3, Types.INTEGER);
callableStatement1.registerOutParameter(4, Types.REAL);
callableStatement1.setString(1, s);
callableStatement1.setString(2, s1);
callableStatement1.execute();
setSj_Jybs(callableStatement1.getInt(3));
setSj_Je(callableStatement1.getFloat(4));
callableStatement1.close();
connection.close();
return;