File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes servlet-jdbc help!!!!!!!!!!!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet-jdbc help!!!!!!!!!!!!!!!" Watch "servlet-jdbc help!!!!!!!!!!!!!!!" New topic
Author

servlet-jdbc help!!!!!!!!!!!!!!!

Fernando Mendez
Greenhorn

Joined: Jun 06, 2004
Posts: 10
excuse my english.

hi.

i`m having a lot of problem with a simple servlet-jdbc, i`m using tomcat 5.26 and jdk 1.5 the code that i use is this:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Driver;
public class HolaServlet extends HttpServlet {

private Connection con;
private PrintWriter out;
private Statement stmt;

public void init(ServletConfig conf)
throws ServletException {

super.init(conf);

}

public void service(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");

try{



out = res.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title> Sample JDBC Servlet Demo" +
"</title>");
out.println("</head>");
out.println("<body>");

try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();

}
catch(ClassNotFoundException e)
{

System.out.println("error de clase no encontrada " + e.getMessage());
}

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ejemplo?user=root");


stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM empleados");

out.println("<UL>");

rs.next();

out.println("<LI>" + rs.getString("nombre") + "<LI>");


out.println("</UL>");
rs.close();

stmt.close();
con.close();
}


catch(IOException e) {

System.err.println("An IOException was thrown.");

}
catch(SQLException e)
{
out.println();
out.println("An SQL Exception was thrown."+ e.getMessage() + e.getSQLState());
}
catch(InstantiationException e)
{
System.out.println(e.getMessage());
}
catch(IllegalAccessException e)
{
System.out.println(e.getMessage());
}

out.println("</body>");

out.println("</html>");
out.close();
}

}

when i try to put this " Class.forName("com.mysql.jdbc.Driver").newInstance(); " in the Init method, i get a NullPointerException, but when i leave it where is it now , i get an SQLException: "no suitable driver".

i don`t now, if before i use a servlet-jdbc with tomcat i need to configure something, i have try everything to solve this. plisss help!!!

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
Hi Fernando,
I guess you need to put driver file in tomcat\comman\lib folder...
im not sure..but it is classes111.zip file...
Cheers
Praful


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
 
wood burning stoves
 
subject: servlet-jdbc help!!!!!!!!!!!!!!!
 
Similar Threads
database connection in servlet on Tomcat 5.5
Servlet connection with Jdbc
small problem with query
Connecting to Database from servlets(Can anyone give reply)
Connection Pooling example appears to create more than one pool