resultset.next()仅显示第一行的输出

时间:2019-02-28 15:09:15

标签: java mysql resultset

所以,我有这样的东西:

System.out.println("Enter owner's IC no. or plate no. : ");
String update = in.nextLine();

String sql = String.format("SELECT * FROM `vehicle` WHERE ic='%s' OR plate ='%s'",update,update);

ResultSet rs = stmt.executeQuery(sql);

if(rs.next()) {
    System.out.println("RegNo." +"\t\t"+ "Name" + "\t\t" + "IC" +"\t\t" + "Plate No." + "\t" + "Color" + "\t\t" + "Year" + "\t\t" + "Make" + "\t\t" + "Model" +"\t\t"+ "Capacity" + "\t" + "Type" +"\t\t" + "Max Load");
}
else {
    System.out.println("IC and PLate No. not found....");}

while (rs.next()) {
    regno = rs.getInt("regno");
    name = rs.getString("name");
    ic = rs.getString("ic");
    plate = rs.getString("plate");
    color = rs.getString("color");
    year = rs.getInt("year");
    make = rs.getString("make");
    model = rs.getString("model");
    capacity = rs.getDouble("capacity");
    type = rs.getString("type");
    maxload = rs.getDouble("maxload");

    System.out.println(toString());
}   

我想做的是,如果在数据库中找到数据,它将为匹配的输出打印下表。

现在,应该打印出每个输出。但是,它只会打印出第一个。

我认为是以下代码引起的:

 if(rs.next()) {
    System.out.println("RegNo." +"\t\t"+ "Name" + "\t\t" + "IC" +"\t\t" + "Plate No." + "\t" + "Color" + "\t\t" + "Year" + "\t\t" + "Make" + "\t\t" + "Model" +"\t\t"+ "Capacity" + "\t" + "Type" +"\t\t" + "Max Load");
}
else {
    System.out.println("IC and PLate No. not found....");}

3 个答案:

答案 0 :(得分:0)

使用MessageFormat格式化输出,并使用counter确定结果集是否为空,如下所示:

String strFormat = "RegNo. {0}\tName {1}\tIC {2}\tPlate No. {3}\tColor {4}\tYear {5}\tMake {6}\tModel {7}\tCapacity {8}\tType {9}\tMax Load {10}");

int counter = 0;

while (rs.next()) {
    counter++;

    regno = rs.getInt("regno");
    name = rs.getString("name");
    ic = rs.getString("ic");
    plate = rs.getString("plate");
    color = rs.getString("color");
    year = rs.getInt("year");
    make = rs.getString("make");
    model = rs.getString("model");
    capacity = rs.getDouble("capacity");
    type = rs.getString("type");
    maxload = rs.getDouble("maxload");

    System.out.println(MessageFormat.format(strFormat, regno, name, ic, plate, color, year, make, model, capacity, type, maxload));
}

if (counter == 0) {
    System.out.println("IC and PLate No. not found....");
}

答案 1 :(得分:0)

所以我在这里看到两个问题。最大的是:

System.out.println(toString());

这将在当前类上调用.toString()方法,该方法将从您的ResultSet中输出任何数据。至少,并非基于您显示的任何代码。您将从变量ResultSet返回的所有值都存储在变量中,但是这些变量似乎在任何地方都没有被使用。您需要以某种方式将这些变量添加到您的.println()中。

第二个问题是rs.next()将光标向前移动一行。因此,当您这样做时:

if(rs.next()) {

这会导致您跳过第一行。实际上,这很难解决,因为在没有调用ResultSet的情况下无法判断.next()是否为空。我可能会处理的方式是将所有结果拉入列表中的对象,然后根据列表而不是ResultSet本身进行所有打印。

答案 2 :(得分:0)

当使用带Prepared语句的select查询时,我也面临同样的问题。 例如-

String sqlquerySELECTAgentsWithParam = " select * from AGENTS WHERE AGENT_CODE = ( ? ) ";

它返回了一行,因为我使用了IF条件,然后使用了while(rs.next()) 我无法打印单行,因此,我使用了如下所示的do-while循环来打印第一行结果。下面的代码段-希望对您有所帮助!

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(oracelDBUrl, oracleDBUser,oracleDBPwd );

if (conn != null) { System.out.println("Connected to the Oracle DB ");  }
else { System.out.println("Failed to Connect to Oracle DB "); }
PreparedStatement pstmt = conn.prepareStatement(sqlquerySELECTAgentsWithParam);
pstmt.setString(1,"<agent_id>");
ResultSet rs = pstmt.executeQuery();
boolean returnResultrs = rs.next();
System.out.println("Fetched Result is = " +returnResultrs);
if(returnResultrs)
{
    do {
        System.out.println("Inside Do - While Loop");
        System.out.println("AGENT_CODE = " + rs.getString(1)+ "has AGENT_NAME = "  +rs.getString(2));
        }
while(rs.next());

}