我正在尝试连接到 phpMyAdmin 和 Eclipse 上托管的 MySQL 数据库。我已将 JDBC bin 文件导入到包中,并通过 XAMPP 访问 phpMyAdmin。这是代码:
import java.sql.Connection;
import java.sql.DriverManager;
public class database {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
System.out.println("Connection successful");
} catch (Exception e) {
System.err.println(e);
}
}
}
但是当我编译它时,它说:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test'
这是什么意思?我正在观看一个 YouTube 教程,该教程执行完全相同的操作,但已建立连接。
最佳答案
首先注册您的驱动程序:
Class.forName("com.mysql.jdbc.Driver");
您的代码应该是:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/test","root","");
System.out.println("Connection successful");
} catch (Exception e) {
System.err.println(e);
}
关于java - 连接Mysql和eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32790135/