• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet connection with Jdbc

 
Lakshmi siri
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see any messages printed in your console?
[ February 18, 2004: Message edited by: Bruce Jin ]
 
Lakshmi siri
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Where should i check the console.
--Lakshmi
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To pin down the error, add e.printStackTrace() in try/catch.
 
Lakshmi siri
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use System.err.println("Ecception: "+e.getStackTrace());

In general, Oracle provide two drivers, are you using the correct one?
Dan
 
danny liu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Now i got result.I set the classpath in Tomcat setclasspath.Now my program is running.
Thanks for your help.
--Lakshmi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic