aspose file tools*
The moose likes Servlets and the fly likes Servlet connection with Jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet connection with Jdbc" Watch "Servlet connection with Jdbc" New topic
Author

Servlet connection with Jdbc

Lakshmi siri
Ranch Hand

Joined: Feb 06, 2004
Posts: 44
Hi,
The following program is servlet with Jdbc connection.
Iam using oracle 9i as databse.
There are no errors while compiling.
When i run on tomcat server
http://localhost/servlet/servletwithjdbc
iam not getting any thing on explorer.
Iam getting empty page.
How can i run this program to get results.
Program:

import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class servletwithjdbc extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out=null;
Connection con=null;
con=connect();
Statement stmt=null;
ResultSet rs=null;
resp.setContentType("text/html");
out=resp.getWriter();
try
{
stmt=con.createStatement();
rs=stmt.executeQuery("select ename,job from scott.emp ");
out.println("<HTML><HEAD><TITLE>EMPDETAILS</TITLE><HEAD>");
out.println("<BODY>");

out.println("<UL>");
while(rs.next())
{
out.println("<L1>"+rs.getString("ename")+" "+rs.getString("job"));
}
out.println("</UL>");
out.println("</BODY></HTML>");



System.out.println("end iof listing");
}
catch(Exception e)
{
System.err.println("Exception:"+e.getMessage());

}
finally{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{ }
}
}
protected static Connection connect()
{
String userID="SCOTT";
String password="TIGER";
Connection con=null;
String url="jdbc racle:thin:@localhost:1521:bachi";
try
{


Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(url,userID,password);
}
catch (Exception e)
{
System.err.println("Ecception: "+e.getMessage());
}
return con;
}
}
Thanks for your help.
--Lakshmi
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 671
Did you see any messages printed in your console?
[ February 18, 2004: Message edited by: Bruce Jin ]

BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Lakshmi siri
Ranch Hand

Joined: Feb 06, 2004
Posts: 44
Hi,
Where should i check the console.
--Lakshmi
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 671
when you start Tomcat you have a dos window opened. Check there.
In case you run it as service and don't have a window, check Tomcat log files.
Lakshmi siri
Ranch Hand

Joined: Feb 06, 2004
Posts: 44
Hi,
In console i saw the following error.
Exception: oracle.jdbc.driver.OracleDriver
Exception: null
do i need to write any classpath?
Thanks for your help.
--Lakshmi
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Welcome to the Ranch "lakshmi"!
You'll find this forum a great place to seek help on servlets, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 671
To pin down the error, add e.printStackTrace() in try/catch.
Lakshmi siri
Ranch Hand

Joined: Feb 06, 2004
Posts: 44
Hi,
i add e.getStackTrace in the catch block
catch(SQLException e)
{
e.getStackTrace();
}
I got the following message on explorer.

type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
servletwithjdbc.doGet(servletwithjdbc.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:466)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

note The full stack trace of the root cause is available in the Tomcat logs.
--Lakshmi
danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
Use System.err.println("Ecception: "+e.getStackTrace());

In general, Oracle provide two drivers, are you using the correct one?
Dan
danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
Check the following line
con=connect();
check if con is null. If it is, then the connection is not build due
to error parameters.
Dan
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Lakshmi",
Please see Bear's post above.
Accounts with invalid display names get deleted. Please change your display name to something valid or your account will be deleted very soon.
thanks,
Dave.
Satheesh Chandra
Greenhorn

Joined: Feb 19, 2004
Posts: 2
Hi,
It following line shows that the line no 32 may have error. Please execute your qry as sql and check you sql is correct and returning atleast one row.
servletwithjdbc.doGet(servletwithjdbc.java:32)

Lakshmi siri
Ranch Hand

Joined: Feb 06, 2004
Posts: 44
Hi all,
Now i got result.I set the classpath in Tomcat setclasspath.Now my program is running.
Thanks for your help.
--Lakshmi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet connection with Jdbc