将列名称作为PreparedStatement的输入参数 [英] Having a Column name as Input Parameter of a PreparedStatement

查看:158
本文介绍了将列名称作为PreparedStatement的输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里使用了搜索(以及其他论坛),但没有找到我正在尝试做的答案。



我知道它可以很容易地以其他方式完成,这只是一个小沙盒框架,我正在为大学课程编写代码......在真实的环境中,我只需要Spring,Hibernate等。



所以我所做的就是用POJO编写一个小的通用数据访问层,使用通用方法检索,检查或插入数据到数据库(Oracle)。其中大部分是通过PreparedStatements完成的。



只要我没有连接,这是有效的...是否可以将列作为参数放入? / p>

示例:

 表A具有属性X +其他
表B具有属性Y +其他

带有查询的PreparedStatement SELECT * FROM A ,B在哪里A。X=?



然后填写B。Y作为参数.. 。



数据库不会抛出错误或异常,但执行语句后返回 ResultSet 空。这是不可能的,还是我只是错过了一些转义?



我正在使用 PreparedStatement.setString(int index,String value )填写参数...缺乏想法,我可以使用其他setX方法...



再次,在一个真实的我永远不会自己编写代码,而是使用类似Spring或Hibernate的东西,而不是重新发明轮子,但我认为这是一个有趣的练习,可以自己编写这样一个通用的小数据访问层。

解决方案

不,JDBC不允许这样做。只能设置列值。如果要对sql语句进行动态更改,则必须在创建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.

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?

Example:

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

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

And then fill in "B"."Y" as the parameter...

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?

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...

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.

解决方案

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的输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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