简体   繁体   English

如何将字符串变量添加到jdbc firebird连接上的SQL查询

[英]How to add a string variable to a SQL query on a jdbc firebird connection

I am trying to query a Firebird database from a Java application. 我试图从Java应用程序查询Firebird数据库。 This query contains 2 integer variables and 1 string. 此查询包含2个整数变量和1个字符串。 The query looks like this: 查询如下所示:

Statement statement = conn.createStatement();

ResultSet rs = statement.executeQuery("select * from table where
  column1 < " + intvariable1 + " and ( 
  column2 > " + intvariable2 +" or column2 = 0) and 
  column3 = '" + stringvariable + "' ";

Rs.next();
syso(rs.getInt(1));

It appears that the ResultSet is empty but when I use a hardcoded string value instead of a variable I get the expected result. 看起来ResultSet是空的,但是当我使用硬编码的字符串值而不是变量时,我得到了预期的结果。

I also tried this with preparedstatements with the same result - it is only working without string variable. 我也尝试使用具有相同结果的preparedstatements语句 - 它只在没有字符串变量的情况下工作。

Any ideas what I am doing wrong? 我有什么想法我做错了吗?

EDIT: Problem solved! 编辑:问题解决了! The Stringvariable came from a BLOB Field and had some empty values within it. Stringvariable来自BLOB字段,其中包含一些空值。 Calling the trim function deleted those. 调用trim函数删除了那些。

I noticed that in the "column3" line you have a space after the opening quote and before the closing quote. 我注意到在“column3”行中,您在开始报价之后和收盘报价之前有一个空格。 Unless you are deliberately trying to match strings that start and end with a space, you won't find results. 除非您故意尝试匹配以空格开头和结尾的字符串,否则您将无法找到结果。 If not, cut out the spaces: 如果没有,切出空格:

column3 = '" + stringvariable + "' ";

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

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