You should first join the configuration file for connection pooling and database connections:
Database Connection pack: Ojdbc6.jar
Database Connection Pool Package: Commons-pool2-2.2.jar
Commons-dbcp2-2.0.jar
Database connection configuration file: Config.properties
Connect using connection pooling Be sure not to forget to shut it down or it'll be unlucky.
The writing of DBHelper
Import java.io.IOException;
Import Java.io.InputStream;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.Properties;
Import Org.apache.commons.dbcp2.BasicDataSource;
public class DBHelper {
private static Basicdatasource ds = new Basicdatasource ();
static {
InputStream is = Dbhelper.class
. getResourceAsStream ("config.properties");
Properties prop = new properties ();
try {
Prop.load (IS);
Ds.setdriverclassname (Prop.getproperty ("Driver"));
Ds.seturl (prop.getproperty ("url"));
Ds.setusername (Prop.getproperty ("username"));
Ds.setpassword (Prop.getproperty ("password"));
System.out.println (Ds.getdriverclassname () + Ds.geturl ());
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
if (is = null)
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
public static Connection getconnection () throws SQLException {
return Ds.getconnection ();
}
public static void Close (ResultSet rs, PreparedStatement PS, Connection conn) {
try {
if (rs! = null)
Rs.close ();
if (PS! = null)
Ps.close ();
IF (conn! = null)
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
Config.properties File configuration:
Driver=oracle.jdbc.driver.oracledriver
Url=jdbc\:oracle\:thin\:@10.141.42.119\:1521\:orcl
Username=hdid01
Password=hdid01
Database connection Pool Oracle Version "Name=" image_operate_7261294294145390 "alt=" Java database connection Pool Oracle Edition "src=" http ://s3.sinaimg.cn/middle/5ff02a9ft99274e5cac92&690 "width=" 540 "height=" 217 ">
Placement of two files:
Database connection Pool Oracle Edition "alt=" Java database connection pool Oracle Version "src=" http://s9.sinaimg.cn/middle/ 5ff02a9ft99275363f018&690 "width=" 235 "height=" >
So you can use it, or you can config.properties it somewhere else and change it.
InputStream is = Dbhelper.class
. getResourceAsStream ("config.properties");
Find the path to the file to modify it.