• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting to Database from servlets(Can anyone give reply)

 
Devi R
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to connect to database by using servlets... The code that i am trying is as follows
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TestJdb extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Connection conn;
Statement stmt;
String url = "jdbc dbc:lak";
String sql = "select * from course";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head></head><body><b>database</b>");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
out.println("registered the driver");
}
catch(java.lang.ClassNotFoundException e)
{
out.println("Class not found exception:");
out.println(e.getMessage());
}
catch(Exception e)
{
out.println("exception:");
out.println(e.getMessage());
}
try{
conn = DriverManager.getConnection(url,"system","manager");
out.println("<b>connection succeeded");
}
catch(SQLException e)
{
out.println("SQL Excpetion occured in connection");
}
out.println("</body></html>");
}
}
I am getting SQLException.... Actually I have tried the same logic by using application, it was working, but with servlets it is givin SQLException.... I have tried somany ways... I could not resolve it.... Can somebody tell me what's wrong in the code...
Thanks
Lakshmi
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This could be one of many problems. To help in the debugging you really should print out more information about your SQLException, using e.printStackTrace() is a very useful technique.
Is the strange jdbc url deliberate, or is there really a ':' and a 'o' missing? Does your server have access to the right DSN?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that The Java Ranch has a naming policy, described here and "Devi R" is not a valid name. Please choose one which meets the requirements.
Thanks.
 
Devi R
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The detailed error is:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
It is saying that Data source not found and no default driver is specified... But i tried the same data source name with the application, it is working... I don't know what is the problem with JRun... Do i have to configutre any thing for Microsoft ODBC Driver Manager....
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you configure the DSN as a System DSN? If it is a User DSN, the JRun service would not see it unless it was running as the user who owned the DSN.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic