在JDBC查询中使用表名作为参数的安全方法 [英] Safe way to use table name as parameter in JDBC query

查看:1126
本文介绍了在JDBC查询中使用表名作为参数的安全方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将表名作为参数放在SQL查询中的安全方法是什么?您不能使用 PreparedStatement 将表格名称作为参数。连接字符串以使用动态表名使用 Statement 是可能的,但是不推荐使用SQL注入的风险。这样做最好的方法是什么?

What is the safe way how to put table name as parameter into SQL query? You cannot put table name as parameter using PreparedStatement. Concatenating string to execute query with dynamic table name using Statement is possible, however it is not recommended because of risk of SQL injection. What is the best approach to do this?

推荐答案

我会尝试解决设计问题,所以你不必动态设置表名。如果这是不可能的,我会去一个设计,在那里您管理一个可用的表和用户列表,用户从其中选择一个BY ID,以便您可以从所选的ID中检索真实的表名,并将其替换为表名占位符,避免在表名中替换sql注入的任何机会。

I would try to solve the design problem, so you don't have to set the table name dynamically. If this is not possible, I would go for a design where you manage a list of available tables and users pick one from there, BY ID, so you can retrieve the real table name from the chosen id and replace the table name placeholder with it, avoiding any chance of sql injection in the table name replacement.

这篇关于在JDBC查询中使用表名作为参数的安全方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆