本文介绍了结果集开始前的 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个表中获取 personID 并在第二个表中使用它,然后从第二个表,但是我收到错误 列索引超出范围,3 .订单表由 3 列组成.

I am trying to get the personID from one table and use it in the second table, and the print each column value fromthe second table, however I am getting an the error column index out of range, 3 < 1. The order table consist of 3 columns.

public void getPersonsOrders(String firstName){
    Connection con = connect();
    try{
        Statement s = con.createStatement();
        s.executeUpdate("use stl;");
        ResultSet rs1 = s.executeQuery("select personID from person where first_name = " +"'"+firstName+"'"+";");       //get persons ID no.
        rs1.next();
        ResultSet rs2 = s.executeQuery("select * from orderr where personID = "+rs1.getInt(3)+";");             //use ID no. to
        for(int i = 1; i < 4; i++){                                                                             //retrive order
            System.out.println(rs2.getInt(i));
        }
    }
    catch(SQLException e){
        System.out.println("3" +e.getMessage());
    }
}

推荐答案

rs1 中只有一列返回,因此你应该使用 rs1.getInt(1).

There is only one column returned in rs1, therefore you should use rs1.getInt(1).

另一方面,这应该可以使用连接轻松解决:

On the other hand, this should easily be solved using a join:

SELECT o.* FROM order o
LEFT JOIN person p
ON p.personID = o.personID

这篇关于结果集开始前的 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:09