1.导入jar包
2.包结构,创建properties文件
dbcp.properties编辑
oracle.driver=oracle.jdbc.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:orcl
oracle.user=scott
oracle.password=tiger
#初始化连接
dataSource.inittialSize=10
#最大空闲连接
dataSource.maxIdle=20
#最小空闲连接
dataSource.minIdle=5
#最大连接数量
dataSource.maxTotal=50
#超时等待时间单位毫秒
dataSource.maxWaitMillis=1000
3.连接类ConnectionSource
package com.jdbc.oracle;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.dbcp2.BasicDataSource;
public class ConnectionSource {
private static BasicDataSource dataSource=null;
public ConnectionSource() {
}
public static void init() {
Properties properties=new Properties();
try {
properties.load(ConnectionSource.class.getClassLoader().getResourceAsStream(
"com/jdbc/properties/dbcp.properties"));
} catch (IOException e) {
e.printStackTrace();
}
String driver=properties.getProperty("oracle.driver");
String url=properties.getProperty("oracle.url");
String user=properties.getProperty("oracle.user");
String password=properties.getProperty("oracle.password");
String initialSize=properties.getProperty("dataSource.initialSize");
String minIdle=properties.getProperty("dataSource.minIdle");
String maxIdle=properties.getProperty("dataSource.maxIdle");
String maxWaitMillis=properties.getProperty("dataSource.maxWait");
String maxTotal=properties.getProperty("dataSource.maxActive");
dataSource = new BasicDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
//初始化连接数
if(initialSize != null)
dataSource.setInitialSize(Integer.parseInt(initialSize));
if(minIdle != null)
dataSource.setMinIdle(Integer.parseInt(minIdle));
if(maxIdle != null)
dataSource.setMaxIdle(Integer.parseInt(maxIdle));
if(maxWaitMillis != null)
dataSource.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
if(maxTotal != null)
dataSource.setMaxTotal(Integer.parseInt(maxTotal));
}
public static synchronized Connection getConnection() throws SQLException {
if(dataSource == null) {
init();
}
Connection conn=null;
if(dataSource != null) {
conn=dataSource.getConnection();
}
return conn;
}
}
4.查询数据库
package com.jdbc.oracle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class EmpDAOOracle {
public void selectEmp() {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String sql="select empno,ename,hiredate,sal,comm from emp";
try {
conn=ConnectionSource.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getInt("empno")+","+rs.getString("ename")+","+rs.getDate("hiredate")
+","+rs.getDouble("sal")+","+rs.getDouble("comm"));
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
if(rs!=null) {
rs.close();
}
if(stmt!=null) {
stmt.close();
}
} catch (Exception e2) {
System.out.println("关闭异常");
}
OracleUtil.closeConnection(conn);
}
}
public static void main(String[] args) {
EmpDAOOracle empDAOOracle=new EmpDAOOracle();
empDAOOracle.selectEmp();
}
}
输出结果
7369,SMITH,1980-12-17,800.0,0.0
7499,ALLEN,1981-02-20,1600.0,300.0
7521,WARD,1981-02-22,1250.0,500.0
7566,JONES,1981-04-02,2975.0,0.0
7654,MARTIN,1981-09-28,1250.0,1400.0
7698,BLAKE,1981-05-01,2850.0,0.0
7782,CLARK,1981-06-09,2450.0,0.0
7788,SCOTT,1987-04-19,3000.0,0.0
7839,KING,1981-11-17,5000.0,0.0
7844,TURNER,1981-09-08,1500.0,0.0
7876,ADAMS,1987-05-23,1100.0,0.0
7900,JAMES,1981-12-03,950.0,0.0
7902,FORD,1981-12-03,3000.0,0.0
7934,MILLER,1982-01-23,1300.0,0.0