【问题标题】:Exception in thread "main" java.sql.SQLException: No suitable driver found for线程“主”java.sql.SQLException 中的异常:找不到合适的驱动程序
【发布时间】: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 服务器数据库,但这是行不通的。使用正确的驱动程序。

标签: java mysql eclipse jdbc


【解决方案1】:
"jdbc:msql://localhost:3306/g52apr"

应该是:

"jdbc:mysql://localhost:3306/g52apr"

已编辑将 SQL Server 引用更改为 MySql

【讨论】:

  • 提问者已澄清他们正在尝试连接到 MySQL 数据库,因此问题只是被错误地标记了。您可能需要编辑或删除您的答案以解决该新信息。
  • 最容易犯的错误。我已经犯了几十次甚至数百次类似的错误。
猜你喜欢
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 2016-11-28
  • 2011-08-02
  • 2012-08-28
  • 2015-07-05
相关资源
最近更新 更多