【问题标题】:WHERE clause issueWHERE 子句问题
【发布时间】:2016-04-23 01:01:56
【问题描述】:

给定以下我要构造的 SQL 语句:

SELECT CONCAT(employees.first_name," ", employees.last_name), landlines.number
FROM employees,landlines WHERE employees.id=landlines.emp_id ORDER BY employees.last_name

最初它是使用问号参数创建的,如下所示:

SELECT CONCAT(employees.first_name," ", employees.last_name), landlines.number
FROM employees,landlines WHERE employees.id=? ORDER BY employees.last_name

正如您从 WHERE 条件中看到的那样,我使用固定电话表中的字段引用雇员表中的字段。基本上是引用外键字段的主键字段。很标准的东西。

我的问题是使用 PreparedStatement 类。我有一个使用如下开关盒的方法:

PreparedStatement statement = ...;
...
//wc is a WhereCondition object and getValue() returns an Object which I cast to a particular type
Field.Type value = wc.getKey().getFieldType();
switch (value)
{
  case STRING:
      statement.setString(index, ((String)wc.getValue()));
      index++;
      break;
  case INT:
      if(wc.getValue() instanceof Field)
      {
        Field fld = (Field)wc.getValue();
        statement.setString(index,fld.toString());
      }
      else
        statement.setInt(index, ((Integer)wc.getValue()));
      index++;
      break;
  case FLOAT:
      statement.setFloat(index, ((Float)wc.getValue()));
      index++;
      break;
  case DOUBLE:
      statement.setDouble(index, ((Double)wc.getValue()));
      index++;
      break;
  case LONG:
      statement.setLong(index, ((Long)wc.getValue()));
      index++;
      break;
  case BIGDECIMAL:
      statement.setBigDecimal(index, ((BigDecimal)wc.getValue()));
      index++;
      break;
  case BOOLEAN:
      statement.setBoolean(index, ((Boolean)wc.getValue()));
      index++;
      break;
  case DATE:
      //We don't want to use the setDate(...) method as it expects
      //a java.sql.Date returned which doesn't allow for any time stamp.
      //Let the database perform the conversion from String to Date type.
      statement.setString(index, ((String)wc.getValue()));
      index++;
      break;
  case DBFUNCTION:
      statement.setString(index, ((String)wc.getValue()));
      index++;
      break;
  case IMAGE:
      statement.setString(index, ((String)wc.getValue()));
      index++;
      break;
}

如果您查看案例 INT,我试图通过测试字段类型并因此调用 statement.setString(index,fld.toString() 来避免 ClassCastException。问题是我最终得到了一条 SQL 语句如下所示的 WHERE 子句:

WHERE employees.id = 'landlines.emp_id'

那些讨厌的引号会阻止查询正确执行。有没有办法为字段 employees.id 设置 XXXX 参数,该参数是 INT 类型,以便在不添加引号的情况下输入landlines.emp_id?

【问题讨论】:

  • 您不能将标识符(列名)作为参数传递给 PreparedStatement。您需要在 Java 中动态构建 SQL
  • A PreparedStatement 在服务器上编译 - 因此您必须有完整有效 SQL。跨度>
  • 看起来您根本不需要PreparedStatement,因为您没有将任何变量绑定到查询中;使用第一个查询的普通 Statement 就足够了。

标签: java sql prepared-statement


【解决方案1】:

这将 JOIN 与参数分开。我无法在您的环境中对其进行测试,所以我不确定它是否正确,但它不应该需要引号:

SELECT CONCAT(e.first_name," ", e.last_name), l.number
FROM employees e
JOIN landlines l ON e.id=l.emp_id
WHERE e.id=? ORDER BY e.last_name

【讨论】:

  • 是的,我想我可能会走这条路并按照您的建议更改语法。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多