• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

re: cannot connect to the database

 
Dipanjan Paul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic