将表名作为参数放在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查询中使用表名作为参数的安全方法
发布评论