【发布时间】:2014-12-16 13:52:46
【问题描述】:
我正在尝试连接到 MySQL 数据库,我在 localhost 上使用 phpmyadmin 创建了 SQL 数据库。我的代码似乎是正确的,并且我已将 JAR 文件添加到 eclipse 中。我用谷歌搜索了这个错误,在这里发现了许多相同但无法解决问题的主题。这是我的代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class dbconnection {
public static void main (String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:msql://localhost:3306/g52apr","root", "");
java.sql.PreparedStatement statement = con.prepareStatement("select * from std_details");
ResultSet result = statement.executeQuery();
while (result.next()){
System.out.println(result.getString(1) + " " + result.getString(2));
}
}
}
这是确切的错误:
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:3306/g52apr
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at dbconnection.main(dbconnection.java:10)
【问题讨论】:
-
快速浏览互联网表明您应该将连接字符串更改为
jdbc:microsoft:sqlserver://localhost:3306/g52apr -
我确实尝试过,但没有任何改善我仍然得到错误
-
您尝试运行此代码的机器上是否安装了 sql server?
-
我正在尝试使用 eclipse 运行代码,就像我在消息中所说的那样
-
您使用了错误的 jar。您正在加载 MySQL 驱动程序并尝试连接到 SQL 服务器数据库,但这是行不通的。使用正确的驱动程序。