1. Copy the ojdbc4.jar/ojdbc4_g.jar to "Tomcat 5.0\common\lib", Use Tomecat console set up the server.xml:
JNDI Name: jdbc/myname
Data Source URL: jdbc:oracle:thin:@localhost:1521:mydb
JDBC Driver Class: oracle.jdbc.driver.OracleDriver
User Name: xxxx
Password: xxxx
Max. Active Connections: 4
Max. Idle Connections: 2
Max. Wait for Connection: 5000
Validation Query:
The server.xml will be looks like:
<globalnamingresources>
<environment value="30" type="java.lang.Integer" name="simpleValue">
<resource description="User database that can be updated and saved" type="org.apache.catalina.UserDatabase" name="UserDatabase" auth="Container">
<resource type="javax.sql.DataSource" name="jdbc/myname">
<resourceparams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</resourceparams>
<resourceparams name="jdbc/myname">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>xxxx</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@localhost:1521:mydb</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.OracleDriver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>xxxx</value>
</parameter>
</resourceparams>
</globalnamingresources>
2. Please add below statement in "conf\Catalina\localhost" your web application "myweb.xml"
<context workdir="work\Catalina\localhost\myweb" path="/myweb" docbase="C:/Program Files/Apache Software Foundation/Tomcat 5.0/webapps/myweb" displayname="myweb">
<resourcelink type="javax.sql.DataSourcer" name="jdbc/e2" global="jdbc/e2">
</context>
Must add it to resolve: CANNOT CREATE JDBC DRIVER OF CLASS FOR CONNECT URL NULL
3.Add below statement in "web.xml" of myweb application
<RESOURCE-REF>
<description>Oracle Development Datasource</description>
<res-ref-name>jdbc/e2</RES-REF-NAME>
<res-type>javax.sql.DataSource</RES-TYPE>
<res-auth>Container</RES-AUTH>
</RESOURCE-REF>
4. For oracle may be need to delete the ojdbc4.jar/ojdbc4_g.jar from the lib of myweb/WEB-INF/lib to avoid : ClassCast Exception. There is a bug for oracle 9i jdbc to operate Clob and Blob. We can’t use javax.sql.Clob in oracle 9i jdbc. Please refer another article.
5. Test code:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="javax.naming.*,javax.sql.DataSource, java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'javabeansource.jsp' starting page</title>
</head>
<body>
<%
out.println("Get in");
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myname");
Connection conn = ds.getConnection();
out.println("ok");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select sysdate from dual");
while(rs.next()){
out.println(rs.getString(1));};
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
This blogger is recording some code samples,technical skill of java,.Net,javascript, css, html for myself use. All articles are coming from my own experience and my collections which are from internet world. If you find any material have copy right issue please leave a comment to me. I will delete it immediately. These articles are writing in English or Chinese. Hope these information can help other technical guys. Thanks for reading.
Subscribe to:
Post Comments (Atom)
-
Method 1: import oracle.sql.*; public class JClob { String tableName = null; // String primaryKey = null; // String primaryValue = null; // ...
-
1. Change the tile of web.xml from <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "ht...
-
While starting the server pass the IP address using the -b option. For eg: if you want to access the page using IP address, start the server...
No comments:
Post a Comment