您的位置:宽带测速网 > 网络知识 > eclipse如何连接mysql

eclipse如何连接mysql

2025-06-17 10:01来源:互联网 [ ]

eclipse连接mysql的方法:

1.需要准备个mysql的jar驱动文件,下载地址https://www.mysql.com/products/connector/

2.找到JDBC Driver for MySQL (Connector/J),下载配置。

3.在eclipse配置好jdbc后。

4.新建一个CLASS类。

5.输入代码进行连接数据库测试。

6.代码如下:

importjava.sql.*;

publicclasstest{

publicstaticvoidmain(Stringargs[]){

try{

Class.forName("com.mysql.cj.jdbc.Driver");//加载MYSQLJDBC驱动程序

//Class.forName("org.gjt.mm.mysql.Driver");

System.out.println("SuccessloadingMysqlDriver!");

}

catch(Exceptione){

System.out.print("ErrorloadingMysqlDriver!");

e.printStackTrace();

}

try{

Connectionconnect=DriverManager.getConnection(

"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT","root","xxxxxxx");

//连接URL为jdbc:mysql//服务器地址/数据库名,后面的2个参数分别是登陆用户名和密码

}catch(SQLExceptione){

}

}

}

7.运行代码测试,当连接成功时,控制台会打印成功连接的信息。