JDBC PreparedStatements中的高级参数化 [英] Advanced parametrization in JDBC PreparedStatements

查看:149
本文介绍了JDBC PreparedStatements中的高级参数化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正有参数化PreparedStatement中的表吗?

Are there anyway to parametrize the table in a PreparedStatement?

select * from ? where id=?

如果不这样做,什么是最好的方法?或者,还有其他方法可以在不失去PreparedStatement优势的情况下进行操作吗?

If not, what is the best approach to do that or, are there any other way to do it without loosing the advantages of a PreparedStatement?

谢谢.

推荐答案

简短的答案是,您不能在准备好的语句中参数化表名.您必须使用字符串连接来构造sql.基本准备好的语句用于列值,而不用于表名.

The short answer is that you cannot parameterize a tablename in a prepared statement. You have to contruct the sql with string concatenation. Basically prepared statements are used for column values and not for the table names.

我能想到的最好的方法是像这样使用string.format:

The best I can think of is to use string.format like this:

String sql = String.format("select * from $1%s",  yourtable);

这篇关于JDBC PreparedStatements中的高级参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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