Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Fernando Mendez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic