SQLException:ORA-01008:并非所有变量绑定 - 使用java查询数据库JDBC的SQL错误

时间:2014-03-29 00:20:32

标签: java sql oracle jdbc

        Statement stmt = con.createStatement();

        String pubBooks = "select title_name " +
                "from publisher, title " +
                "where pub_name = ? " +
                "and publisher.pub_no = title.pub_no " +
                "order by title_name";
        ResultSet rS = stmt.executeQuery(pubBooks);
        stmt.close();
        String pubss = "Irwin";
        PreparedStatement pStmt = 
            con.prepareStatement(pubBooks);
        pStmt.setString(1, pubss);
        pStmt.executeUpdate();

嘿,我正在尝试使用JDBC将我的数据库查询到该发布者生成的书名列表但是我遇到了错误java.sql.SQLException:ORA-01008:并非所有变量都绑定了。我一直在尝试我能想到的一切,但我现在还不太清楚该怎么办。

2 个答案:

答案 0 :(得分:3)

你接近尝试PreparedStatement,除了你刚刚调用了错误的“执行”方法。使用executeQuery() method返回ResultSet

PreparedStatement pStmt = 
con.prepareStatement(pubBooks);
pStmt.setString(1, pubss);
ResultSet rS = pStmt.executeQuery();

答案 1 :(得分:1)

在向查询提供参数时无法使用Statement,请使用PreparedStatement。您可能需要修改您的代码,如下所示:

    String pubBooks = "select title_name " +
            "from publisher, title " +
            "where pub_name = ? " +
            "and publisher.pub_no = title.pub_no " +
            "order by title_name";

    String pubss = "Irwin";

    PreparedStatement pStmt =  con.prepareStatement(pubBooks);
    pStmt.setString(1, pubss);
    ResultSet rS = pStmt.executeQuery();

    //TODO: code to use resultset rS

    rS.close();
    pstmt.close();