java封装数据库操作_JAVA中封装对数据库的访问操作
resultset 遍历对数据库SQL的操作不外乎就是增删查改,我们通过JBDC连接数据库后,对其进⾏这些操作.
我们在没有封装⽅法之前,对这些操作都是单条逐⼀写SQL语句进⾏增删查改操作.但封装后,我们可以调⽤该⽅法很⽅便的对数据库进⾏操作,下⾯来看⽅法:
这个⽅法是基于DBHelper类⾥已经连接好数据库,在DAO层的封装./** * 执⾏所有的insert、delete、update语句(without transaction) * @param sql * @param params * @return int 影响数据库的⾏数 */ public int executeUpdate(String params) { //⽤来向数据库发送sql语句的 PreparedStatement pstm = null; //Connection 是⼀个数据连接 Connection conn = null; try { //连接数据库 conn = Instance().getConnection(); //实例化pstm pstm = conn.prepareStatement(sql); //给sql语句中的占位符赋值,根据params中元素的数量推断sql语句中占位符(?)的数量 if(params != null && params.length > 0) { for(int i = 0 ; i < params.length ; i++) { pstm.setObject(i+1, params[i]); } } //发送sql语句 int i = uteUpdate(); return i; } catch (Exception e) { e.printStackTrace(); } finally { Instance().close(pstm, conn); } return 0; }
以上⽅法是对数据库进⾏增删改操作的.
下⾯来看对数据库的查询,以下⽅法只能对单表进⾏查询:/** * 执⾏所有的单表查询,(条件,分页) * @param sql * @param clz * @param params * @return List */ public List executeQuery(String sql,Class params){ ResultSet rs = null; PreparedStatement pstm = null; Connection conn = null; //保存查询到的结果 List data = new ArrayList(); try { //连接数据库conn = Instance().getConnection(); //实例化pstm pstm = conn.prepareStatement(sql); //给sql语句中的占位符赋值,根据params中元素的数量推断sql语句中占位符(?)的数量 if(params != null && params.length > 0) { for(int i = 0 ; i <
params.length ; i++) { pstm.setObject(i+1, params[i]); } } //发送sql语句 rs = uteQuery(); //创建结果集rs 的元数据对象ResultSetMetaData rsmd = rs.getMetaData(); //使⽤元数据对象,查询结果集中的列的数量 int columnCount =
columns.add(columnName); } //遍历结果集 ()) { T t = wInstance(); //根据列的名称,
遍历⾏中的列 for(String columnName : columns) { //获取类中的所有属性,包含private属性 Field [] fields = DeclaredFields(); for(Field field : fields ) { //⽤列名和属性名进⾏⽐较(忽略⼤⼩写) Name().equalsIgnoreCase(columnName)) { Object value = null; Class type = Type(); if(type == java.lang.Integer.class) { value = rs.getInt(columnName); } else if(type == Double.class) { value = rs.getDouble(columnName); } else if(type == Long.class) { value = rs.getLong(columnName); } else if(type == String.class) { value = rs.getString(columnName); } else if(type == Boolean.class) { value = rs.getBoolean(columnName); } else if(type == Date.class || type == java.util.Date.class) { value = rs.getDate(columnName); } else { value = rs.getObject(columnName); } //强制访问属性 field.setAccessible(true); //给 t 对象的 field属性赋值 field.set(t, value); break; } } } data.add(t); } return data; } catch (Exception e) { e.printStackTrace(); } finally { //释放资源 Instance().close(rs, pstm, conn); } return null; }
下⾯来看可以多表查询的⽅法:/** * 执⾏所有的查询 * @param sql * @param clz * @param params * @return List */ public List executeQuery(String sql,ResultHandler params){ ResultSet rs = null; PreparedStatement pstm = null; Connection conn = null; try { //连接数据库 conn = Instance().getConnection(); //实例化pstm pstm =
conn.prepareStatement(sql); //给sql语句中的占位符赋值,根据params中元素的数量推断sql语句中占
位符(?)的数量 if(params != null && params.length > 0) { for(int i = 0 ; i < params.length ; i++) { pstm.setObject(i+1, params[i]); } } //发送sql语句 rs = uteQuery(); List data = handler.handResult(rs); return data; } catch (Exception e) { e.printStackTrace(); } finally { //释放资源 Instance().close(rs, pstm, conn); } return null; }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。