gpt4 book ai didi

java.lang.ClassNotFoundException : com. mysql.jdbc.Driver(jar 文件已链接并放置在 WEB-INF/lib 中)

转载 作者:行者123 更新时间:2023-11-30 01:08:32 24 4
gpt4 key购买 nike

我针对此错误找到的解决方案是“通过构建路径添加 jar 文件”和“将包含驱动程序的 jar 文件放在 WEB-INF/lib 中”(以及类似的表述),但我已经尝试过其中两个。

回到我的问题。这是我的代码:

jsp 文件

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="db.DBAccess" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>

<jsp:useBean id="dba" class="db.DBAccess"></jsp:useBean>

<jsp:setProperty name="dba" property="selection" value="s" />

</body>
</html>

javabean

package db;

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

public class DBAccess {

public DBAccess() {
}

private String selection = "";

public void setSelection(String s) {

final String driver = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://localhost:3306/test";
String user = "user1";
String pswd = "user1pswd";

Connection con = null;
Statement stmt = null;

try {
// initialize connection
Class.forName(driver);
con = DriverManager.getConnection(url, user, pswd);
stmt = con.createStatement();
// execute select
ResultSet results = stmt.executeQuery("SELECT * FROM t");
results.next();
selection = results.getString(1);
// close connection
stmt.close();
con.close();
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe.toString());
} catch (SQLException e) {
System.out.println(e.toString());
}

}
}

t 的内容是一列,标签为“string”,内容为“hello world”

即使我正确链接了 jar 文件,为什么还是会出现此异常?

最佳答案

您是否将 jar 文件插入到 Webinf 的 lib 文件夹中?

关于java.lang.ClassNotFoundException : com. mysql.jdbc.Driver(jar 文件已链接并放置在 WEB-INF/lib 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19609411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com