java - SQL 语句不适用于 Oracle JDBC

标签 java oracle jdbc

我有一个语句可以从 Oracle 中检索上个月的交易记录:

select 
    c.CustomerID as id, 
    c.Order_ID as txID, 
    c.Transaction_Date as date1 
from 
    Members a, Verify_Detail b, Verify_Request c 
where 
    a.CustomerID = b.CustomerID AND 
    b.CustomerID = c.CustomerID AND 
    b.Order_ID = c.Order_ID AND 
    c.Transaction_Date between add_months(trunc(sysdate,'mm'),-1) and last_day(add_months(trunc(sysdate,'mm'),-1)) 
order by 
    c.CustomerID, c.Transaction_Date desc

此语句在 SQL Developer 上运行良好。但是,当我使用 JDBC 和 Prepared 语句尝试获取我的数据时,它始终显示异常 17006: Invalid Column Name, Cause: null

我想知道我的语句有什么问题导致我无法在 JDBC 上执行它?能在Oracle SQL Developer上使用不支持执行吗?

更新: 我使用的代码很简单:

try {
    Context ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup("jdbc/myDBSrc");
    Connection con = ds.getConnection();

    String sql = "select " + 
                 "c.CustomerID as id, c.Order_ID as txID, c.Transaction_Date as date1 " +
                 "from Members a, Verify_Detail b, Verify_Request c " +
                 "where a.CustomerID = b.CustomerID AND b.CustomerID = c.CustomerID AND " +
                 "b.Order_ID = c.Order_ID AND " + 
                 "c.Transaction_Date between add_months(trunc(sysdate,'mm'),-1) and last_day(add_months(trunc(sysdate,'mm'),-1)) " +
                 "order by c.CustomerID, c.Transaction_Date desc";

    PreparedStatement pstmt = con.prepareStatement(sql);
    ResultSet rs = pstmt.executeQuery();

    while(rs.next()){
        out.println("ID: " + rs.getLong("id") + ", txID: " + rs.getString("txID") + ", Date: " + rs.getString("date1"));
    }
}
catch(SQLException e){
    out.println("SQL state: " + e.getSQLState() + ", Code: " + e.getErrorCode() + ", Msg: " + e.getMessage() + ", Cause: " + e.getCause());
}

最佳答案

这只是一个猜测,但对于评论部分来说太大了......

也许您以合并两行连续行的方式连接了这些行(在我的示例中,一行中的 AND 与下一行中的 b.CustomerID 合并) :

PreparedStatement stmt = conn.prepareStatement("select "+
    "c.CustomerID as id, "+
    "c.Order_ID as txID, "+
    "c.Transaction_Date as date1 "+
"from "+
    "Members a, Verify_Detail b, Verify_Request c "+
"where "+
    "a.CustomerID = b.CustomerID AND"  // <=====
    "b.CustomerID = c.CustomerID AND "+
    "b.Order_ID = c.Order_ID AND "+
    "c.Transaction_Date between add_months(trunc(sysdate,'mm'),-1) "+
       "and last_day(add_months(trunc(sysdate,'mm'),-1)) "+
"order by "+
    "c.CustomerID, c.Transaction_Date desc");

编辑:我认为原因要简单得多......这是 Oracle 将所有标识符转换为大写,所以试试这个:

String sql = "select " + 
                 "c.CustomerID as \"id\", c.Order_ID as \"txID\", c.Transaction_Date as \"date1\" " +
                 "from Members a, Verify_Detail b, Verify_Request c " +
                 "where a.CustomerID = b.CustomerID AND b.CustomerID = c.CustomerID AND " +
                 "b.Order_ID = c.Order_ID AND " + 
                 "c.Transaction_Date between add_months(trunc(sysdate,'mm'),-1) and last_day(add_months(trunc(sysdate,'mm'),-1)) " +
                 "order by c.CustomerID, c.Transaction_Date desc";

关于java - SQL 语句不适用于 Oracle JDBC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42269175/

相关文章:

java - Couchbase UPDATE 未更新所有匹配过滤器的文档?

Java:如何使用 synchronized 和 volatile

c++ - Oracle 中触发 SQL 查询的底层机制

sql - 在 Rails 应用程序中集成 Oracle 数据库

java - 无法连接到MySql数据库

java - 使用java在数据库中保存unicode字符

java - 拆分从android发送到php的嵌套数组

java - 如何在没有Flash的情况下在Java Swing类中添加动画?

java - 如何获取一段时间内特定日期的出现情况

java - 从哈希集中的表中获取特定列的数据