Writing a Servlet Using NuoDB
I am starting to use NuoDB
. I have a task to redo mine servlet-application
so that it can use the database NuoDB
. So this is my new servlet method doGet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//doPost(request, response);
String outPage = "<h1>Output:</h1><br>";
List<Info> list_cars = new ArrayList<Info>();
try
{
//DriverManager.registerDriver(new com.nuodb.jdbc.Driver());
Class.forName("com.nuodb.jdbc.Driver");
DBWorker db = new DBWorker("dba", "goalie", "cars");
list_cars = db.getUser();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
for (int i = 0; i < list_cars.size(); i++)
{
outPage += "Car name: " + list_cars.get(i).getCarName() + "<br>";
outPage += "Car color" + list_cars.get(i).getCarColor() + "<br>";
outPage += "Car size: " + list_cars.get(i).getCarSize().toString() + "<br>";
outPage += "Car release: " + list_cars.get(i).getCarRelease().toString() + "<br>";
outPage += "-------------<br>";
}
Map<String, String> map = new HashMap<String, String>();
map.put("data", outPage);
PrintWriter out = response.getWriter();
String res = Templater.view("results", map);
out.println(res);
}
This is my class that works with the database:
public class DBWorker
{
/**
* The driver class provided by NuoDB.
*/
public static final String DRIVER_CLASS = "com.nuodb.jdbc.Driver";
/**
* The base URL for connecting to a local database server.
*/
public static final String DATABASE_URL =
"jdbc:com.nuodb://localhost:8080/";
// the established connection to a local server
private Connection dbConnection;
public DBWorker(String user, String password, String dbName) throws SQLException
{
Properties properties = new Properties();
properties.put("user", user);
properties.put("password", password);
properties.put("schema", "HOCKEY");
dbConnection = DriverManager.getConnection(DATABASE_URL + dbName, properties);
}
public void insertUser(String name, String color, Integer size, Date release) throws SQLException
{
PreparedStatement stmt = dbConnection.prepareStatement("insert into CARS (CAR_NAME, CAR_COLOR, CAR_SIZE, CAR_RELEASE) values (?, ?, ?, ?)");
try
{
stmt.setString(1, name);
stmt.setString(2, color);
stmt.setInt(3, size);
stmt.setDate(4, new java.sql.Date(release.getTime()));
stmt.addBatch();
stmt.executeBatch();
}
catch (Exception exception)
{
System.out.println("Skipping insert...");
}
dbConnection.commit();
}
public List<Info> getUser() throws SQLException
{
Statement stmt = dbConnection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CARS");
List<Info> users = new ArrayList<Info>();
try
{
while (rs.next())
{
users.add(new Info(rs.getString(2), rs.getString(3), rs.getInt(4), new Date(rs.getDate(5).getTime())));
}
}
finally
{
rs.close();
stmt.close();
}
return users;
}
}
My problem is that I cannot write and read anything in the database. Errors in the console:
java.lang.ClassNotFoundException: com.nuodb.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at testPackage.CarServlet.doGet(CarServlet.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
How to fix it?
source to share
The problem is the NuoDB JDBC bank is not in your classpath.
The NuoDB documentation states that it is under the directory where NuoDB is installed, under a subdirectory named jar
.
You must either add this location to your path to your web application (assuming your tomcat is on the same machine as your database) or copy the jar to where tomcat can find it, along with all the other jars you're using.
source to share