package com.db.dbHelp; import java.sql.*; public class DBAccess { String sDBDriver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:xdf"; String sHint = ""; ResultSet rs = null; Connection conn = null; Statement stmt = null; String user = "scott"; String pwd = "tiger"; public String getSHint(){ return sHint; } public void setSDBDriver(String dbDriver){ sDBDriver = dbDriver; } public String getSDBDriver(){ return sDBDriver; } public String getUrl(){ return url; } public void setUrl(String s){ url = s; } public ResultSet getResultSet(){ return rs; } //初始化 public boolean initialize(String dbDrive,String dbConn,String user,String pwd){ sDBDriver = dbDrive; url = dbConn; user = user; pwd = pwd; return initialize(); } //初始化 public boolean initialize(){ try{ Class.forName(sDBDriver); sHint = "Initialization sucessfully"; return true; }catch(ClassNotFoundException e){ sHint = "Initialization Exception:" + e.getMessage(); return false; } } //执行查询语句 public ResultSet executeQuery(String sql){ rs = null; try{ conn = DriverManager.getConnection(url,user,pwd); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); }catch(SQLException ex){ sHint = "Query Exception:" + ex.getMessage(); } return rs; } //执行更新语句 public boolean executeUpdate(String sql){ try{ conn = DriverManager.getConnection(url,user,pwd); stmt = conn.createStatement(); stmt.executeUpdate(sql); return true; }catch(SQLException ex){ sHint = "Update Exception :" + ex.getMessage(); return false; } } //关闭连接、结果集、statemernt,释放资源 public boolean close(){ try{ if(rs != null) rs.close(); if(stmt != null) stmt.close(); if(conn != null) conn.close(); return true; }catch(SQLException ex){ sHint = "Close Exception:" + ex.getMessage(); return false; } } }