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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Please do not post the same question more than once.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DataSource Pooling