Direct—The direct method sends the complete result set in one request to the driver. It is useful for queries that only produce a small amount of data that you fetch completely. You should avoid using direct when executing queries that produce a large amount of data, as the result set is cached completely on the client and constrains memory. In this mode, each statement requires its own connection to the database. This is accomplished by "cloning" connections. Cloned connections use the same connection properties as the original connection; however, because transactions must occur on a single connection, auto commit mode is required. Due to this, JTA is not supported in direct mode. In addition, some operations, such as updating an insensitive result set, are not supported in direct mode because the driver must create a second statement internally. Exceptions generated due to the creation of cloned statements usually return an error message similar to “Cannot start a cloned connection while in manual transaction mode.”上面这一段是JDBC带的说明文档,也正说明了此问题。
解决方法是你把Connection对象作为这个类的一个类变量,在getConn方法中把设置这个变量的值,那么在其它方法中也可以直接用它,就不会有问题了。
request to the driver. It is useful for queries that only produce a
small amount of data that you fetch completely. You should avoid
using direct when executing queries that produce a large amount
of data, as the result set is cached completely on the client and
constrains memory. In this mode, each statement requires its own
connection to the database. This is accomplished by "cloning"
connections. Cloned connections use the same connection
properties as the original connection; however, because
transactions must occur on a single connection, auto commit
mode is required. Due to this, JTA is not supported in direct mode.
In addition, some operations, such as updating an insensitive
result set, are not supported in direct mode because the driver
must create a second statement internally. Exceptions generated
due to the creation of cloned statements usually return an error
message similar to “Cannot start a cloned connection while in
manual transaction mode.”上面这一段是JDBC带的说明文档,也正说明了此问题。
Connection conn = null;
Statement stmt = null;
public void DBconn(){
... //这里连接数据库
conn = DBManager.getConnection();
stmt = conn.createStatement();
}
public ResultSet executeQuery(String sql){
try{
this.conn.setAutoCommit(false);
ResultSet rs = stmt.executeQuery();
this.conn.setAutoCommit(true);
return rs;
}catch(SQLException e){
//handle the exception
}
}
}
this.conn.setAutoCommit(true);这里的this不用显式写出来的。
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B313181
http://support.microsoft.com/default.aspx?scid=kb;EN-US;313220