共計 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è)資訊頻道。
正文完
發(fā)表至: 數(shù)據(jù)庫
2023-08-03