我正在尝试在特定数量的查询后得到结果,即
String sql="Select from mytable where sell_id =
'"+purchase_id+"'";
java.sql.Statement stmt=conn.createStatement();
java.sql.ResultSet res=stmt.executeQuery(sql);
while(res > 4){ //this line is where i still couldn't figured out
String quantity = res.getString("quantity");
String item = res.getString("item_name");
String price = res.getString("price");
}
但显示错误“二进制运算符'>'的错误操作数类型”
| quantity | item | price |
| 20 | soda | 5$ |
| 10 | noodle | 7$ |
| 5 | water | 3$ |
| 15 | gum | 4$ |
| 14 | tissue | 2$ |
| 2 | snack | 6$ |
结果应该是这样
| 14 | tissue | 2$ |
| 2 | snack | 6$ |
任何建议都会被接受
答案 0 :(得分:1)
您的变量res
不是数字。您实际上是在将对象与数字文字进行比较,因此会出现错误。如果您想进入结果集,可以执行以下操作:
while(res.next()){
// do something here with your data
}
答案 1 :(得分:1)
您可以使用计数器来检查情况,如下所示:
int counter=1;
while(res.next()){
if(counter>4){
String quantity = res.getString("quantity");
String item = res.getString("item_name");
String price = res.getString("price");
}
counter++;
}
答案 2 :(得分:1)
尝试这样的事情:
while(res.next()){ //anything you can think of!! }
答案 3 :(得分:1)
要跳到ResultSet的第5行,可以使用:
res.absolute(5);
由于您在定位之后并获取数据之前正在调用.next(),因此您的代码将像这样工作:
String sql="Select from mytable where sell_id =
'"+purchase_id+"'";
java.sql.Statement stmt=conn.createStatement();
java.sql.ResultSet res=stmt.executeQuery(sql);
res.absolute(4); // will get your cursor to the 4th row
while(res.next()){ // will get you to the 5th row and continue to the end
String quantity = res.getString("quantity");
String item = res.getString("item_name");
String price = res.getString("price");
}
第二种选择是在您的select语句中使用Limit子句:
String sql="Select from mytable where sell_id = '"+purchase_id+"'" + " limit 4, 9999999999";
那么您将不需要res.absolute(n),因为第一个res.next()会将您直接发送到第五行。我在这里假设您的表少于9999999999行。如果数量更多,则使用更大的数量。
以下是absolute(n)的API文档:
https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html
boolean absolute(int row)
throws SQLException
将光标移动到此ResultSet对象中的给定行号。
如果行号为正,则光标将相对于结果集的开头移至给定的行号。第一行是第1行,第二行是第2行,依此类推。
如果给定的行号为负,则光标将移动到相对于结果集末尾的绝对行位置。例如,调用方法absolute(-1)将光标定位在最后一行;调用方法absolute(-2)将光标移动到倒数第二行,依此类推。
如果指定的行号为零,则光标将移动到第一行之前。
尝试将光标定位在结果集中第一行/最后一行之外的位置会使光标停留在第一行之前或最后一行之后。
注意:调用absolute(1)与调用first()相同。调用absolute(-1)与调用last()相同。
参数: row-光标应移动到的行号。零值表示光标将位于第一行之前;正数表示从结果集开始算起的行号;负数表示从结果集的末尾开始计数的行号 返回值: 如果光标移动到此ResultSet对象中的某个位置,则为true;否则为false。如果光标在第一行之前或最后一行之后,则为false
投掷:
SQLException-如果发生数据库访问错误;在封闭的结果集上调用此方法,或者结果集类型为TYPE_FORWARD_ONLY
SQLFeatureNotSupportedException-如果JDBC驱动程序不支持此方法
答案 4 :(得分:0)
您需要的是--module-path
。
ROW_NUMBER OVER ()