Java连接Oracle数据库的基类
作用:1.Java连接Oracle数据库 2.关闭连接 3.提供查询方法 4.提供增删改方法
1 BaseDao 2 package dao; 3 import java.sql.*; 4 5 /** 6 * @author 数据库操作基类 7 */ 8 public class BaseDao { 9 public final static String DRIVER = "oracle.jdbc.driver.OracleDriver"; // 数据库驱动 10 11 public final static String URL = "jdbc:oracle:thin:@Perston-PC:1521:myhr"; // url 12 13 public final static String DBNAME = "system"; // 数据库用户名 14 15 public final static String DBPASS = "myhr"; // 数据库密码 16 17 /** 18 * 得到数据库连接 19 * 20 * @throws ClassNotFoundException 21 * @throws SQLException 22 * @return 数据库连接 23 */ 24 public Connection getConn() throws ClassNotFoundException, SQLException { 25 Connection conn = null; 26 try { 27 Class.forName(DRIVER); // 注册驱动 28 conn = DriverManager.getConnection(URL, DBNAME, DBPASS); // 获得数据库连接 29 } catch (SQLException e) { 30 e.printStackTrace(); 31 } 32 return conn; // 返回连接 33 } 34 35 /** 36 * 释放资源 37 * 38 * @param conn 39 * 数据库连接 40 * @param pstmt 41 * PreparedStatement对象 42 * @param rs 43 * 结果集 44 */ 45 public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) { 46 47 /* 如果rs不空,关闭rs */ 48 if (rs != null) { 49 try { 50 rs.close(); 51 } catch (SQLException e) { 52 e.printStackTrace(); 53 } 54 } 55 /* 如果pstmt不空,关闭pstmt */ 56 if (pstmt != null) { 57 try { 58 pstmt.close(); 59 } catch (SQLException e) { 60 e.printStackTrace(); 61 } 62 } 63 /* 如果conn不空,关闭conn */ 64 if (conn != null) { 65 try { 66 conn.close(); 67 } catch (SQLException e) { 68 e.printStackTrace(); 69 } 70 } 71 72 } 73 74 /** 75 * 执行SQL语句,可以执行查询 76 * 77 * @param sql 78 * 预编译的 SQL 语句 79 * @param param 80 * 预编译的 SQL 语句中的‘?’参数的字符串数组 81 * @return 影响的条数 82 */ 83 public ResultSet executeQuerySQL(String preparedSql, String[] param) { 84 Connection conn = null; 85 PreparedStatement pstmt = null; 86 ResultSet rs = null; 87 // int num = 0; 88 89 /* 处理SQL,执行SQL */ 90 try { 91 conn = getConn(); // 得到数据库连接 92 pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象 93 if (param != null) { 94 for (int i = 0; i < param.length; i++) { 95 pstmt.setString(i + 1, param[i]); // 为预编译sql设置参数 96 } 97 } 98 rs = pstmt.executeQuery(); // 执行SQL语句 99 } catch (ClassNotFoundException e) { 100 e.printStackTrace(); // 处理ClassNotFoundException异常 101 } catch (SQLException e) { 102 e.printStackTrace(); // 处理SQLException异常 103 } 104 return rs; 105 } 106 107 /** 108 * 执行SQL语句,可以进行增、删、改的操作,不能执行查询 109 * 110 * @param sql 111 * 预编译的 SQL 语句 112 * @param param 113 * 预编译的 SQL 语句中的‘?’参数的字符串数组 114 * @return 影响的条数 115 */ 116 public int executeSQL(String preparedSql, Object[] param) { 117 Connection conn = null; 118 PreparedStatement pstmt = null; 119 int num = 0; 120 121 /* 处理SQL,执行SQL */ 122 try { 123 conn = getConn(); // 得到数据库连接 124 pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象 125 if (param != null) { 126 for (int i = 0; i < param.length; i++) { 127 pstmt.setObject(i + 1, param[i]); // 为预编译sql设置参数 128 } 129 } 130 // System.out.println(preparedSql); 131 num = pstmt.executeUpdate(); // 执行SQL语句 132 } catch (ClassNotFoundException e) { 133 e.printStackTrace(); // 处理ClassNotFoundException异常 134 } catch (SQLException e) { 135 e.printStackTrace(); // 处理SQLException异常 136 } finally { 137 this.closeAll(conn, pstmt, null); 138 } 139 return num; 140 } 141 } 142 /** 143 * 执行SQL语句,可以执行查询 144 * 145 * @param sql 146 * 预编译的 SQL 语句 147 * @param param 148 * 预编译的 SQL 语句中的‘?’参数的字符串数组 149 * @return 影响的条数 150 */ 151 public ResultSet executeQuerySQL(String preparedSql, Object[] param) { 152 Connection conn = null; 153 PreparedStatement pstmt = null; 154 ResultSet rs = null; 155 // int num = 0; 156 157 /* 处理SQL,执行SQL */ 158 try { 159 conn = getCon(); // 得到数据库连接 160 pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象 161 if (param != null) { 162 for (int i = 0; i < param.length; i++) { 163 pstmt.setObject(i + 1, param[i]); // 为预编译sql设置参数 164 } 165 } 166 rs = pstmt.executeQuery(); // 执行SQL语句 167 } catch (SQLException e) { 168 e.printStackTrace(); // 处理SQLException异常 169 } 170 return rs; 171 } 172 173 /** 174 * 执行SQL语句,可以进行增、删、改的操作,不能执行查询 175 * 176 * @param sql 177 * 预编译的 SQL 语句 178 * @param param 179 * 预编译的 SQL 语句中的‘?’参数的字符串数组 180 * @return 影响的条数 181 */ 182 public int executeSQL(String preparedSql, Object[] param) { 183 Connection conn = null; 184 PreparedStatement pstmt = null; 185 int num = 0; 186 187 /* 处理SQL,执行SQL */ 188 try { 189 conn = getCon(); // 得到数据库连接 190 pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象 191 if (param != null) { 192 for (int i = 0; i < param.length; i++) { 193 pstmt.setObject(i + 1, param[i]); // 为预编译sql设置参数 194 } 195 } 196 // System.out.println(preparedSql); 197 num = pstmt.executeUpdate(); // 执行SQL语句 198 }catch (SQLException e) { 199 e.printStackTrace(); // 处理SQLException异常 200 } finally { 201 this.closeAll(conn, pstmt, null); 202 } 203 return num; 204 }