以下的文章主要描述的是jsp & java 连接ibm DB2的正确操作步骤,我们大家都知道安装了DB2数据库之后,就可以复制DB2数据库的安装目录下(一般在C:\Program Files\IBM\SQLLIB\java)的DB2java.zip至网站lib下,扩展名改为jar.
连接测试:
- Class.forName("COM.ibm.DB2.jdbc.app.DB2Driver").newInstance();
- //DriverManager.registerDriver (new COM.ibm.DB2.jdbc.app.DB2Driver());
不用上一行,用这一行也可以
- String url = "jdbc:DB2:数据库";
- Connection con = DriverManager.getConnection(url,"用户","密码");
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select * from ADMINISTRATOR.T2 order by id desc fetch first 10 rows only");
- while (rs.next())
- {
- out.print(rs.getString(1) + "," + rs.getString(2)+"<br>");
- }
- String cc = "INSERT INTO ADMINISTRATOR.T2 (TITLE) values('中国的')";
- stmt.executeUpdate(cc);
jcc方式,复制DB2jcc.jar到网站lib下
- <%
- try
- {
- String url = "jdbc:DB2://loaclhost:50000/数据库";
- Class.forName("com.ibm.DB2.jcc.DB2Driver").newInstance();
- Connection con = DriverManager.getConnection(url,"用户","密码");
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select * from ADMINISTRATOR.T2 order by id desc fetch first 10 rows only");
- while (rs.next())
- {
- out.print(rs.getString(1) + "," + rs.getString(2)+"<br>");
- }
- String cc = "INSERT INTO ADMINISTRATOR.T2 (TITLE) values('中国的')";
- stmt.executeUpdate(cc);
- }
- catch(Exception Err)
- {
- out.print(Err.toString());
- }
- %>
以上的相关内容就是对jsp & java 连接ibm DB2的介绍,望你能有所收获。
【编辑推荐】