jdbc六步:直接来两个代码(记住)
package day03;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/*
实现功能:
1、需求:
模拟用户登录功能的实现。2、业务描述:
程序运行的时候,提供一个输入的入口,可以让用户输入用户名和密码用户输入用户名和密码之后,提交信息,
java程序收集到用户信息Java程序连接数据库验证用户名和密码是否合法
合法:显示登录成功
不合法:显示登录失败3、数据的准备:
在实际开发中,表的设计会使用专业的建模工具,我们这里安装一个建模工具: PowerDesigner使用PD工具来进行数据库表的设计。(参见user-login.sql脚本)
此程序存在sql注入!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
public class B {
public static void main(String[] args) {
//1.初始化一个界面
Map<String, String> userLoginInfo = initUI();
//2.验证用户名和密码
boolean loginSuccess = login(userLoginInfo);
//3.输出结果
System.out.println(loginSuccess ? "登陆成功" : "登陆失败");
}
/**
* 用户登陆
*
* @param userLoginInfo 包装称map的用户登陆信息
* @return false表示失败 true表示成功
*/
private static boolean login(Map<String, String> userLoginInfo) {
boolean isSuccess = false;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "123456");
// 3.获取数据库操作对象
statement = connection.createStatement();
// 4.执行sql语句
String sql = "select * from t_user where loginName = '" + userLoginInfo.get("loginName") + " ' and loginPwd = '" + userLoginInfo.get("loginPwd") + "'";
/*
* 当前程序存在的问题:
* 用户名:fdsa
* 密码:fdsa' or '1'='1登录成功
* 此时sql语句变成了 select * from t_user where loginName = 'fdsa' and loginPwd = 'fdsa' or '1' = '1' 从而破换了sql语句的意思
* 这种现象被称为sql注入(安全隐患)。(黑客经常使用)
*
* 那么导致SQL注入的根本原因是什么?
* 用户输入的信息中含有sql语句的关键字,并且这些关键字参与sql语句的编译过程,导致sql语句的原意被扭曲,进而达到sql注入。
*/
resultSet = statement.executeQuery(sql);
// 5.处理查询结果集
if (resultSet.next()) {
isSuccess = true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6.释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return isSuccess;
}
/**
* 初始化用户界面
*
* @return 用户的用户名和密码等登录信息
*/
private static Map<String, String> initUI() {
Scanner scanner = new Scanner(System.in);
System.out.print("用户名:");
String userName = scanner.nextLine();
System.out.print("密码:");
String userPwd = scanner.nextLine();
Map<String, String> userLoginInfo = new HashMap<>();
userLoginInfo.put("loginName", userName);
userLoginInfo.put("loginPwd", userPwd);
return userLoginInfo;
}
}
package day03;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/*
1、解决sql注入问题?
只要用户提供的信息不参与sql语句的编译过程,问题就解决了。
即使用户提供的信息中含有sql语句的关键字,但是没有参与编译,不起作用。
要想用户信息不参与sql语句的编译,那么必须使用java.sql.PreparedStatement
PreparedStatement接口继承了java.sql.Statement
PreparedStatement是属于预编译的数据库操作对象。
PreparedStatement的原理是:预先对sql语句的框架进行编译,然后再给sql语句传"值”。
2.解决sql注入的关键是什么?
用户提供的信息中即使含有sql语句的关键字,但是这些关键字不参与编译,只是一个字符串,没有起作用
3.对比Statement和PreparedStatement?
-Statement存在sql注入问题
-Statement是编译一次执行一次,PreparedStatement是编译一次执行N次
-PreparedStatement会在编译阶段做类型的安全检查
所以:一般都是用PreparedStatement,只有极少数的情况需要用Statement。
比如业务方面需要sql注入的时候,就要用Statement
*/
public class C {
public static void main(String[] args) {
//1.初始化一个界面
Map<String, String> userLoginInfo = initUI();
//2.验证用户名和密码
boolean loginSuccess = login(userLoginInfo);
//3.输出结果
System.out.println(loginSuccess ? "登陆成功" : "登陆失败");
}
/**
* 用户登陆
*
* @param userLoginInfo 包装称map的用户登陆信息
* @return false表示失败 true表示成功
*/
private static boolean login(Map<String, String> userLoginInfo) {
boolean isSuccess = false;
Connection connection = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
// 1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "123456");
// 3.获取预编译的数据库操作对象
//sql语句的框架,其中一个?表示一个占位符,将来接受一个值,注意占位符不能使用‘’括起来
String sql = "select * from t_user where loginName = ? and loginPwd = ?";
//程序执行到此处会发送sql语句框子给DBMS,然后DBMS会进行程序的预编译
ps = connection.prepareStatement(sql);
//需要给占位符?传值,第一个问号下标为1,第二个问号下标为2,JDBC中所有下标从1开始
ps.setString(1, userLoginInfo.get("loginName"));
ps.setString(2, userLoginInfo.get("loginPwd"));
// 4.执行sql语句
resultSet = ps.executeQuery();//这里()内不要填sql,即我们写的sql语句,否则会再编译一遍
// 5.处理查询结果集
if (resultSet.next()) {
isSuccess = true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6.释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return isSuccess;
}
/**
* 初始化用户界面
*
* @return 用户的用户名和密码等登录信息
*/
private static Map<String, String> initUI() {
Scanner scanner = new Scanner(System.in);
System.out.print("用户名:");
String userName = scanner.nextLine();
System.out.print("密码:");
String userPwd = scanner.nextLine();
Map<String, String> userLoginInfo = new HashMap<>();
userLoginInfo.put("loginName", userName);
userLoginInfo.put("loginPwd", userPwd);
return userLoginInfo;
}
}
这里利用银行转账的例子把mysql的自动提交机制改为手动提交机制
/*
重点三行代码
connection.setAutoCommit(false);
connection.commit();
connection.rollback();
*/
public class F {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "123456");
// 自动提交机制改成手动提交机制
connection.setAutoCommit(false);
String sql = "update t_act set balance = ? where aname = ?";
ps = connection.prepareStatement(sql);
ps.setInt(1, 10000);
ps.setInt(2, 111);
int i = ps.executeUpdate();
/*
这里是一个明显的空指针异常,测试出来jdbc中提交一条执行语句接执行一条,从而下面的程序没有执行,反映出来就是少了10000块钱
所以我们需要将这两个语句的执行放到一个事务中,我们将jdbc中的自动提交机制改成手动提交机制
*/
// String s = null;
// s.toString();
ps.setInt(1, 10200);
ps.setInt(2, 222);
i += ps.executeUpdate();
System.out.println(i == 2 ? "转账成功" : "转账失败");
// 当程序走到了这里表示以上程序没有异常,事务结束,手动提交
connection.commit();
} catch (Exception e) {
// 如果有异常,手动回滚
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}