JDBC没有找到合适的驱动程序; java.sql.SQLException:找不到适用于jdbc:derby:JH6Test; create = true的驱动程序

时间:2019-11-18 23:37:13

标签: java jdbc netbeans

我不断收到此错误:java.sql.SQLException:找不到适用于jdbc:derby:JH6Test; create = true的驱动程序。 “ JH6Test”是我的项目名称。在同一项目和包中,我还有另一个类使用MyConnection类进行连接,并且它工作得很好(我用它来创建表)。这是我的代码:

package DB_PersonCollection;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbc.*;

public class DB_PersonServlet extends HttpServlet {

/**
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    ServletContext servletContext = getServletContext();
    //ConnectionPool connectionPool = (ConnectionPool) servletContext.getAttribute("connectionPool");

    Connection connection ;
    Statement statement ;
    String errorMessage = "";
    try {
        connection = MyConnection.getConnection("JH6Test");
        statement = connection.createStatement();

        if (statement != null ) {
            errorMessage = DB_PersonCollection.update(statement, request);
            statement.close();      
        }
        if (connection == null) {
            System.out.println("Connection Is Null");
            //connectionPool.free(connection);
        }

        if (connection != null) {
            System.out.println("Connection Not Null");
            //connectionPool.free(connection);
        }
    } catch (SQLException ex) {
        errorMessage = ex.toString();
    }

    request.setAttribute("errorMessage", errorMessage);

    RequestDispatcher dispatcher =
            getServletContext().getRequestDispatcher("/personCollection.jsp");

    dispatcher.forward(request, response);
}

/**
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doGet(request, response);
}
}

MyConnection类如下:

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class MyConnection {

/**
 *
 * @param projectName
 * @return
 * @throws SQLException
 */
public static Connection getConnection(String projectName) throws SQLException {
    String urlStr = "jdbc:derby:" + projectName + ";create=true";
    return DriverManager.getConnection(urlStr);
}
}

我在项目下的Libraries文件夹中有derby.jar。我该如何解决?

0 个答案:

没有答案