CallableStatement 數據庫
public static void main(String[] args) { dom
Connection conn = DBConn.connOracle(); url
CallableStatement cs = conn.prepareCall("{call p(?,?,?,?)}"); spa
cs.registerOutParameter(3, Types.INTEGER); 資源
cs.registerOutParameter(4, Types.INTEGER); rem
cs.setInt(1, 20); get
cs.setInt(2, 30); it
cs.setInt(4, 40); io
cs.execute(); ast
System.out.println(cs.getInt(3));
System.out.println(cs.getInt(4));
cs.close();
conn.close();
}
獲取遊標參數
public static void main(String[] args) throws Exception{
Connection conn = DBUtil.getConn();
//PreparedStatement--->Statement
CallableStatement cs = conn.prepareCall("{call p_curosr (?)}");
cs.registerOutParameter(1, OracleTypes.CURSOR);
cs.execute();
ResultSet rs = ((OracleCallableStatement)cs).getCursor(1);
while(rs.next()){
System.out.println(rs.getInt(1)+"====="+rs.getString(2));
}
}
Batch
public static void main(String[] args) {
Connection conn = DBConn.connOracle();
/*
Statement stmt = conn.createStatement();
stmt.addBatch("insert into dept2 values(51,'500','haha')");
stmt.addBatch("insert into dept2 values(52,'500','haha')");
stmt.addBatch("insert into dept2 values(53,'500','haha')");
stmt.executeBatch();
stmt.close();
*/
PreparedStatement ps = conn.prepareStatement("insert into dept2 values(?,?)");
ps.setInt(1, 61);
ps.setString(2, "haha");
ps.addBatch();
ps.setInt(1, 62);
ps.setString(2, "hehe");
ps.addBatch();
ps.executeBatch();
ps.close();
conn.close();
}
讀取數據庫配置文件
public class DBUtil {
private static Properties properties = null;
static{
//InputStream is = DBUtil.class.getResourceAsStream("db.properties");
//InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
//若是位於包下面的資源,則能夠:
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/kingdom/db.properties");
properties = new Properties();
try {
properties.load(is);
Class.forName(properties.getProperty("driver"));
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
最小化鏈接池
public class DBConnPool {
private static List<Connection> pool;
private static final int POOL_MAX_SIZE = 100;
private static final int POOL_MIN_SIZE = 50;
private static String url;
private static String user;
private static String pwd;
static {
InputStream is = DBConnPool.class.getClassLoader().getResourceAsStream("db.properties");
Properties prop = new Properties();
try {
prop.load(is);
Class.forName(prop.getProperty("jdbcDriver"));
url = prop.getProperty("url");
user = prop.getProperty("user");
pwd = prop.getProperty("pwd");
is.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
initPool();
}
/**
* 獲取數據庫鏈接池,若是當前池中有可用鏈接,則將池中最後一個返回(並從池中remove該鏈接,表示
* 正在使用,用完後再放回池中),若是沒,則新建一個返回。
*/
public synchronized static Connection getConnection(){
Connection conn = null;
if (pool==null) {
pool = new ArrayList<Connection>();
}
if (pool.isEmpty()) {
conn = createConnection();
} else {
int lastIndex = pool.size()-1;
conn = pool.get(lastIndex);
pool.remove(lastIndex);
}
return conn;
}
/**
* 將使用完畢的鏈接放回鏈接池中,判斷當前池中的鏈接數是否已經超過最大值,若是超過,則關閉該鏈接,
* 若是不超,則放回池中以備下次使用。
*/
public synchronized static void closeConnection(Connection conn){
if (pool.size()>POOL_MAX_SIZE) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
pool.add(conn);
}
}
/**
* 獲得鏈接
*/
private static Connection createConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url,user,pwd);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 初始化鏈接池
*/
private static void initPool() {
if (pool==null) {
pool = new ArrayList<Connection>();
}
while (pool.size()<POOL_MIN_SIZE) {
pool.add(createConnection());
System.out.println(pool.size());
}
}
}