看看ResultSet的javadoc吧A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results. 当ResultSet rs2 = db.execQuery(sql);//每个房间有多少人在线 这句语句执行的时候,前面的rs就被自动关闭了。
会不会是每次执行的时候使用同一个statement
//Mysql类:import java.sql.*;
import java.io.*;
/**
* 处理数据库的连接和访问
* @author
* @version
*/
public class Mysql{ private Connection conn = null;
private Statement stmt = null;
public Mysql(String dbUrl) throws Exception
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection(dbUrl);
stmt = conn.createStatement();
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("Unable to load driver:" + e.getMessage());
}
}
/**
* 返回连接
* @return Connection 连接
*/
public Connection getConnection()
{
return conn;
}
/**
* PreparedStatement
* @return sql 预设SQL语句
*/
public void clearParameters() throws SQLException
{
prepstmt.clearParameters();
}
/**
* 返回状态
* @return Statement 状态
*/
public Statement getStatement()
{
return stmt;
}
/**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException
{
if (stmt != null)
{
return stmt.executeQuery(sql);
}
else
return null;
}
/**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet execQuery(String sql) throws SQLException
{
if (stmt != null)
{
return stmt.executeQuery(sql);
}
else
return null;
}
/**
* 执行SQL语句
* @param sql SQL语句
*/
public void executeUpdate(String sql) throws SQLException
{
if (stmt != null)
stmt.executeUpdate(sql);
}
/**
* 执行SQL语句
* @param sql SQL语句
*/
public void execUpdate(String sql) throws SQLException
{
if (stmt != null)
stmt.executeUpdate(sql);
}
/**
* 关闭连接
*/
public void close() throws Exception
{
if (stmt != null)
{
stmt.close();
stmt = null;
}
if (prepstmt != null)
{
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
}
}
这句语句执行的时候,前面的rs就被自动关闭了。