简体   繁体   English

将列名作为PreparedStatement的输入参数

[英]Having a Column name as Input Parameter of a PreparedStatement

I already used the search here (and other forums as well) but haven't found an answer exacty to what I'm trying to do. 我已经在这里(以及其他论坛)使用过搜索,但是没有找到与我要执行的操作完全相同的答案。

I know that it can easily be done in some other way, and this is just a small sandbox-framework I'm coding for a University course... in a real environment I'd just take Spring, Hibernate etc. 我知道可以很容易地以其他方式完成它,这只是我为大学课程编写的一个小沙盒框架...在真实的环境中,我只会使用Spring,Hibernate等。

So what I did was coding myself a small generic Data Access Layer with POJOs, working with generic methods to retrieve, check or insert data to the database (Oracle). 因此,我所做的就是使用POJO为自己编写一个小型的通用数据访问层,并使用通用方法来检索,检查数据或将数据插入数据库(Oracle)。 Most of this is done through PreparedStatements. 大多数操作是通过PreparedStatements完成的。

This is working as long as I don't have joins... is it possible to put in a Column as parameter? 只要我没有联接,它就可以工作...是否可以将Column作为参数放置?

Example: 例:

Table A has Attribute X + others
Table B has Attribute Y + others

PreparedStatement with query SELECT * FROM A,B WHERE "A"."X" = ? 带查询SELECT * FROM A,B WHERE "A"."X" = ? PreparedStatement

And then fill in "B"."Y" as the parameter... 然后填写“ B”。“ Y”作为参数...

The database doesn't throw me an error or exception, but the ResultSet returned after executing the statement is empty. 数据库不会抛出错误或异常,但是执行该语句后返回的ResultSet为空。 Is it just not possible to do, or am I just missing some escaping? 只是不可能做,还是我只是想逃脱一些?

I'm using PreparedStatement.setString(int index, String value) to fill in the parameter... in lack of ideas which other setX method I could use... 我正在使用PreparedStatement.setString(int index, String value)来填充参数...缺乏想法,我可以使用其他setX方法...

Again, in a real project I'd never code that myself, but rather use something like Spring or Hibernate and not re-invent the wheel, but I see it as an interesting exercise to code such a generic small data access layer myself. 再说一次,在一个真实的项目中,我永远不会自己编写代码,而是使用诸如Spring或Hibernate之类的东西,而不是重新发明轮子,但我认为这是我自己编写这样的通用小数据访问层的有趣练习。

No, JDBC does not allow this. 不,JDBC不允许这样做。 Only column values can be set. 只能设置列值。 If you want to make dynamic changes to the sql statement you will have to do it before you create the PreparedStatement. 如果要动态更改sql语句,则必须在创建PreparedStatement之前进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM