Tomcat 数据库连接德比

Tomcat Database connection derby

我正在尝试使用 Tomcat 在 Netbeans 上编写 Web 应用程序。当我尝试连接到 sql 数据库时遇到问题: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/mydbname

我已经在类路径和 WEB-INF/lib 文件夹中包含了 derby.jar 和 derbyclient.jar。我还创建了一个单独的 java 文件,我可以在其中 访问我的数据库:我没有收到任何错误,一切都很好,但是当我尝试通过 [=29= 连接时] 我收到上面提到的驱动程序错误!

这是我的 servlet java 文件:

public class Servlet extends HttpServlet {

Connection con = null;
String url = "jdbc:derby://localhost:1527/Onlineshop";   

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection(url);
        System.err.println("connection established");
    } catch (Exception ex) {
        Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null,ex);
    }
    response.setContentType("text/plain");
    response.getWriter().println("connected to database");
    request.getRequestDispatcher("/ServletHTML").forward(request, response);
    }
}

非常感谢帮助!

很明显你搞砸了 MySql Derby 配置。

在成功连接到 Derby 数据库的快速步骤中,您需要更改:

String url="jdbc:derby://localhost:1527/Onlineshop;create=true;user=me;password=mine";

然后加载驱动程序并建立这样的连接:

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
//Get a connection
conn = DriverManager.getConnection(url); 

有关完整示例,请参阅 this

希望对您有所帮助