The following articles mainly describe the correct operation steps for connecting jsp and java to ibm DB2. We all know that after the DB2 database is installed, you can copy the installation directory of the DB2 database, which is generally in C: \ Program Files \ IBM \ SQLLIB \ java‑based db2java.zip to the website lib, with the extension changed to jar.
Connection test:
- Class.forName("COM.ibm.DB2.jdbc.app.DB2Driver").newInstance();
- //DriverManager.registerDriver (new COM.ibm.DB2.jdbc.app.DB2Driver());
You do not need to use the previous line. You can use this line.
- String url = "jdbc: DB2: Database ";
- Connection con = DriverManager. getConnection (url, "user", "password ");
- Statement stmt = con. createStatement ();
- ResultSet rs = stmt.exe cuteQuery ("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 ('China ')";
- Stmt.exe cuteUpdate (cc );
In jcc mode, copy DB2jcc. jar to the lib directory of the website.
- <%
- Try
- {
- String url = "jdbc: DB2: // loaclhost: 50000/Database ";
- Class. forName ("com. ibm. DB2.jcc. DB2Driver"). newInstance ();
- Connection con = DriverManager. getConnection (url, "user", "password ");
- Statement stmt = con. createStatement ();
- ResultSet rs = stmt.exe cuteQuery ("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 ('China ')";
- Stmt.exe cuteUpdate (cc );
- }
- Catch (Exception Err)
- {
- Out. print (Err. toString ());
- }
- %>
The above content is an introduction to connecting jsp and java to ibm DB2. I hope you will have some gains.