使用NuoDB编写servlet

时间:2014-10-31 14:24:41

标签: java servlets nuodb

我是使用NuoDB的新手。我有一项任务是重做我的servlet-application,它可以使用NuoDB数据库。 所以,这是我新的doGet servlet方法:

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);                   
}

这是我使用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;
    }
}

我的问题在于我无法在数据库中编写和读取任何内容。 控制台中的错误:

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)

如何解决?

1 个答案:

答案 0 :(得分:2)

问题是NuoDB JDBC jar不在你的类路径中。

NuoDB documentation表示它位于安装NuoDB的目录下,位于名为jar的子目录下。

您必须将该位置添加到Web应用程序类路径(假设您的tomcat与数据库位于同一台计算机上),或者将jar复制到tomcat将能够找到它的位置,以及所有其他jar你正在使用。