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();
 }
 }
}