package top.qaqaq.P23.statement.crud;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Scanner;
import org.junit.Test;
import top.qaqaq.P12.statement.crud.User;
import top.qaqaq.P14.util.JDBCUtils;
/**
*
* @Description 演示使用PreparedStatement替换Statement,解决SQL注入问题
* @author RichieZhang Email:18358892@qq.com
* @version
* @date 2022年11月26日下午9:29:01
*
*/
public class PreparedStatementTest {
@Test
public void testLogin() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String user = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.next();
//SELECT user,password FROM user_table WHERE user = '1' or ' AND password = '=1 or '1' = '1'
String sql = "SELECT user,password FROM user_table WHERE user = ? AND password = ?";
User returnUser = getInstance(User.class, sql, user, password);
if(returnUser != null) {
System.out.println("登陆成功");
}else {
System.out.println("用户名不存在或密码错误");
}
}
/**
*
* @Description
* @author RichieZhang 针对于不同的表的通用的查询操作,返回表中的一条记录
* @date 2022年11月26日下午9:04:58
* @param <T>
* @param clazz
* @param sql
* @param args
* @return
*/
public <T> T getInstance(Class<T> clazz, String sql, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
// 获取结果集的元数据 : ResultSetMetaData
ResultSetMetaData rsmd = rs.getMetaData();
// 通过ResultSetMetaData获取结果集中的列数
int columnCount = rsmd.getColumnCount();
if (rs.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnvalue = rs.getObject(i + 1);
// 获取每个列的列名
// String columnName = rsmd.getColumnName(i + 1);
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给t对象指定的columnName属性,赋值为columnvalue;通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnvalue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, rs);
}
return null;
}
}
暂无评论