数据库中的DBCP
public class DBCPDemo {
public static void main(String[] args) {
String url = "jdbc:oracle:thin:@localhost:1521:orcl"; //声明连接字符串
String driver = "oracle.jdbc.driver.OracleDriver"; //声明驱动类
String userName = "scott";
String pwd = "tiger";
BasicDataSource bds = new BasicDataSource(); //实例化数据源对象
bds.setUrl(url); //设置属性
bds.setDriverClassName(driver);
bds.setUsername(userName);
bds.setPassword(pwd);
try {
Connection con = bds.getConnection();
String sql = "select * from dept";
PreparedStatement pstm = con.prepareStatement(sql);
ResultSet res = pstm.executeQuery();
while(res.next()){
System.out.print(res.getString("deptno")+"\t");
System.out.print(res.getString("dname")+"\t");
System.out.println(res.getString("loc"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
bds.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。