一、DBUtil工具类的创建
在之前的博客中,为了提高代码的复用性,将查询和增删改的实现过程分别封装到了两个方法中。但是以后我们想要使用该功能时,还是需要先把相应的方法写好才能调用,这样依然很不方便。于是,根据Java面向对象的思想,定义一个DBUtil工具类,将查询和增删改的方法封装到工具类中,以后如果想要使用查询和增删改的功能,就可以直接通过DBUtil这个工具类来调用,非常方便。
//创建一个接口以实现查询功能
interface IRowMapper{
void rowMapper(ResultSet res);
}
public class DBUtil {
//因为加载驱动的代码只需要执行一次即可,没必要重复执行,所以封装到静态代码块中
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//将建立连接的代码抽取出来,提高代码的复用性
private static Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "0314");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//实现查询功能
public static void select(String sql, IRowMapper rowMapper) {
Connection connection = null;
Statement statement = null;
ResultSet result = null;
try {
connection = getConnection();
statement = connection.createStatement();
result = statement.executeQuery(sql);
rowMapper.rowMapper(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(result, statement, connection);
}
}
//实现增删改功能
public static boolean update(String sql) {
Connection connection = null;
Statement statement = null;
try {
connection = getConnection();
statement = connection.createStatement();
return statement.executeUpdate(sql) > 0;
} catch (Exception e) {
e.printStackTrace();
}finally {
close(statement, connection);
}
return false;
}
//将关闭资源代码抽取出来,提高代码的复用性
private static void close(Statement statement, Connection connection) {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//close方法的重载
private static void close(ResultSet result, Statement statement, Connection connection) {
try {
if (result != null) {
result.close();
}
result = null;
} catch (SQLException e) {
e.printStackTrace();
}
close(statement, connection);
}
}
二、DBUtil工具类的使用
1、使用DBUtl工具类实现查询功能
public class Select {
public static void main(String[] args){
String sql = "select * from user_info";
class RowMapper implements IRowMapper{
@Override
public void rowMapper(ResultSet res) {
try {
while (res.next()) {
String student_id = res.getString("id");
String userName = res.getString("user_name");
String password = res.getString("password");
System.out.println(student_id+" ,"+userName+" ,"+password);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
IRowMapper rowMapper = new RowMapper();
DBUtil.select(sql, rowMapper);
}
}
下面是运行结果截图
2、使用DBUtil工具类实现删除功能
public class Update {
public static void main(String[] args) {
String sql = "delete from user_info";
if (DBUtil.update(sql)) {
System.out.println("YSE");
}else {
System.out.println("NO");
}
}
}
下面是运行结果截图
三、DBUtil工具类的改进(防止SQL注入)
虽然DBUtil工具类已经创建好了,但是仍然存在一个问题,就是无法防止SQL注入。
1、什么是SQL注入
SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。具体来说,它是利用现有应用程序,将(恶意的)SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句.。下面将通过模拟一个简单的登录系统来说明什么是SQL注入。输入账号和密码,如果和数据库中存储的一致则登录成功,否则登陆失败。
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String userName = scanner.nextLine();
System.out.println("请输入密码");
String password = scanner.nextLine();
String sql = "select * from user_info where user_name = '"+userName+"' and password = '"+password+"'";
IRowMapper rowMapper = new IRowMapper() {
@Override
public void rowMapper(ResultSet resultSet) {
try {
while(resultSet.next()) {
String name = resultSet.getString("user_name");
System.out.println(name + "欢迎您");
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("您输入的账号或者密码错误");
}
};
DBUtil.select(sql, rowMapper);
}
}
下面是运行结果截图:
输入正确的用户名和密码
输入错误的用户名和密码
SQL注入
2、改进DBUtil工具类(防止SQL注入)
使用PreparedStatement代替Statement,下面是改进后的查询和增删改的方法:
//改进后的查询方法(重载)
public static void select(String sql, IRowMapper rowMapper, Object ... params) {//由于传入的参数不确定,所以这里使用了动态参数
Connection connection = null;
PreparedStatement pStatement = null;
ResultSet result = null;
try {
connection = getConnection();
pStatement = connection.prepareStatement(sql);
for (int i = 1; i <= params.length; i++) {
pStatement.setObject(i, params[i-1]);
}
result = pStatement.executeQuery();
rowMapper.rowMapper(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(result, pStatement, connection);
}
}
//改进后的增删改方法(重载)
public static boolean update(String sql,Object ... params) {//由于传入的参数不确定,所以这里使用了动态参数
Connection connection = null;
PreparedStatement pStatement = null;
try {
connection = getConnection();
pStatement = connection.prepareStatement(sql);
for (int i = 1; i <= params.length; i++) {
pStatement.setObject(i, params[i-1]);
}
return pStatement.executeUpdate() > 0;
} catch (Exception e) {
e.printStackTrace();
}finally {
close(pStatement, connection);
}
return false;
}
改进后的登录系统:
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String userName = scanner.nextLine();
System.out.println("请输入密码");
String password = scanner.nextLine();
// String sql = "select * from user_info where user_name = '"+userName+"' and password = '"+password+"'";
String sql = "select * from user_info where user_name = ? and password = ?";
IRowMapper rowMapper = new IRowMapper() {
@Override
public void rowMapper(ResultSet resultSet) {
try {
while(resultSet.next()) {
String name = resultSet.getString("user_name");
System.out.println(name + "欢迎您");
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("您输入的账号或者密码错误");
}
};
DBUtil.select(sql, rowMapper,userName,password);
}
}
下面是SQL注入运行结果截图:
SQL注入问题得到了解决。