gpt4 book ai didi

java - 使用mysql的jdbc连接

转载 作者:可可西里 更新时间:2023-11-01 07:42:38 26 4
gpt4 key购买 nike

 import java.sql.*;

public class MysqlConnect{

public static void main(String[] args) {

System.out.println("MySQL Connect Example.");

Connection conn = null;

String url = "jdbc:mysql://localhost:3306/";

String dbName = "sint";

String driver = "com.mysql.jdbc.Driver";

String userName = "root";

String password = "najeer";

try {

Class.forName(driver).newInstance();

conn = DriverManager.getConnection(url+dbName,userName,password);

System.out.println("Connected to the database");

conn.close();

System.out.println("Disconnected from database");

} catch (Exception e) {

e.printStackTrace();
}

}

}

大家好我正在尝试这个.. 然后

c:/>java MysqlConnect.java

C:\>java -cp .;\local\lib\mysql-connector-java-5.1.15-bin.jar MysqlConnect

MySQL 连接示例。

Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/SQLClientInf
oException

at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.mysql.jdbc.ConnectionImpl.<clinit>(ConnectionImpl.java:270)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MysqlConnect.main(MysqlConnect.java:15)

发生这种类型的错误请帮助任何人....

最佳答案

如果您可以在类路径中包含 rt.jar,那么异常就会消失。

关于java - 使用mysql的jdbc连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5594163/

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