我在我的应用程序这种形式的几种功能:使用CallableStatements中的枚举作为一个白名单针对SQL注入
public long generatedCatId(String name,int age, CallableStatement statement) throws SQLException
{
statement.setString(1,name);
statement.setInt(2,age);
statement.registerOutParameter(3, Types.NUMERIC);
statement.execute();
return statement.getLong(3);
}
我想这样做是有一个包含所有允许CallableStatement
对象为Enum
创建一个白名单,这样我就可以在枚举中的一组值上做一个简单的包含。我遇到的问题是,如果没有Connection
对象,我无法创建CallableStatement
对象。有没有办法解决这个限制?我不想传递连接对象,也不想重复编译Statement
,因为这将作为自动化过程的一部分,每天运行数十亿次。最后我确实不会使用任何ORM工具。
UPDATE
任意语句的例子,我会跑:
CallableStatement statement = conn.prepareCallable("{Call insert_new_cat(?,?,?)}";
SQL: insert into cat(id,name,age)
values(cat_sequence.nextval,name,age)
returning id;
CallableStatement statement = conn.prepareCallable("{Call update_cat(?,?)}";
SQL: update cat
set age = age
where id = id;
第二次更新
似乎有左右我在做什么了一些混乱。程序的流程如下:
进程A调用上面声明的函数,提供编译语句。示例如下:
Connection conn = DriverManager.getConnection("foo");
CallableStatement statement = conn.prepareCall("insert_new_cat");
for(Cat currentCat : CatList)
{
generateCatId(currentCat.name(),currentCat.age(), statement);
}
conn.commit();
请注意以上是一个提交和一个编译或准备语句。我希望该语句成为枚举或其他最终数据结构的一部分,然后我可以在generateCatId
函数中对其进行比较。
你可以举两个或两个以上'允许'的例子吗? – epoch 2012-08-16 13:40:50
为什么不使用查询字符串作为白名单并在使用预处理语句之后?我不认为你可以避免使用连接 – 2012-08-16 13:45:37
@epoch在那里添加了一些任意的语句来显示我在做什么。 – Woot4Moo 2012-08-16 13:48:37