SDF
Package Com. mengya. connutil;
ImportJava. Io. inputstream;
ImportJava. SQL. connection;
ImportJava. SQL. resultset;
ImportJava. SQL. sqlexception;
ImportJava. SQL. statement;
ImportJava. util. properties;
ImportJavax. SQL. datasource;
ImportOrg. Apache. commons. DBCP. basicdatasourcefactory;
Public Class Sqlconnutil {
Private Static Inputstream instream;
Private Static Properties pro;
Private Static Datasource;
Static {
Instream = sqlconnutil. Class . Getclass (). getresourceasstream ("/dbcpconfig. properties ");
Pro = New Properties ();
Try {
Pro. Load (instream );
Datasource = basicdatasourcefactory. createdatasource (Pro );
} Catch (Exception e ){
E. printstacktrace ();
Throw New Runtimeexception ("initialization error! ");
}
}
Public Static Datasource getdatasource (){
Return Datasource;
}
Public Static Connection getconnection (){
Try {
Return Datasource. getconnection ();
} Catch (Sqlexception e ){
E. printstacktrace ();
Throw New Runtimeexception ("failed to get database connection! ");
}
}
Public Static Void Free (resultset RS, statement STA, connection conn ){
Try {
If (RS! = Null ){
Rs. Close ();
}
} Catch (Exception e ){
E. printstacktrace ();
} Finally {
Try {
If (STA! = Null ){
Sta. Close ();
}
} Catch (Exception e ){
E. printstacktrace ();
} Finally {
If (Conn! = Null ){
Try {
Conn. Close ();
} Catch (Sqlexception e ){
E. printstacktrace ();
}
}
}
}
}
}
# Connection settings
Driverclassname = com. Microsoft. sqlserver. JDBC. sqlserverdriver
Url = JDBC: sqlserver: // localhost: 1433; databasename = MP
Username = sa
Password = mengya
#<! --Initialize connection-->
Initialsize = 10
# Maximum number of connections
Maxactive = 50
#<! --Maximum idle connections-->
Maxidle = 20
#<! --Minimum idle connection-->
Minidle = 5
#<! --Timeout wait time is measured in milliseconds. The unit is 6000 milliseconds/1000 is equal to 60 seconds.-->
Maxwait = 60000
# The format of the connection property that is attached to the JDBC driver when establishing a connection must be as follows: [property name = property;]
# Note: The attributes "user" and "password" are passed explicitly, so they are not included here.
Connectionproperties = useunicode = true; characterencoding = GBK
# Specify the auto-commit status of the connection created by the connection pool.
Defaultautocommit = true
# Driver default specifies the read-only status of the connection created by the connection pool.
# If this value is not set, the "setreadonly" method will not be called. (Some drivers do not support read-only mode, such as Informix)
Defaultreadonly =
# Driver default specifies the transaction level (transactionisolation) of the connection created by the connection pool ).
# The available value is one of the following: (for details, see javadoc .) None, read_uncommitted, read_committed, repeatable_read, serializable
Defaulttransactionisolation = read_uncommitted
SDF