SQL语句不适用于Oracle JDBC

时间:2017-02-16 09:00:05

标签: 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());
}

2 个答案:

答案 0 :(得分:3)

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

也许您以两条连续线合并的方式连接线条(在我的示例中,一条线的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";

答案 1 :(得分:1)

17006: Invalid Column Name如果您尝试从结果集中获取不存在的列,则会引发此问题。

1)选项添加外部选择,然后重试select * from (your_select)

2)尝试使用列名获取数据。 "ID: " + rs.getLong("CustomerID") + ", txID: " + rs.getString("Order_ID") + ", Date: " + rs.getString("Transaction_Date")

我知道有连接属性GET_COLUMN_LABEL_FOR_NAME。如果设置为false。 ResultSet只知道真正的column_name。但我不确定ojdbc是否支持它。