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)
-
If you get "This page calls for XML namespace http://richfaces.org/a4j declared with prefix a4j but no taglibrary exists for that names...
-
Method 1: import oracle.sql.*; public class JClob { String tableName = null; // String primaryKey = null; // String primaryValue = null; // ...
-
1.HTTP-binding(ServiceMix) 1.4 各组件间传递消息流的过程如下: 1. HttpClient : Http 客户端运行一个独立的 Java 客户端程序,通过其中的 URLConnection 类连接到 http://...
No comments:
Post a Comment