java連接Oracle數據庫的方式
1 特點:oracle.jdbc.OracleDriver是注冊oracle驅動類;
jdbc:oracle:thin:@localhost:1521:xe:連接oracle的方式:網絡協議+訪問方式+IP+端口號+xe數據庫;
user:hr數據庫用戶名
Password:hr數據庫的用戶密碼
缺點:statement方式連接數據庫容易被黑客注入式攻擊 所有不安全 現在企業中很少采用這種方式的了
連接數據庫后一定的關閉連接,這個最容易忘記 調用close()方法關閉連接
public static void main(String[] args) throws Exception {
// 1 注冊驅動類
Class.forName("oracle.jdbc.OracleDriver");
// 2 創建連接
String url="jdbc:oracle:thin:@localhost:1521:xe";
Connection con = DriverManager.getConnection(url,"hr","hr");
// 3 創建stm
Statement stm = con.createStatement();
// 4 執行SQL
String sql = "select accountNo cardid,accountName name,balance bal from accounts";
// executeQuery: 執行DQL語句(select),返回ResultSet類型,代表 查詢到的虛表
ResultSet rs = stm.executeQuery(sql);
// 5 處理查詢結果
while (rs.next()){
// 獲取每個字段的值 rs.getXxx("字段名") rs.getXxx(index)
int no = rs.getInt("cardid");
double bal = rs.getDouble("bal");
String name = rs.getString(2);
String pwd = rs.getString("password");
System.out.println("卡號:" + no +" , name=" + name +", bal=" + bal+", pwd=" + pwd);
}
// 6 釋放資源
if (rs != null)
rs.close();
if (stm != null)
stm.close();
if (con != null)
con.close();
}
2 特點:io讀取配飾文件db.properties的信息 配置文件里為自己手寫是mydriver路徑myurl連接oracle的方式:網絡協議+訪問方式+IP+端口號+xe數據庫 myuser數據庫用戶名稱mypasswoed:數據庫訪問密碼
把以上信息封裝到配置文件中 可以復用 減少代碼的冗余 使用一次調用一次
關閉連接封裝帶getRelease()方法中 需要使用時再調用 dao層一般不關閉連接 一般在service層關閉連接 否則容易造成業務出錯
private static Properties prop = new Properties();
優點:封裝成方法 需要時調用 減少代碼的冗余
安全性能提高:?占位符賦值 比第一種方法安全 解決注入式攻擊等問題
缺點:屬於單例模式 多線程並發訪問時 容易產生線程安全問題 例如雙十一秒殺時 多用戶同時訪問同一資源 臨界資源對象如果加鎖會造成線程等待 不加鎖大量用戶並發訪問會造成線程安全問題
// 類加載時,讀一次 配置文件
static{
try {
// 獲取配置文件的輸入流
InputStream is = JdbcUtil2.class.getResourceAsStream("/com/baizhi/day2/db.properties");
// 使用prop的load方法自動讀文件
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關流
}
}
// 獲取連接
public static Connection getConn(){
Connection con = null;
try {
// 獲取配置文件的內容
String driverClassName = prop.getProperty("mydriverclass");
String url = prop.getProperty("myurl");
String userName = prop.getProperty("username");
String pwd = prop.getProperty("password");
Class.forName(driverClassName);
con = DriverManager.getConnection(url, userName, pwd);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return con;
}
// 釋放資源
public static void release(ResultSet rs, Statement stm, Connection con){
if (rs != null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if (stm != null){
try {
stm.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if (con != null){
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
3 特點/優點:引入線程對象ThreadLocal 針對多用戶並發訪問問題 可以支持大量用戶同時訪問
public class JdbcUtil {
private static Properties prop = new Properties();
// 讀配置文件
static {
try {
InputStream is = JdbcUtil.class.getResourceAsStream("/com/baizhi/day3/db.properties");
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
}
}
// 增加靜態的成員變量
private static final ThreadLocal<Connection> thl = new ThreadLocal<Connection>();
// 獲取連接
public static Connection getConn() {
Connection conn = thl.get(); // 從線程局部變量中取值
try {
if (conn == null) { // 沒有值
// 獲取配置文件中的內容
String driverClass = prop.getProperty("mydriverclass");
String myurl = prop.getProperty("myurl");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
Class.forName(driverClass);
conn = DriverManager.getConnection(myurl, username, password);
thl.set(conn); // 把conn存到thl
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return conn;
}
// 釋放資源
public static void release(ResultSet rs, Statement stm, Connection conn) {
try {
if (rs != null)
rs.close();
if (stm != null)
stm.close();
if (conn != null){
conn.close();
thl.remove(); // 關閉的連接 必須從thl刪除************
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
4:特點/優點:引入數據池 減少訪問數據庫的次數 提高執行效率
public class JdbcUtilPool {
public static final ThreadLocal thl=new ThreadLocal();
public static Connection getConn(){
Connection conn=(Connection) thl.get();
try {
if(conn == null){
//獲取資源所在的根
Context ctx = new InitialContext();
//根據名稱和目錄獲取資源
javax.sql.DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myoracle");
//從連接池獲取連接
conn=ds.getConnection();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
return conn;
}
public static void release(ResultSet rs,PreparedStatement pstm,Connection conn){
try {
if (rs!=null) rs.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
try {
if (pstm!=null) pstm.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
try {
if (conn!=null) {
conn.close(); //把連接還給連接池
thl.remove();//清除線程連接
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
}