1 /**
2 * 增删改查操作公共类(以oracle数据库为例)3 * 2017-8-29 wuyafei4 *5 */
6 public classCrudUtil {7 /**
8 * 1.获取数据库连接9 * 2.获取statement,加载sql语句10 * 3.获取resultset11 *12 */
13 public static Connection conn=null;14 public static Statement sm=null;15 public static ResultSet rs=null;16
17
18 /**
19 * 查询操作20 *@paramsql21 */
22 private static voidexecuteQuery(String sql) {23 try{24 rs=sm.executeQuery(sql);25 while(rs.next()){26 Dept dept=newDept();27 dept.setDeptno(rs.getInt("deptno"));28 dept.setDname(rs.getString("dname"));29 dept.setLoc(rs.getString("loc"));30 System.out.println(dept);31 }32 } catch(SQLException e) {33 //TODO Auto-generated catch block
34 e.printStackTrace();35 }36
37 }38
39 /**
40 * 更新操作(增删改)41 *@paramsql42 */
43 private static voidexecuteUpdate(String sql) {44 try{45 int num=sm.executeUpdate(sql);46 if(num>0){47 System.out.println("更新成功");48 }else{49 System.out.println("更新失败");50 }51 } catch(SQLException e) {52 //TODO Auto-generated catch block
53 e.printStackTrace();54 }55 }56
57
58
59 /**
60 * 关闭资源61 *@paramrs62 *@paramsm63 *@paramconn64 */
65 private static voidcloseResouce(ResultSet rs, Statement sm,66 Connection conn) {67 //TODO Auto-generated method stub
68 if(rs!=null){69 try{70 rs.close();71 } catch(SQLException e) {72 //TODO Auto-generated catch block
73 e.printStackTrace();74 }75 }76 if(sm!=null){77 try{78 sm.close();79 } catch(SQLException e) {80 //TODO Auto-generated catch block
81 e.printStackTrace();82 }83 }84 if(conn!=null){85 try{86 conn.close();87 } catch(SQLException e) {88 //TODO Auto-generated catch block
89 e.printStackTrace();90 }91 }92 }93
94
95
96 /**
97 * 获取statement98 */
99 private staticStatement createStatement() {100 try{101 sm=conn.createStatement();102 } catch(SQLException e) {103 //TODO Auto-generated catch block
104 e.printStackTrace();105 }106 returnsm;107
108 }109
110
111 /**
112 * 获取Connection113 */
114 private staticConnection getConnection() {115 //TODO Auto-generated method stub116 //Connection conn=null;117 //String url="jdbc:oracle:thin:@localhost:1521:orcl";118 //String driver="oracle.jdbc.OracleDriver";119 //String username="scott";120 //String password="tiger";
121 Properties pt=newProperties();122 InputStream is=null;123 try{124 //通过类加载器加载配置文件
125 is=CrudUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");126 //通过类加载配置文件,文件名称需要加上"/"127 //is = CrudUtil.class.getResourceAsStream("/jdbc.properties");
128 pt.load(is);129 //数据库驱动
130 String driver=pt.getProperty("driver");131 String url=pt.getProperty("url");132 String username=pt.getProperty("username");133 String password=pt.getProperty("password");134 try{135 //注册加载驱动
136 Class.forName(driver);137 conn=DriverManager.getConnection(url, username, password);138 System.out.println("get connection success!~"+conn);139 } catch(ClassNotFoundException e) {140 //TODO Auto-generated catch block
141 e.printStackTrace();142 } catch(SQLException e) {143 //TODO Auto-generated catch block
144 e.printStackTrace();145 }146 } catch(FileNotFoundException e1) {147 //TODO Auto-generated catch block
148 e1.printStackTrace();149 } catch(IOException e1) {150 //TODO Auto-generated catch block
151 e1.printStackTrace();152 }finally{153 if(is!=null){154 try{155 is.close();156 } catch(IOException e) {157 //TODO Auto-generated catch block
158 e.printStackTrace();159 }160 }161 }162 returnconn;163
164
165 }166
167 public static voidmain(String[] args) {168 //1.获取Connection
169 conn=getConnection();170 //2.获取Statement
171 sm=createStatement();172 //3.操作
173
174 /**
175 * 3.1.查询操作176 */
177
178 String sql="select * from dept";179 executeQuery(sql);180
181 /**
182 * 3.2.添加操作183 */
184
185 //String sql="insert into dept values (50,'招生部','8楼')";186 //executeUpdate(sql);
187
188 /**
189 * 3.3.修改操作190 */
191
192 //String sql="update dept set dname='就业部' where deptno=50";193 //executeUpdate(sql);
194
195 /**
196 * 3.4.删除操作197 */
198
199 //String sql="delete from dept where deptno=60";200 //executeUpdate(sql);201
202 //4.关闭资源
203 closeResouce(rs,sm,conn);204
205 }206
207 }