• 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

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

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