请问:怎样取得sql语句的操作行数?

auron01 2004-07-27 09:40:12
import java.sql.*;
/**
*
* @author Administrator
*/
public class Data {
private final String url="jdbc:odbc:xiaohuoguanli";
private final String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection conn;
Statement stmt;
ResultSet rs;
Object data[][];
/** Creates a new instance of Data */
public Data() {
}

protected Object[][] sqlSelect(){
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,"sa","");
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from htxxb order by id");
while (rs.next()){
for (int i=0;i<操作行数;i++){
data[i][0]=rs.getString("id");
data[i][1]=rs.getString("ywy");
}
//System.out.println(rs.getString(1));
}
return data;
}catch(Exception ex){
System.out.println(ex.getMessage());
return null;
}
}
}
请问,应该怎么写?
...全文
180 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
langxiaofeng 2004-07-27
  • 打赏
  • 举报
回复
FOR中给一个数值???/
??
shareanway 2004-07-27
  • 打赏
  • 举报
回复
不清楚意图。
pipigo_wzj 2004-07-27
  • 打赏
  • 举报
回复
楼主是不是想在结果集中指定的要操作行数。
这样可以在FOR中给一个数值
Alexxxxxx 2004-07-27
  • 打赏
  • 举报
回复
while(rs.next())本身已经是个条件循环,干吗还要for啊?
建议楼主去看看JAVA资源网:www.yultao.tk
lovelx 2004-07-27
  • 打赏
  • 举报
回复
楼上说的清楚。
如果想要得到结果集中的行数,可以这样做:

rs=stmt.executeQuery("select * from htxxb order by id");
rs.last();
rs.getRow();
while( rs.previous() )
{
// do something
}
tomcatjava 2004-07-27
  • 打赏
  • 举报
回复
rs=stmt.executeQuery("select * from htxxb order by id");
while (rs.next()){
//for (int i=0;i<操作行数;i++){
data[i][0]=rs.getString("id");
data[i][1]=rs.getString("ywy");
//}
}
这样便可以遍历整个结果集了,rs.next()调用后会自动指向下一个结果的。

superryu 2004-07-27
  • 打赏
  • 举报
回复
rs=stmt.executeQuery("select count(*) as 行数 from htxxb");
rs.next();
int i = rs.getInt("行数");

understand?
auron01 2004-07-27
  • 打赏
  • 举报
回复
我知道怎么做了。
import java.sql.*;
/**
*
* @author Administrator
*/
public class Data {
private final String url="jdbc:odbc:xiaohuoguanli";
private final String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection conn;
Statement stmt;
ResultSet rs;
Object data[][];
/** Creates a new instance of Data */
public Data() {
}

protected Object[][] sqlSelect(String sql){
try{
int x=0;
Class.forName(driver);
conn=DriverManager.getConnection(url,"sa","");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
rs.last();
x=rs.getRow();
//System.out.println(x);
data=new Object[x][2];
rs.first();
//System.out.println(rs.getRow());
for (int i=0;i<x;i++){
data[i][0]=rs.getString("id");
data[i][1]=rs.getString("ywy");
rs.next();
}
//System.out.println(rs.getString(1));
rs.close();
stmt.close();
conn.close();
return data;
}catch(Exception ex){
System.out.println(ex.getMessage());
return null;
}
}
}
这样就可以了.贴上来大家分享
zhouying1573 2004-07-27
  • 打赏
  • 举报
回复
shiyong rownum lai kongzhi

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧