There are generally 2 ways to connect to Oracle in a JSP:
1, Oracle jdbc Oci8 Way
2, Oracle jdbc Thin way
I prefer the 2nd, because the Web publisher and the database server are generally not on the same computer, and when using thin connection, the Web server side does not need to install Oracle clients.
Before we start the code, we'll configure the environment properly. From the database server where Oracle is installed, locate the Oracle installation directory, and then copy the Jdbc\lib\classes12.jar files from that directory to a directory on the Web Publisher. Suppose you put it directly in the C:\ root directory, and then add the path to the ' System-advanced-environment variable ' value in the variable named ' CLASSPATH ', such as: D:\Program files\sqllib\java\db2java.zip;d:\ Program Files\sqllib\java\runtime.zip;c:classes12.jar; So that Java can find this package.
After configuring the environment, we began to write code. About the database connection code, should write a special connection class to call, no need to think of some articles on the network like that, write directly to the JSP code.
The code for the connection is simple
private Connection newConnection(String user,String password) {
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
con = DriverManager.getConnection (“jdbc:oracle:thin:@192.168.96.1:1521:oracle9i”,user,password);
}
catch (SQLException e) {
return null;
}
return con;
}