java連接orcale數(shù)據(jù)庫示例分享
database.properties
jdbc.driver_class=oracle.jdbc.driver.OracleDriver
jdbc.connection.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.connection.username=scott
jdbc.connection.password=tiger
ConfigManager.java
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
//讀取配置文件(屬性文件)的工具類
public class ConfigManager {
private static ConfigManager configManager;
// properties.load(inputStream);讀取屬性文件
private static Properties properties;
// 在構(gòu)造工具類時,進行配置文件的讀取
private ConfigManager() {
String configFile = "database.properties";
properties = new Properties();
InputStream is = ConfigManager.class.getClassLoader()
.getResourceAsStream(configFile);
try {
// 讀取配置文件
properties.load(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 通過單例模式設置實例化個數(shù)
public static ConfigManager getInstance() {
if (configManager == null) {
configManager = new ConfigManager();
}
return configManager;
}
// 通過key獲取對應的value
public String getString(String key) {
return properties.getProperty(key);
}
}
BaseDao.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.book.util.ConfigManager;
//基類:數(shù)據(jù)庫操作通用類
public class BaseDao {
protected Connection conn;
protected PreparedStatement ps;
protected Statement stmt;
protected ResultSet rs;
// 獲取數(shù)據(jù)庫連接
public boolean getConnection() {
// // 讀取配置信息
String driver = ConfigManager.getInstance().getString(
"jdbc.driver_class");
String url = ConfigManager.getInstance().getString(
"jdbc.connection.url");
String username = ConfigManager.getInstance().getString(
"jdbc.connection.username");
String password = ConfigManager.getInstance().getString(
"jdbc.connection.password");
// 加載JDBC驅(qū)動
try {
Class.forName(driver);
// 與數(shù)據(jù)庫建立連接
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
return false;
} catch (SQLException e) {
return false;
}
return true;
}
// 增刪改的通用方法
public int executeUpdate(String sql, Object[] params) {
int updateRows = 0;
getConnection();
try {
ps = conn.prepareStatement(sql);
// 填充占位符
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
System.out.println(i + 1 + "---" + params[i]);
}
System.out.println(sql);
updateRows = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return updateRows;
}
// 查詢
public ResultSet executeSQL(String sql, Object[] params) {
getConnection();
try {
ps = conn.prepareStatement(sql);
// 填充占位符
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
// 關閉資源
public void closeAll(){
try {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- Java使用JDBC連接數(shù)據(jù)庫的實現(xiàn)方法
- Java實現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法
- java常用工具類之數(shù)據(jù)庫連接類(可以連接多種數(shù)據(jù)庫)
- java連接數(shù)據(jù)庫增、刪、改、查工具類
- java連接mysql數(shù)據(jù)庫學習示例
- java使用jdbc連接數(shù)據(jù)庫工具類和jdbc連接mysql數(shù)據(jù)示例
- java連接MySQl數(shù)據(jù)庫實例代碼
- Java Web項目中連接Access數(shù)據(jù)庫的配置方法
- Java連接各種數(shù)據(jù)庫的方法
相關文章
Java使用redisson實現(xiàn)分布式鎖的示例詳解
這篇文章主要為大家詳細介紹了在Java項目中使用redisson實現(xiàn)分布式鎖,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考一下2023-07-07
JAVA中實現(xiàn)原生的 socket 通信機制原理
本篇文章主要介紹了JAVA中實現(xiàn)原生的 socket 通信機制原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Scala數(shù)據(jù)庫連接池的簡單實現(xiàn)
本文主要介紹了Scala數(shù)據(jù)庫連接池的簡單實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
SpringBoot實現(xiàn)elasticsearch 查詢操作(RestHighLevelClient 
這篇文章主要給大家介紹了SpringBoot如何實現(xiàn)elasticsearch 查詢操作,文中有詳細的代碼示例和操作流程,具有一定的參考價值,需要的朋友可以參考下2023-07-07

