JDBC的复习(二):使用IDEA开发
配置驱动
- 创建项目
- 打开Module setting
-
点击+号后找到自己的数据库架包
没有的话可以到官网下载:
- 点击ok
- 显示就成功
测试是否导入:
package com.bjpowernode.jdb;
/**
* @author 31200
*/
public class JdbcTest06 {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
导入成功!!!
创建项目
怎样创建目录的时候自动创建子级包?
参考这个博客:
https://blog.csdn.net/sinat_36158016/article/details/107423723
创建数据库表
模拟用户登陆
- main函数
public static void main(String[] args) {
//初始化用户界面
Map<String,String> userLoginInfo = InitUI();
//登陆函数
boolean flag = Login(userLoginInfo);
//输出结果
System.out.println(flag? "登陆成功!":"登陆失败!");
}
- 初始化UI界面
/**
* 初始化用户界面
* @return 用户输入的用户名和密码登陆信息
*/
private static Map<String, String> InitUI() {
Map<String,String> userLoginInfo = new HashMap<>();
Scanner s = new Scanner(System.in);
System.out.println("用户名:");
String userName = s.nextLine();
System.out.println("密码:");
String passWord = s.nextLine();
userLoginInfo.put("useName",userName);
userLoginInfo.put("passWord",passWord);
return userLoginInfo;
}
- 登录函数
/**
* 登陆函数,判断登陆是否成功
* @param userLoginInfo
* @return 返回登陆是否成功
*/
private static boolean Login(Map<String, String> userLoginInfo) {
boolean flag = false;
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
String userName = userLoginInfo.get("useName");
String passWord = userLoginInfo.get("passWord");
try {
//1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2. 获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?characterEncoding=UTF8&useSSL=false","root","001204");
//3. 获取操作对象
stat = conn.createStatement();
//4. 执行sql语句
String sql = "select * from userinfo where userName = '"+userName+"' and passWord = '"+passWord+"'";
rs = stat.executeQuery(sql);
//5. 处理查询结果集合
if(rs.next()){
flag = true;
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
//释放资源
if(rs!=null){
rs.close();
}
}catch (Exception e){
e.printStackTrace();
}
try {
//释放资源
if(stat!=null){
stat.close();
}
}catch (Exception e){
e.printStackTrace();
}
try {
//释放资源
if(conn!=null){
conn.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
return flag;
}
注意:如果报错 WARN: Establishing SSL connection without server's identity verification is not recommended.
Mon Feb 28 12:33:02 CST 2022 WARN: 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.
解释:
周一2月28日12:33:02 CST 2022警告:不建议在没有服务器身份验证的情况下建立SSL连接。根据MySQL 5.5.45+、5.6.26+和5.7.6+的要求,如果未设置显式选项,默认情况下必须建立SSL连接。为了符合不使用SSL的现有应用程序,verifyServerCertificate属性设置为“false”。您需要通过设置useSSL=false显式禁用SSL,或者设置useSSL=true并为服务器证书验证提供信任库。
解决方案:
在连接数据库的配置后面增加useSSL=false。
测试成功!!