gpt4 book ai didi

java - XAMPP jdbc 驱动程序不加载

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

我做了一个简单的 MySQL 连接程序,但它不起作用。

package main;
import java.sql.*;

public class Main {

static Connection con = null;
static Statement stmt = null;
static ResultSet rs = null;
static String url = "jdbc:mysql://localhost:3306/phpmyadmin";
static String user = "root";
static String password = "(i dont show this ;)";

public static void main(String[] args) {

try {
System.out.println("Connecting database...");
con = DriverManager.getConnection(url, user, password);
System.out.println("Database connected!");
} catch (SQLException e) {
throw new RuntimeException("Cannot connect the database!", e);
} finally {
System.out.println("Closing the connection.");
if (con != null) try { con.close(); } catch (SQLException ignore) {}
}

}

}

我遇到了这个错误:

Connecting database...
Closing the connection.
Exception in thread "main" java.lang.RuntimeException: Cannot connect the database!
at main.Main.main(Main.java:22)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/phpmyadmin
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at main.Main.main(Main.java:19)

我创建了构建路径,并将文件(从连接器)从 lib 文件夹移动到 \xampp\mysql\lib。我也启动了 tomcat(我没有更改任何配置),但它仍然无法正常工作。

最佳答案

您错过了一些重要的步骤,例如以下

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

您可以使用以下链接获取完整说明

http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/

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

public class JDBCExample {

public static void main(String[] argv) {

System.out.println("-------- MySQL JDBC Connection Testing ------------");

try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}

System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;

try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mkyongcom","root", "password");

} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}

if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}

关于java - XAMPP jdbc 驱动程序不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20250708/

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