Servlet Connectivity with database????
I have written one program to connect to database(orcle 9i) using servlets. Thereis no compilation error in the program but still the programme does not retrieve any valus from the database. Can anyone tell me whats the problem with the following code. please ignore the username and password in the programme.
import java.io.*;
import java.sql.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DBConnServlet extends HttpServlet{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String passwd = " ";
public void doGet(HttpServletRequest req , HttpServletResponse res) throws ServletException , IOException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
connect();
out.println("<HTML><HEAD><TITLE>DBConnection</TITLE></HEAD>");
out.println("<BODY><BR>The password is : ");
out.println(passwd);
out.println("<BR></BODY></HTML>");
}
/* This method connects to the database*/
public void connect()
{
try
{
System.out.println("Just for debugging purposes");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:MyConn","username","passwd");
ps = conn.prepareStatement("select * from sample where name=?");
ps.setString(1,"a");
rs = ps.executeQuery();
rs.next();
passwd = passwd + rs.getString(2);
System.out.println("The passwd is "+passwd);
ps.close();
conn.close();
}
catch( SQLException sql)
{
System.out.println("SQL Exception Caught");
}
}
}
in the above code MyConn is the DSN connection i created using DataSources from ControlPanel.
Krish
|