以下的文章主要描述的是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的介紹,望你能有所收獲。