问题描述
我已经在此处(以及其他论坛)使用过搜索,但没有找到与我正在尝试做的事情完全相符的答案.
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.
我知道它可以通过其他方式轻松完成,这只是我正在为大学课程编码的一个小型沙箱框架……在真实环境中,我只需要使用 Spring、Hibernate 等.
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.
所以我所做的是使用 POJO 为自己编写一个小的通用数据访问层,使用通用方法来检索、检查或插入数据到数据库 (Oracle).其中大部分是通过 PreparedStatements 完成的.
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). Most of this is done through PreparedStatements.
只要我没有连接就可以工作......是否可以将列作为参数放入?
This is working as long as I don't have joins... is it possible to put in a Column as parameter?
示例:
Table A has Attribute X + others
Table B has Attribute Y + others
PreparedStatement with query SELECT * FROM A,B WHERE "A"."X" = ?
PreparedStatement with query SELECT * FROM A,B WHERE "A"."X" = ?
然后填写B".Y"作为参数...
And then fill in "B"."Y" as the parameter...
数据库没有给我抛出错误或异常,但是执行语句后返回的ResultSet
是空的.是不可能做到,还是我只是错过了一些转义?
The database doesn't throw me an error or exception, but the ResultSet
returned after executing the statement is empty. Is it just not possible to do, or am I just missing some escaping?
我正在使用 PreparedStatement.setString(int index, String value)
来填充参数......我不知道我可以使用哪些其他 setX 方法......
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...
再说一次,在一个真实的项目中,我永远不会自己编写代码,而是使用 Spring 或 Hibernate 之类的东西,而不是重新发明轮子,但我认为编写这样一个通用的小型数据访问层是一个有趣的练习我自己.
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.
推荐答案
不,JDBC 不允许这样做.只能设置列值.如果要对 sql 语句进行动态更改,则必须在创建 PreparedStatement 之前进行.
No, JDBC does not allow this. 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.
这篇关于将列名作为 PreparedStatement 的输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!