gpt4 book ai didi

java - JDBC连接MSQL错误 "No suitable driver found"

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

java.sql.SQLException: No suitable driver found for jdbc:mysql@localhost:3306:emp
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.jdbd.connection.ConnectionDemo.main(ConnectionDemo.java:13)

这是我的代码

package com.jdbd.connection;

import java.sql.*;

public class ConnectionDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
try{

//1. get a connection to database

Connection myconn = DriverManager.getConnection("jdbc:mysql@localhost:3306:emp","root","Dreamliner787");
//2.create a statement
Statement mystm =myconn.createStatement();
//3. Execute sql query
ResultSet myRs = mystm.executeQuery("select*from employee");
//4. process the result set
while(myRs.next()){
System.out.println(myRs.getString("last")+ "," + myRs.getString("first"));

}

}
catch(Exception e){
e.printStackTrace();

}
}

}

最佳答案

该错误要么是因为您的 URL 错误,要么是缺少 JDBC 驱动程序。

JDBC URL 通常类似于 jdbc:mysql://localhost:3306/mysql。我不确定为什么你有一个 @ 在那里。但这可能就是问题所在。

您可以通过像这样加载驱动程序来查明问题是否出在类路径中。

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

编辑:

Class.forName 不是 JDBC 特定的。它只是将 Driver 类加载到当前的类加载器中。那里没有与数据库相关的内容。

在 JDBC 4.0 之前,您必须以这种方式初始化驱动程序。我想既然这有效,你一定使用的是较低版本。

关于java - JDBC连接MSQL错误 "No suitable driver found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38427176/

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