Answer the question
In order to leave comments, you need to log in
ClassNotFoundException error when using Servlet?
Guys, I found a simple tutorial on the net on using a servlet and JDBC.
There are two classes:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class DirectorsViewServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
DirectorSelect ds = new DirectorSelect();
out.println(ds.selectDirectors());
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
doGet(req, res);
}
}
import java.sql.*;
class DirectorSelect{
public String selectDirectors() {
Connection koneksi = null;
Statement stat = null;
String str = "";
try{
Class.forName("com.mysql.jdbc.Driver");
koneksi = DriverManager.getConnection("jdbc:mysql://localhost/dbizza","root","");
stat = koneksi.createStatement();
ResultSet hasil = stat.executeQuery("SELECT * FROM directors");
while (hasil.next()) {
str = str + (hasil.getInt(1) + "\t" + hasil.getString(2)+ "\t" + hasil.getString(3)+ "\t" + hasil.getDate(4)+ "\t" + hasil.getString(5));
}
stat.close();
koneksi.close();
} catch (SQLException sqle) {
str = "SQLException error";
} catch (ClassNotFoundException cnfe) {
str = "ClassNotFoundException error";
}
return str;
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>DirViewServlet</servlet-name>
<servlet-class>DirectorsViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DirViewServlet</servlet-name>
<url-pattern>/directors-view</url-pattern>
</servlet-mapping>
</web-app>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question