标签:localhost oracle public 数据库 数据源
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();
}
}
}
}
标签:localhost oracle public 数据库 数据源
原文地址:http://9882931.blog.51cto.com/9872931/1614986