首页 > 数据库技术 > 详细

JDBC

时间:2014-11-14 12:00:37      阅读:272      评论:0      收藏:0      [点我收藏+]

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():true
ps.getQueryTimeout():0
Column:OWNER,Value:SYS
Column:TABLE_NAME,Value:ICOL$
Column:TABLESPACE_NAME,Value:SYSTEM
Column:CLUSTER_NAME,Value:C_OBJ#
Column:IOT_NAME,Value:null
Column:STATUS,Value:VALID
Column: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

原文:http://www.cnblogs.com/softidea/p/4096684.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!