• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

re: cannot connect to the database

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic