Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

servlet-jdbc help!!!!!!!!!!!!!!!  RSS feed

 
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!!!

 
Ranch Hand
Posts: 643
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!