Java中如何返回多结果集

hl_me 2004-10-28 01:26:04
sorry:我还没有写清楚
public myClass exec_select(String sql,Connection conn)
{

int arr[]=null;
ResultSet[] rs=null;
Statement stm=null;
.........请问如何给予 rs 值?
return new myClass(arr,rs);
}
此处不用考虑connection,我的sql语句查询得到三个结果集,myClass是我封装的一个返回类型的类饱含下列二个变量,int[] tag,ResultSet[] rs,和一个构造函数
public void myClass(int[] i,ResultSet[] rst)
{
tag=i;
rs=rst;
}
...全文
524 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2004-10-29
  • 打赏
  • 举报
回复
uping......
vvip 2004-10-29
  • 打赏
  • 举报
回复
guanzhu
xinfeng802 2004-10-29
  • 打赏
  • 举报
回复
哈哈,快说吧,大家学习一下!!!
galewithwing 2004-10-29
  • 打赏
  • 举报
回复
老兄,你是不是在得到一个结果集的时候,从中去出一个数据,再进行查询?
那么,你要用两个不同的statement!因为如果只用一个,那么你的rs1会在产生rs2的时候关闭~!
得不到所有你的数据~!

Statement stmt1=con.get......;
Statement stmt2=con.get......;

ResultSet rs1=null;
ResultSet rs2=null;
rs1=stmt1. (sql1);
while(rs1.next()){
var1=rs1.getString("...");
sql2= "-----------";
rs2=stmt2. (sql2);
while(rs2.next()){
rs2.getString("...");
//.......
}

}
gks_cn 2004-10-29
  • 打赏
  • 举报
回复
你把你要的东西做成一个类。然后返回这个类就可以了。
luckydog903 2004-10-29
  • 打赏
  • 举报
回复
恭喜你啊,分享喜悦
trumplet 2004-10-29
  • 打赏
  • 举报
回复
把分给谁还不你说了算,有权不使,过期做废,哈哈哈哈。
hl_me 2004-10-29
  • 打赏
  • 举报
回复
各位大吓,问题已经解决了,哪位感兴趣可以一起分享!
hl_me 2004-10-28
  • 打赏
  • 举报
回复
自己顶一下先
debug148 2004-10-28
  • 打赏
  • 举报
回复
ding
hl_me 2004-10-28
  • 打赏
  • 举报
回复
上面的代码肯定是返回ResultSet,报错如下:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has bee
n closed.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source
)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.validateClosedState(Unknown Sou
rce)
at com.microsoft.jdbc.base.BaseResultSet.commonFetchInitialize(Unknown S
ource)
at com.microsoft.jdbc.base.BaseResultSet.next(Unknown Source)
at databaseTest.dbTest.main(dbTest.java:99)

请帮忙分析,谢谢!!!!!!!!!
youthy_yy 2004-10-28
  • 打赏
  • 举报
回复
/**
* Moves to this <code>Statement</code> object's next result, returns
* <code>true</code> if it is a <code>ResultSet</code> object, and
* implicitly closes any current <code>ResultSet</code>
* object(s) obtained with the method <code>getResultSet</code>.
*
* <P>There are no more results when the following is true:
* <PRE>
* <code>(!getMoreResults() && (getUpdateCount() == -1)</code>
* </PRE>
*
* @return <code>true</code> if the next result is a <code>ResultSet</code>
* object; <code>false</code> if it is an update count or there are
* no more results
* @exception SQLException if a database access error occurs
* @see #execute
*/
youthy_yy 2004-10-28
  • 打赏
  • 举报
回复
用Statement的getMoreResults()来判断循环得到多个结果集

语法:
boolean getMoreResults() throws SQLException;
hl_me 2004-10-28
  • 打赏
  • 举报
回复
我把源码全部贴出来:
public return_rsArray exec_select(String sql,Connection conn)
{
Statement stm=null;
ResultSet[] rs=null;

int[] arr=null;
try
{
String[] str=sql.split(";");
stm=conn.createStatement();
int leng=str.length;
arr=new int[leng];
rs=new ResultSet[leng];

if(leng!=1)
{
for(int i=0;i<leng;i++)
{
System.out.println ("abstract sql.length():"+leng+"\t"+str[i]);
if(stm.execute(str[i]))
{
System.out.println ("stm.execute(str[]);");
rs[i]=stm.getResultSet();
arr[i]=0;
}else
{
arr[i]=-1;
continue;
}
}
}else
{
if(stm.execute(sql))
{
rs[0]=stm.getResultSet();
arr[0]=0;
}
}
}catch(SQLException sqe)
{
System.out.println("select not succeed!");
}
return_rsArray ret=new return_rsArray(arr,rs);
System.out.println ("execute ok!!!!!!!!!");
return ret;
}

调用代码如下:
public static void main(String args[])
{
Connection conn=null;
Statement stm=null;
ResultSet[] rs=null;
return_rsArray ret=null;

String driver;
String url;
String userName;
String password;
String poolName;

driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
url="jdbc:microsoft:sqlserver://HI:1433;DatabaseName=Northwind";
userName="sa";
password="";
poolName="sqlserver";

connection_para para=connection_para.getInstance();
dbTest test=new dbTest();
/*設置連接參數*/
test.connPar.setDriver(driver);
para.setUrl(url);
para.setUserName(userName);
para.setPassword(password);
para.setConnPoolName(poolName);
para.setMaxConn(10);

try
{
String sql="select * from employees;select * from userTable;select * from userTable";
test.manaPool.createPool();

conn=test.getConnection(poolName);
ret=test.exec_select(sql,conn);
rs=ret.rsTpye;
for(int jj=0;jj<rs.length;jj++)
System.out.println (rs[jj]);
上面这句显示得到三个结果集了
for(int k=0;k<rs.length;k++)
{
while(rs[k].next())
{
ResultSetMetaData rsd=rs[k].getMetaData();
int cloumn=rsd.getColumnCount();
for(int i=1;i<cloumn+1;i++)
{
Object str=rs[k].getObject(rsd.getColumnName(i));
System.out.println(str);
}
}
System.out.println ("%%%%%%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^^^&&&&&&&&&&&&&&&");
}*/

}catch(Exception ex)
{
ex.printStackTrace();
}
}
如果运行则报错提示JDBCObject cosed ,但是改成rs[2](只取一个)没有问题,改成0 或者说1 都会报一样的错误,请帮忙!!!!!!!!
trumplet 2004-10-28
  • 打赏
  • 举报
回复
rs[0] = ......;
rs[1] = ......;
rs[2] = ......;
h_hx 2004-10-28
  • 打赏
  • 举报
回复
真的不明白楼主在说什么
hl_me 2004-10-28
  • 打赏
  • 举报
回复
快来拿分啊!!!!

81,094

社区成员

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

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