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());
}
}
}