permaculture playing cards*
The moose likes Servlets and the fly likes re: cannot connect to the database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "re: cannot connect to the database" Watch "re: cannot connect to the database" New topic
Author

re: cannot connect to the database

Dipanjan Paul
Greenhorn

Joined: Nov 16, 2000
Posts: 5
Hi,
I am new to servlets and I am using javawebserver2.0. I have written a program which si going to use the database wich is in mycase a text file and I am trying to use the database abut I am not being able to. The program is getting compiled but when I try to run it is showing a 501 error. I am attaching the program alongwith.I need urgent help on this.
thankximport java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.*;
public class ServletA extends HttpServlet
{
Connection dbcon;
public void init() throws ServletException
{
try
{
String driver = getInitParameter("JdbcDriver");
String dbURL = getInitParameter("dbURL");
Class.forName(driver);
dbcon = DriverManager.getConnection(dbURL,"","");
}
catch (ClassNotFoundException e)
{
System.out.println("DB Driver not found");
System.out.println(e.toString());
throw new UnavailableException(this, "DB Driver class");
}
catch (SQLException e)
{
System.out.println("Error in connect");
System.out.println(e.toString());
throw new UnavailableException(this, "Cannot connect");
}
}

public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Search Results</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
try
{
Statement st=dbcon.createStatement();
ResultSet rec=st.executeQuery(
"Select Item_Name,Item_Number "+
"from Items");
while(rec.next())
{
out.println("Item_Name:" + rec.getString("Item_Name"));
out.println("Item_Number:" + rec.getString("Item_Number"));
}
st.close();
}
catch(Exception e)
{
}
out.println("</BODY></HTML>");

out.close();
}
}
,
dp
P SOLAIAPPAN
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Hi,
You have mentioned that you are using text database file for your program, then how come you are trying to make a connectivity to a database server.
what is your JdbcDriver & dbURL ??
If you are using Windpws 98 with Access data base , Are you defining DSN for your mdb files.
solaiappan
 
jQuery in Action, 2nd edition
 
subject: re: cannot connect to the database
 
Similar Threads
Pl help me with this href portion in this code
J2EE server not connecting to database
problems in servelts
First Servlet, Jdbc program :(
re: how do i link my first servlet to the second and then to third etc?