I have written a simple JDBC and it works well (I run it in command prompt using java and javac).
I also have written a simple HelloworldServlet and it works well (using Apache Tomcat 7.0). I access the servlet using browser, http://www.myhost.com:8088/projecthello/servlethello, and it displays the Helloworld.
Now I'm trying to combine a simple servlet and JDBC. I want to make a query SELECT to a table, and display it in my browser using this URL: http://www.myhost.com:8088/projectmovie/directors-view
In folder /WEB-INF/classes, I have 2 files: DirectorsViewServlet.java and DirectorSelect.java
Here's DirectorsViewServlet.java:
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);
}
}
And here's the DirectorSelect.java:
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;
}
}
And here's my web.xml file:
<?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>
The problem is, when I access it in my browser with this local URL http://www.myhost.com:8088/projectmovie/directors-view
The result is "ClassNotFoundException error". How to solve it?
I read someone saying servlet and JDBC are somewhat orthogonal tecnology. Any further explanation of what is that mean? And why?
Thanks.