gpt4 book ai didi

java - 找不到 jdbc 的驱动程序(仅在 servlet 上)

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:38 26 4
gpt4 key购买 nike

我在从 servlet 连接到 MySQL 数据库时遇到问题。这是我的连接代码

private static void connectToDatabase(){
try{
static Connection conn = null;
static String url = "jdbc:mysql://localhost/database?userinfo";
conn = DriverManager.getConnection(url);
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connected");
} catch(ClassNotFoundException wyjatek) {
System.out.println("Problem ze sterownikiem");
} catch(SQLException wyjatek) {
System.out.println("SQLException: " + wyjatek.getMessage());
System.out.println("SQLState: " + wyjatek.getSQLState());
System.out.println("VendorError: " + wyjatek.getErrorCode());
}
}

和doGet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();

String param = request.getParameter("lookFor");

out.println("TEST");

connectToDatabase();
}

每次我得到这个

SQLException: No suitable driver found for jdbc:mysql://localhost/database?userinfo
SQLState: 08001
VendorError: 0

我把 mysql-connector-java-5.1.27-bin 放在 webapps\WEB-INF\lib和 apache-tomcat-7.0.55\lib

还是一样。在应用程序的桌面版本中,一切正常。它只发生在 servlet 上。

最佳答案

基本问题是

你的第一步应该是使用这个注册驱动程序

Class.forName("com.mysql.jdbc.Driver");

然后尝试使用它从 DriverManager 获取连接

conn = DriverManager.getConnection(url);

我猜你的做法是相反的

关于java - 找不到 jdbc 的驱动程序(仅在 servlet 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25358141/

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