首页 > 代码库 > JDBC

JDBC

DriverManager.getLoginTimeout():0
jdbc Connection默认是autoCommit:true
Statement查询超时时间默认是0:无限制
遍历ResultSet时Connection不能关闭

java.sql
Class DriverManager

setLoginTimeout

public static void setLoginTimeout(int seconds)
Sets the maximum time in seconds that a driver will wait while attempting to connect to a database.

 

Parameters:
seconds - the login time limit in seconds; zero means there is no limit
See Also:
getLoginTimeout()



java.sql
Interface Statement
setQueryTimeout

void setQueryTimeout(int seconds)                     throws SQLException
Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds. If the limit is exceeded, an SQLException is thrown. A JDBC driver must apply this limit to the execute, executeQuery and executeUpdate methods. JDBC driver implementations may also apply this limit to ResultSet methods (consult your driver vendor documentation for details).

 

Parameters:
seconds - the new query timeout limit in seconds; zero means there is no limit
Throws:
SQLException - if a database access error occurs, this method is called on a closed Statement or the condition seconds >= 0 is not satisfied
See Also:
getQueryTimeout()

 

 

 

package oracle.maxconnection;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import oracle.Common;public class CloseConnectionAfterGetResultset {    public static void main(String[] args) throws SQLException {        Connection conn=Common.getConnection();        System.out.println("conn.getAutoCommit():"+conn.getAutoCommit());                String sql="select * from dba_tables";        try {            PreparedStatement  ps=conn.prepareStatement(sql);            System.out.println("ps.getQueryTimeout():"+ps.getQueryTimeout());                        ResultSet rs=ps.executeQuery();                        ResultSetMetaData rsm=rs.getMetaData();                        while (rs.next()) {                                for (int i = 1,count=rsm.getColumnCount(); i <=count; i++) {                    System.out.println("Column:"+rsm.getColumnLabel(i)+",Value:"+rs.getString(i));                }                                if (conn.isClosed()==false) {                    conn.close();                    System.out.println("Connection has closed");                }                            }                    } catch (SQLException e) {            e.printStackTrace();        }                            }}

输出:

conn.getAutoCommit():trueps.getQueryTimeout():0Column:OWNER,Value:SYSColumn:TABLE_NAME,Value:ICOL$Column:TABLESPACE_NAME,Value:SYSTEMColumn:CLUSTER_NAME,Value:C_OBJ#Column:IOT_NAME,Value:nullColumn:STATUS,Value:VALIDColumn:PCT_FREE,Value:0关闭Connection后next会报错:java.sql.SQLException: 关闭的连接: next    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)    at oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:180)    at oracle.maxconnection.CloseConnectionAfterGetResultset.main(CloseConnectionAfterGetResultset.java:27)

 

 

http://jingyan.baidu.com/article/fc07f98922615a12ffe519ce.html

 

http://www.admin10000.com/document/1360.html

 

JDBC