带PreparedStatement的JDBC错误

带PreparedStatement的JDBC错误

我正在尝试编写一个获取id u(整数)的小函数,并返回距离为我写了这个简单的查询:

   String likesDistance =
        "SELECT uid2 " + //DISTANCE = 1
        "FROM Likes " +
        "WHERE uid1 = ? " +
        "UNION " +
        "SELECT L2.uid2 " + //DISTANCE = 2
        "FROM Likes L1, Likes L2 " +
        "WHERE L1.uid1 = ? and L1.uid2 = L2.uid1 " +
        "UNION "+
        "SELECT L3.uid2 " + //DISTANCE = 3
        "FROM Likes L1, Likes L2 , Likes L3 " +
        "WHERE L1.uid1 = ? and L1.uid2 = L2.uid1 and L2.uid2 = L3.uid1 ";


然后,我执行以下操作:

 PreparedStatement pstmt2 = con.prepareStatement(likesDistance);
        pstmt1.setInt(1, u);
        pstmt1.setInt(2, u);
        System.out.println("2");
        pstmt1.setInt(3, u);
        System.out.println("3");
        pstmt1.setInt(4, u);
        pstmt1.setInt(5, u);


如前所述,其中u是传递给函数的整数。
所有这些代码都在“ try”块内。当我尝试运行它时,它将打印第一个打印输出(“ 2”),而不打印第二个打印输出(“ 3”),并且得到以下异常消息:
列索引超出范围:3,列数:2。

为什么会这样,如何更改它以使其按需工作?

非常感谢。

最佳答案

复制和粘贴?猜猜您想为语句2设置参数。

关于java - 带PreparedStatement的JDBC错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8474629/

10-11 17:39