我的动态 Web 项目遇到问题。这是我的第一个项目,我想做一些相当简单的事情。我想打印出数据库中的数据。我用的是MYSQL,并且已经建立了数据库。我想使用以下代码进行连接:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// Set the MIME type for the response message
response.setContentType("text/html");
// Get a output writer to write the response message into the network socket
PrintWriter out = response.getWriter();
Connection conn = null;
Statement stmt = null;
try {
// Step 1: Create a database "Connection" object
// For MySQL
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/webshop", "root", "admin"); // <<== Check
} catch (SQLException ex) {
ex.printStackTrace();
}
}
我得到了与其他帖子已经提到的相同的消息:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/webshop
我从其他帖子中了解到,必须将连接器添加到类路径中。我通过使用 Eclipse(我正在使用的 IDE)的属性向导来完成此操作。这不起作用!我已将 mysql-connector-java-5.1.37 添加到类路径中。我也尝试将其直接添加到 WEB-INF/lib 文件夹中,可惜没有成功。我尝试的最后一件事是将其添加到 tomcat7 库,但也没有成功。
以上似乎都不起作用。我究竟做错了什么?我在其他帖子中找不到工作建议。
最佳答案
正如this page中指出的那样,你应该:
- 从this page下载mysql连接器jar文件。
- 将下载的 jar 文件放入您的
lib
文件夹中。 在编写代码之前,您应该执行
加载 mysql 派生程序
,如下所示:Class.forName("com.mysql.jdbc.Driver");
关于java - 未找到 JDBC 驱动程序,即使我已将其添加到类路径中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33184973/