Linux 拨号vps windows公众号手机端

eclipse如何连接mysql

lewis 7年前 (2018-11-28) 阅读数 12 #VPS/云服务器
文章标签 eclipsemysql

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门