【发布时间】:2015-10-20 15:08:30
【问题描述】:
我的动态 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)的 properties 向导完成了这项工作。这没有用!我已将 mysql-connector-java-5.1.37 添加到类路径中。我还尝试将其直接添加到 WEB-INF/lib 文件夹中,可惜没有成功。我尝试的最后一件事也是不成功,就是将它添加到 tomcat7 库中。
以上方法似乎都不起作用。我究竟做错了什么?我在其他帖子中找不到可行的建议。
【问题讨论】:
-
您是否尝试将 lib/ 下的 *.JAR 直接链接到您的项目中?您应该通过项目属性的配置将其指定给 eclipse 运行时。提供大量在线教程,例如 wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java) 来设置构建和/或运行时路径。
-
是的,我基本上尝试将它放在每个 /lib 文件夹下。如果需要,可以通过项目属性窗口将其添加到构建路径中。他们都没有工作。
-
Deschamps,您是否尝试过复制/移动“*/WebContent/WEB-INF/lib”下的库?在这里,一个有用的链接:javahotchocolate.com/tutorials/web-jars.html.