eclipse连接mysql数据库

eclipse连接数据库挺简单的,由于我需要做综合实验,需要连接数据库,居然发现忘记了,所以就想写下来,给更多忘记如何连接数据库的朋友,上干货。

1. 导入jar包

新建项目后右击项目(怎么建项目自己琢磨)->build path->configure build path
在这里插入图片描述
选择Libraries,点击Add External JARs…,选择jia包所在的文件夹,引入就可以了,点击Apply and close搞定
在这里插入图片描述
在这里插入图片描述
无论是java se项目,还是java ee 项目,都会在相应目录下显示引入的jar包
在这里插入图片描述
没有jar包的来这提取:
链接:https://pan.baidu.com/s/1T75WxUTTLTMxiOIKBqYKRg
提取码:bldq

2.编写程序检测是否成功

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.junit.Test;

public class DBUtil {

	public static Connection getConnection() throws ClassNotFoundException, SQLException {
		Connection conn = null;
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/db_librarySys?useUnicode=true&characterEncoding=UTF-8&user=root&password=root";
		/*此处根据自己的数据库进行相应的修改,db_librarySys为数据库的名字,user和password为数据库的用户和密码*/
		conn = DriverManager.getConnection(url);
		return conn;
	}
	
	@Test
	public void testConn() throws ClassNotFoundException, SQLException {
		if (getConnection() != null) {
			System.out.println("连接成功");
		}
	}
}

如果没有报错则连接成功!搞定!!!

在连接之后可能会报错,比如这个错误

Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

很恶心的一段文字,其实仔细看看之后他已经将解决办法告诉你了,添加useSSL=true就可以了

String url = "jdbc:mysql://localhost:3306/db_librarySys?useUnicode=true&characterEncoding=UTF-8&useSSL=true&user=root&password=root";

好了,就这些,我写这些比较基础,请各位支持,第一次写不好请见谅,希望大神指点。

  • 10
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值