wood burning stoves 2.0*
The moose likes Servlets and the fly likes DataSource Pooling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "DataSource Pooling" Watch "DataSource Pooling" New topic
Author

DataSource Pooling

Ramesh Pappala
Ranch Hand

Joined: Sep 14, 2005
Posts: 50
I had a problem in the following servlet program.

I added the resource tag to "server.xml",which contains all the details about database connection and about the number of connections it has to open.

<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/myoracle">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc racle:thin:@ORASRV:1521:MIRACLE</value>
</parameter>
<parameter>
<name>username</name>
<value>itg10</value>
</parameter>
<parameter>
<name>password</name>
<value>itg10</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>


And my "web.xml" file contains


<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

<resource-ref>
<description>Oracle DataSource Example </description>
<res-ref-name>jdbc/myoracle </res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<servlet>
<servlet-name>employee</servlet-name>
<servlet-class>EmployeeDetails</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>employee</servlet-name>
<url-pattern>/remployee</url-pattern>
</servlet-mapping>

</web-app>

And my servlet class contains

import javax.servlet.*;
import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import javax.naming.InitialContext.*;

public class EmployeeDetails extends GenericServlet
{
public void init()
{
System.out.println("Servlet Intialised");
}
public void service(ServletRequest request,ServletResponse response) throws ServletException,IOException
{
PrintWriter out=null;
try
{
response.setContentType("text/html");
out=response.getWriter();

Context initContext=new InitialContext();
Context encContext=(Context)initContext.lookup("java:/comp/env");
DataSource ds=(DataSource)encContext.lookup("jdbc/myoracle");
//DataSource ds=(DataSource)initContext.lookup("java:/comp/env/jdbc/myoracle");
Connection con=ds.getConnection();
System.out.println("Connection Created");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from emp1");
//Statement st1=con.createStatement();
//ResultSet rs1=st1.executeQuery("Select * from r_employee");
out.println("<html><body><br>");
while(rs.next())
{
out.println(rs.getInt(1)+"<br>");
}
//out.println("<br><br>R_Employee");
//while(rs1.next())
//{
//out.println(rs1.getInt(1)+"<br>");
// }

out.println("Resultset Processed </body></html>");
out.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
out.println(e);
}
}
public void destroy()
{
System.out.println("Servlet Destroyed");
}
}

and i wrote the html file also

iam getting an error as

java.sql.SQLException: Cannot load JDBC driver class 'null'

Is there is any problem with in server.xml or in the servlet class file

Please tell me where is the problem and how to solve it.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Two things that don't really solve your problem, but will definately help anyway...

First, edit your post and put the code and xml between the CODE brackets. It will make your post much easier to read.

Second, why are you overriding the service method? You should be overriding the doGet() or doPost() or do***() methods in the servlet instead.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Ricardo Alcazar
Greenhorn

Joined: Oct 28, 2005
Posts: 11
Just a thought...

Have you verified that your Oracle JDBC packages/archives have been included in the tomcat/commons/lib or tomcat/commons/classes directory?

Regards,
R. Alcazar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DataSource Pooling