JDBC:
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
// 1.加载驱动
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
/\*Driver driver = new com.ibm.db2.jcc.DB2Driver();
DriverManager.registerDriver(driver);
//这样做,意义不大,因为类在加载的时候就已经创建了DB2Driver对象,并向DriverManager注册\*/
} catch (Exception e) {
e.printStackTrace();
}
String url = "jdbc:db2://10.10.38.138:50000/malltest";
String username = "db2inst1";
String password = "db2inst1";
try {
// 2.创建数据库连接
conn = DriverManager.getConnection(url, username, password);
// 3.获取数据库操作对象
stmt = conn.createStatement();
// 4.操作数据库获取结果集
rs = stmt.executeQuery("select \* from ly.t\_merinf where merid='M0000178'");
// 5.处理结果集
while(rs.next()){
System.out.println(rs.getString("mername"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭结果集
if(rs != null) {
try { rs.close(); } catch (SQLException e) { }
}
// 关闭数据库操作对象
if(stmt != null) {
try { stmt.close(); } catch (SQLException e) { }
}
// 关闭数据库连接
if(conn != null) {
try { conn.close(); } catch (SQLException e) { }
}
}
DBCP:
// 1.创建连接池
DataSource ds = null;
try {
Properties prop = new Properties();
// 通过类路径来加载属性文件
prop.load(DbcpTest.class.getClassLoader().getResourceAsStream("database/dbcp/dbcp.properties"));
// 获取数据源
ds = BasicDataSourceFactory.createDataSource(prop);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 2.获取数据库连接
conn = ds.getConnection();
// 3.创建数据库操作对象
stmt = conn.createStatement();
// 4.操作数据库获取结果集
rs = stmt.executeQuery("select \* from ly.t\_merinf where merid='M0000178'");
// 5.处理结果集
while (rs.next()) {
System.out.println(rs.getString("mername"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭结果集
if(rs != null) {
try { rs.close(); } catch (SQLException e) { }
}
// 关闭数据库操作对象
if(stmt != null) {
try { stmt.close(); } catch (SQLException e) { }
}
// 关闭数据库连接
if(conn != null) {
try { conn.close(); } catch (SQLException e) { }
}
}
配置文件:
driverClassName=com.ibm.db2.jcc.DB2Driver
url=jdbc:db2://10.10.38.138:50000/malltest
username=db2inst1
password=db2inst1
initialSize=3
maxActive=5
maxIdle=3
minIdle=1
maxWait=30000
C3P0:
ComboPooledDataSource cpds = new ComboPooledDataSource();
// 加载数据库驱动
try {
cpds.setDriverClass("com.ibm.db2.jcc.DB2Driver");
} catch (PropertyVetoException e1) {
e1.printStackTrace();
}
// 设置访问数据库的地址、用户名和密码
cpds.setJdbcUrl("jdbc:db2://10.10.38.138:50000/malltest");
cpds.setUser("db2inst1");
cpds.setPassword("db2inst1");
// 设置C3P0的一些配置,不设置则使用默认值
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
cpds.setMaxStatements(180);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据库连接
conn = cpds.getConnection();
// 获取数据库操作对象
stmt = conn.createStatement();
// 操作数据库获取结果集
rs = stmt.executeQuery("select \* from ly.t\_merinf where merid='M0000178'");
// 处理结果集
while (rs.next()) {
System.out.println(rs.getString("mername"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭结果集
if(rs != null) {
try { rs.close(); } catch (SQLException e) { }
}
// 关闭数据库操作对象
if(stmt != null) {
try { stmt.close(); } catch (SQLException e) { }
}
// 关闭数据库连接
if(conn != null) {
try { conn.close(); } catch (SQLException e) { }
}
try {
DataSources.destroy(cpds);
} catch (SQLException e) {
e.printStackTrace();
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章