我的第一个jdbc连接db2的程序
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class test1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String JDBCDriver = "COM.ibm.db2.jdbc.app.DB2Driver";
String DbSource = "jdbc:DB2:sample";
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Connection conn = DriverManager.getConnection(DbSource,
"用户名", "密码");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from sales");
int i = 1;
while(rs.next()) {
System.out.println(rs.getString(1) + " " + i++ + "n");
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException cnfe) {
System.err.println("Unable to load driver.");
cnfe.printStackTrace();
} catch (SQLException se) {
System.err.println("sql error.");
se.printStackTrace();
} catch (Exception E) {
System.err.println("other exception");
E.printStackTrace();
}
}
}
代码中使用的3个jar文件在db2安装目录(D:IBMSQLLIBjava)下,名称为:db2java.zip,db2jcc.jar,db2jcc_license_cu.jar
[@more@]来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26482/viewspace-1024313/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26482/viewspace-1024313/