eclipse mysql

eclipse如何连接mysql

小新
170
2020-12-10 15:26:41
栏目: 云计算

eclipse如何连接mysql

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.代码如下:

import java.sql.*;

public class test{  

public static void main(String args[]){    

try{      

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

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

System.out.println("Success loading Mysql Driver!");    

}    

catch(Exception e){      

System.out.print("Error loading Mysql Driver!");      

e.printStackTrace();    

}    

try{      

Connection connect = DriverManager.getConnection(          

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

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

}catch(SQLException e){  

}

}

}

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

0
看了该问题的人还看了