Java中的Oracle Sql语句

Posted

技术标签:

【中文标题】Java中的Oracle Sql语句【英文标题】:Oracle Sql statement in java 【发布时间】:2015-09-24 02:18:19 【问题描述】:

嘿,我正在尝试在 Oracle sql load.sql 文件中运行 java 程序。运行程序时,我收到错误“ORA-00904:“C”。“STAFFID”:无效标识符”。我认为问题在于我准备 sql 语句并尝试执行它。我试图从两个 sql 表中获取信息:a2_loan 和 a2_customer。在 a2_loan 中,我想要获取 loan_num 和 contract_date,而在表 a2_customer 中,我想要名称和 ird_num。 我的声明目前是:

String sql = "select a.name, ird_num, loan_num, contract_date from a2_loan, customer b, customer c where aname=b.ird_num and b name=c.name";
PreparedStatement stmt = con.prepareStatement(sql);

我认为问题出在此处,但如果在其他地方,请告诉我,我将复制其余代码!

【问题讨论】:

您说表名是a2_customer,但在SQL 语句中您指的只是customer。是这个问题吗? 在您的查询中不应该是 a2_customer 吗? 更正您的 where 子句。 where a.name=b.ird_num and b.name=c.name 您更新的 SQL 不再具有别名为 c 的表,因此任何 c. 都会失败。 SQL 没有"C"."STAFFID",因此您的错误不可能是 this SQL。 您继续编辑 SQL 查询,但它并没有添加到问题文本中。最后编辑(#5)甚至不是有效的语法。我建议你删除这个问题,弄清楚你想问什么,然后发布一个新问题。 以“不清楚你在问什么”结束。 【参考方案1】:

注意:此答案适用于原始问题。

带注释的 SQL 语句:

select a.name           -- 'a' is undefined
     , ird_num          -- note: always qualify (helps others read it)
     , loan_num         --   same
     , contract_date    --   same
  from a2_loan          -- is this support to have alias 'a'?
     , customer b       -- not table `a2_customer`
     , customer c       -- not table `a2_customer`
 where aname=b.ird_num  -- missing period(?), and 'a' is undefined
   and b name=c.name    -- missing period

【讨论】:

以上是关于Java中的Oracle Sql语句的主要内容,如果未能解决你的问题,请参考以下文章

oracle 中的 sql语句查询

oracle sql语句中的替换问题

如何调整oracle中的sql语句输入的最大长度

如何写sql语句去掉oracle返回结果中的空值(NULL)

oracle删除某个表的索引的sql语句

如何用java语言中如何执行sql查询语句