java操作sql server数据中,关于PreparedStatement数据注入问题

本文介绍了一种在Java中使用PreparedStatement处理SQL参数的方法,通过创建特定的类模型和方法来支持不同类型的参数输入,提高了代码的复用性和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

在Java操作SQL Server数据库时,会使用PreparedStatement数据注入方式避免“万能密码”的产生以及提高代码执行效率。例如经常使用如下方式:

requery("select * from stu where name = ? and address = ?", new String[]{ "liu", "wo"});

或 requery("select * from stu where name = ? ", new String[]{ "liu"});

//这样实现requery函数的复用,减少代码量。

public void requery(String s,String[] data){

try{
Class.forName(className);
ct=DriverManager.getConnection(url,name,passWord);
ps=ct.prepareStatement(s);

for(int i=0; i <data.length; i++){
ps.setString(i+1,data[i]);
}
rs=ps.executeQuery();
while(rs.next()){
temp=new Vector();
temp.add(rs.getString(1));
temp.add(rs.getString(2));
rowData.add(temp);
}

}catch(Exception e){
e.printStackTrace();
}finally{
//关闭资源
try {
if(rs!=null)
rs.close();
if(ps!=null)
ps.close();
if(ct!=null)
ct.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

//但接下来问题来了,若sql语句里的问号所需要填充的数据包括String 、int等各种类型,上述的方法就不能使用了,那怎样才能继续代码复用的这种思想呢?

经过笔者研究,提出如下的解决思路:

1、首先建立一个类模型,成员变量为你要访问的数据库表所对应的各种类型字段,此外加上一个int类型的数组成员变量,长度与前面成员变量个数一直,这个数组用来存放

前面对应的每一个成员变量的位置顺序。如下图



2、A类中添加一个根据位置取A的成员变量的方法

public class User {
public final static int SIZE = 2;

private String username;
private int password; 
private int[] flag = new int[SIZE];//对应上面的两个成员

public Object getMenber(int num){
Object o = null;
for(int i = 0; i < SIZE; i++){
if(num == flag[i]){
switch (i) {
case 0: //用户名
o = username;
break;
case 1: //密码
o = password;
break;
}
}
}
return o;
}

public void setFlag(int[] flag) {
this.flag = flag;
}

public void setUsername(String username) {
this.username = username;
}

public void setPassword(int password) {
this.password = password;
}

}

3、改写requery方法

public void requery(String s,User data, int count){
//查询
columnNames=new Vector();
rowData=new Vector();
//设置列名
columnNames.add("用户名");
columnNames.add("密码");
try{
Class.forName(className);
ct=DriverManager.getConnection(url,name,passWord);
ps=ct.prepareStatement(s);

for(int i=1; i <= count; i++){
ps.setObject(i, data.getMenber(i));

}
rs=ps.executeQuery();
while(rs.next()){
temp=new Vector();
temp.add(rs.getString(1));
temp.add(rs.getString(2));
rowData.add(temp);
}

}catch(Exception e){
e.printStackTrace();
}finally{
//关闭资源
try {
if(rs!=null)
rs.close();
if(ps!=null)
ps.close();
if(ct!=null)
ct.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

4、最后实际应用

User data = new User();

data.setUsername("liu");

data.setPassword(123);

data.setFlag(new int[]{2, 1});
int count = 2; //两个问号
st=new stuMode();
st.requery("select * from stu where password = ? and username = ?",data, count);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值