温馨提示×

eclipse如何连接mysql

小新
187
2020-12-10 15:26:41
栏目: 云计算
亿速云mysql数据库,读写分离,安全稳定,弹性扩容,低至0.3元/天!! 点击查看>>

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.运行代码测试,当连接成功时,控制台会打印成功连接的信息。

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

推荐阅读:eclipse和mysql怎么连接

0