jdbc:db2://"+server+":"+port+"/"+databaseName 中的port是要用db2的命令db2jstrt来启动的,比如进到C:\Program Files\SQLLIB\bin,输入命令: db2jstrt 11111,然后再执行你的程序就可以了。java实例(提示:参数server可写IP也可写机器名,port必须是db2jstrt启动的端口号;另外我这里使用sample数据库,你只要换成你的数据库名即可):import java.sql.*;public class db2net { static { try { Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Connection con; try { // get parameter values from the html page String server = "9.185.25.113";//getParameter("server"); String port = "11111";//getParameter("port"); // construct the URL ( sample is the database name ) String url = "jdbc:db2://"+server+":"+port+"/sample"; String userid = "administrator";//getParameter("userid"); String password = "123456";//getParameter("password"); // connect to database with userid and password con = DriverManager.getConnection(url, userid, password ); // retrieve data from database System.out.println("First, let's retrieve some data from the database..."); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * from employee"); System.out.println("Received results:"); // display the result set // rs.next() returns false when there are no more rows int i = 0; while (rs.next() && (i<2)) { i++; String a= rs.getString(1); String str = rs.getString(2); String oneLine = " empno= " + a + " firstname= " + str; System.out.println(oneLine); } stmt.close(); // update the database System.out.println("Now, update the database..."); stmt = con.createStatement(); int rowsUpdated = stmt.executeUpdate("UPDATE employee set firstnme = 'SHILI' where empno = '000010'"); // display the number of rows updated String msg = "Updated " + rowsUpdated; if (1 == rowsUpdated) msg = msg +" row."; else msg = msg +" rows."; System.out.println(msg); stmt.close(); } catch( Exception e ) { e.printStackTrace(); } } }
还有
web-inf/lib/这个目录好像不存在阿?
1 com要大写COM
COM.ibm.db2.jdbc.net.DB2Driver 或
COM.ibm.db2.jdbc.app.DB2Driver
2 url与类不匹配
jdbc:db2://localhost:5000/sample 要使用net型的.3 保证db2java.zip在类路径中
此文件在sqllib\java\下
好像不在sqllib\java\下吧?没有这个目录,是不是/usr/IBMdb2/V7.1/java/db2java.zip?
怎么把db2java.zip加入类路径呢???
如果你的WEB服务器是TOMCAT
就把它放在和JSP文件同一目录下的web-inf/classes下。
db2jstrt 11111,然后再执行你的程序就可以了。java实例(提示:参数server可写IP也可写机器名,port必须是db2jstrt启动的端口号;另外我这里使用sample数据库,你只要换成你的数据库名即可):import java.sql.*;public class db2net { static {
try {
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Connection con;
try {
// get parameter values from the html page
String server = "9.185.25.113";//getParameter("server");
String port = "11111";//getParameter("port"); // construct the URL ( sample is the database name )
String url = "jdbc:db2://"+server+":"+port+"/sample"; String userid = "administrator";//getParameter("userid");
String password = "123456";//getParameter("password"); // connect to database with userid and password
con = DriverManager.getConnection(url, userid, password );
// retrieve data from database
System.out.println("First, let's retrieve some data from the database..."); Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from employee");
System.out.println("Received results:"); // display the result set
// rs.next() returns false when there are no more rows
int i = 0;
while (rs.next() && (i<2)) {
i++;
String a= rs.getString(1);
String str = rs.getString(2);
String oneLine = " empno= " + a + " firstname= " + str;
System.out.println(oneLine); }
stmt.close(); // update the database
System.out.println("Now, update the database...");
stmt = con.createStatement();
int rowsUpdated = stmt.executeUpdate("UPDATE employee set firstnme = 'SHILI' where empno = '000010'"); // display the number of rows updated
String msg = "Updated " + rowsUpdated; if (1 == rowsUpdated)
msg = msg +" row.";
else
msg = msg +" rows.";
System.out.println(msg); stmt.close();
} catch( Exception e ) {
e.printStackTrace();
}
}
}
如果在不可以,给我短信,我给你一个连接db2数据库的类。
应该使用COM.ibm.db2.jdbc.net.DB2Driver
再有你的驱动就是db2java.zip那个文件可以在db2安装路径\java\下面找到
放到一个你指定的路径下,再配置环境变量中的classpath 例如你把那个驱动文件放到了e:\driver\db2java.zip
那么你的CLASSPATH中也要加入一条e:\driver\db2java.zip;关于版本问题,只要作到你程序使用的jdbc驱动版本和远程数据库使用的一样
就可以并不一定象 wjmmml(笑着悲伤) 说的要升级
看样子你用的是AIX吗,正确安装了应该就在类路径中.
看看我说的1,2点吧.