发布网友 发布时间:2022-04-10 21:37
共3个回答
热心网友 时间:2022-04-10 23:06
package com.oa..impl; import java.sql.Connection; </SPAN></li>import java.sql.PreparedStatement; </SPAN></li>import java.sql.ResultSet; </SPAN></li>import java.sql.SQLException; </SPAN></li>import java.sql.Statement; </SPAN></li>import java.util.List; </SPAN></li>import java.util.Map; </SPAN></li> import org.apache.commons.logging.Log; </SPAN></li>import org.apache.commons.logging.LogFactory; </SPAN></li> import com.oa.core.DynaForm; </SPAN></li>import com.oa..Dao; </SPAN></li>import com.oa.jdbc.ConnectionFactory; </SPAN></li>import com.oa.jdbc.DaoAccessException; </SPAN></li>import com.oa.jdbc.DybaBean; </SPAN></li>import com.oa.jdbc.JdbcPage; </SPAN></li>import com.oa.jdbc.Page; </SPAN></li>import com.oa.jdbc.RowsDynaClass; </SPAN></li> /** </SPAN></li> * 基类DAO,实现一些基本共有方法 * * @author Gao_wx Mar 26, 2008 */ public class BaseDao implements Dao { </SPAN></li> protected Log logger = LogFactory.getLog(BaseDao.class); </SPAN></li> // 获取数据库链接 </SPAN></li> protected Connection getConnection() { </SPAN></li> return ConnectionFactory.getConnection(); </SPAN></li> } // 在此链接上,根据sql语句获取List </SPAN></li> protected List<DybaBean> getList(Connection conn, String sql) </SPAN></li> throws DaoAccessException { </SPAN></li> try { </SPAN></li> Statement st = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = st.executeQuery(sql); return getList(rs); </SPAN></li> } catch (SQLException e) { </SPAN></li> throw new DaoAccessException(sql, e); </SPAN></li> } } // 根据Result获取List </SPAN></li> protected List<DybaBean> getList(ResultSet rs) throws SQLException { </SPAN></li> RowsDynaClass rsdc = new RowsDynaClass(rs); </SPAN></li> return rsdc.getRows(); </SPAN></li> } // 根据sql语句获取List </SPAN></li> protected List<DybaBean> getList(String sql) throws DaoAccessException { </SPAN></li> Connection conn = getConnection(); return getList(conn, sql); </SPAN></li> } // 在此连接上,根据sql语句,id加载一条记录 </SPAN></li> protected DybaBean loadById(Connection conn, String sql, int id) </SPAN></li> throws DaoAccessException { </SPAN></li> try { </SPAN></li> PreparedStatement pst = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); pst.setInt(1, id); </SPAN></li> ResultSet rs = pst.executeQuery(); List list = getList(rs); if (list.size() > 1) { </SPAN></li> logger.warn("加载了2条以上的记录,请检查sql:" + sql); </SPAN></li> } if (list.size() == 0) { </SPAN></li> logger.warn("数据库没有此记录!"); </SPAN></li> return null; </SPAN></li> } else { </SPAN></li> return (DybaBean) list.get(0); </SPAN></li> } } catch (SQLException e) { </SPAN></li> throw new DaoAccessException(sql, e); </SPAN></li> } } // 根据sql语句,id,加载一条记录 </SPAN></li> protected DybaBean loadById(String sql, int id) throws DaoAccessException { </SPAN></li> Connection conn = getConnection(); return loadById(conn, sql, id); </SPAN></li> } // 在此连接上,根据Sql和id删除一条记录,返回影响行数 </SPAN></li> protected int deleteById(Connection conn, String sql, int id) </SPAN></li> throws SQLException { </SPAN></li> PreparedStatement pst = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); pst.setInt(1, id); </SPAN></li> int del = pst.executeUpdate(); </SPAN></li> return del; </SPAN></li> } // 根据sql语句,id删除一条记录 </SPAN></li> protected int deleteById(String sql, int id) throws DaoAccessException { </SPAN></li> Connection conn = getConnection(); try { </SPAN></li> int del = deleteById(conn, sql, id); </SPAN></li> return del; </SPAN></li> } catch (SQLException e) { </SPAN></li> throw new DaoAccessException(sql, e); </SPAN></li> } } protected Page getPage(DynaForm form, String sql) throws DaoAccessException { </SPAN></li> Connection conn = getConnection(); Page page = getPage(conn, form, sql); return page; </SPAN></li> } // 获取分页对象 </SPAN></li> protected Page getPage(Connection conn, DynaForm form, String sql) </SPAN></li> throws DaoAccessException { </SPAN></li> int p = form.getInt("p"); </SPAN></li> if (p < 1) { </SPAN></li> p = 1; </SPAN></li> } int size = form.getInt("size"); </SPAN></li> if (size < 10) { </SPAN></li> size = 10; </SPAN></li> } Page page; try { </SPAN></li> page = new JdbcPage(conn, sql, p, size); </SPAN></li> } catch (SQLException e) { </SPAN></li> throw new DaoAccessException(sql, e); </SPAN></li> } return page; </SPAN></li> } }热心网友 时间:2022-04-11 00:24
package com.;热心网友 时间:2022-04-11 01:59
package s2jsp.lg..impl;