久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Java中怎么利用JDBC和DBCP訪問數(shù)據(jù)庫

151次閱讀
沒有評論

共計 3308 個字符,預(yù)計需要花費 9 分鐘才能閱讀完成。

本篇文章為大家展示了 Java 中怎么利用 JDBC 和 DBCP 訪問數(shù)據(jù)庫,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

JDBC:

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
// 1. 加載驅(qū)動
try {
 Class.forName( com.ibm.db2.jcc.DB2Driver 
 /*Driver driver = new com.ibm.db2.jcc.DB2Driver();
 DriverManager.registerDriver(driver);
 // 這樣做,意義不大,因為類在加載的時候就已經(jīng)創(chuàng)建了 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. 創(chuàng)建數(shù)據(jù)庫連接
 conn = DriverManager.getConnection(url, username, password);
 // 3. 獲取數(shù)據(jù)庫操作對象
 stmt = conn.createStatement();
 // 4. 操作數(shù)據(jù)庫獲取結(jié)果集
 rs = stmt.executeQuery( select * from ly.t_merinf where merid= M0000178 
 // 5. 處理結(jié)果集
 while(rs.next()){System.out.println(rs.getString( mername));
} catch (SQLException e) {e.printStackTrace();
} finally {
 //  關(guān)閉結(jié)果集
 if(rs != null) {try { rs.close(); } catch (SQLException e) { }
 //  關(guān)閉數(shù)據(jù)庫操作對象
 if(stmt != null) {try { stmt.close(); } catch (SQLException e) { }
 //  關(guān)閉數(shù)據(jù)庫連接
 if(conn != null) {try { conn.close(); } catch (SQLException e) { }
}

DBCP:

// 1. 創(chuàng)建連接池
DataSource ds = null;
try {Properties prop = new Properties();
 //  通過類路徑來加載屬性文件
 prop.load(DbcpTest.class.getClassLoader().getResourceAsStream(database/dbcp/dbcp.properties));
 //  獲取數(shù)據(jù)源
 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. 獲取數(shù)據(jù)庫連接
 conn = ds.getConnection();
 // 3. 創(chuàng)建數(shù)據(jù)庫操作對象
 stmt = conn.createStatement();
 // 4. 操作數(shù)據(jù)庫獲取結(jié)果集
 rs = stmt.executeQuery( select * from ly.t_merinf where merid= M0000178 
 // 5. 處理結(jié)果集
 while (rs.next()) {System.out.println(rs.getString( mername));
} catch (SQLException e) {e.printStackTrace();
} finally {
 //  關(guān)閉結(jié)果集
 if(rs != null) {try { rs.close(); } catch (SQLException e) { }
 //  關(guān)閉數(shù)據(jù)庫操作對象
 if(stmt != null) {try { stmt.close(); } catch (SQLException e) { }
 //  關(guān)閉數(shù)據(jù)庫連接
 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();
//  加載數(shù)據(jù)庫驅(qū)動
try {cpds.setDriverClass( com.ibm.db2.jcc.DB2Driver} catch (PropertyVetoException e1) {e1.printStackTrace();
//  設(shè)置訪問數(shù)據(jù)庫的地址、用戶名和密碼
cpds.setJdbcUrl( jdbc:db2://10.10.38.138:50000/malltest 
cpds.setUser( db2inst1 
cpds.setPassword( db2inst1 
//  設(shè)置 C3P0 的一些配置,不設(shè)置則使用默認(rèn)值
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
cpds.setMaxStatements(180);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
 //  創(chuàng)建數(shù)據(jù)庫連接
 conn = cpds.getConnection();
 //  獲取數(shù)據(jù)庫操作對象
 stmt = conn.createStatement();
 //  操作數(shù)據(jù)庫獲取結(jié)果集
 rs = stmt.executeQuery( select * from ly.t_merinf where merid= M0000178 
 //  處理結(jié)果集
 while (rs.next()) {System.out.println(rs.getString( mername));
} catch (SQLException e) {e.printStackTrace();
} finally {
 //  關(guān)閉結(jié)果集
 if(rs != null) {try { rs.close(); } catch (SQLException e) { }
 //  關(guān)閉數(shù)據(jù)庫操作對象
 if(stmt != null) {try { stmt.close(); } catch (SQLException e) { }
 //  關(guān)閉數(shù)據(jù)庫連接
 if(conn != null) {try { conn.close(); } catch (SQLException e) { }
 try {DataSources.destroy(cpds);
 } catch (SQLException e) {e.printStackTrace();
}

上述內(nèi)容就是 Java 中怎么利用 JDBC 和 DBCP 訪問數(shù)據(jù)庫,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道。

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-08-03發(fā)表,共計3308字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 改则县| 佛教| 海安县| 澄迈县| 任丘市| 神农架林区| 赤城县| 博客| 罗山县| 福鼎市| 隆尧县| 甘谷县| 兴业县| 太和县| 达日县| 德保县| 昌邑市| 上虞市| 临泽县| 呼玛县| 大新县| 武威市| 乐亭县| 来安县| 蓬安县| 荥经县| 靖州| 大港区| 饶河县| 清流县| 南开区| 达拉特旗| 金坛市| 方正县| 彩票| 沿河| 兴业县| 阿克| 弋阳县| 循化| 澎湖县|