- package frame.dao;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- public abstract class BaseDao {
- private Connection getConnection() throws SQLException {
- return ConnectionFactory.getConnection();
- }
- /**
- * 获得PreapredStatement的方法
- *
- * @param sqlString
- * @return
- * @throws SQLException
- */
- protected PreparedStatement getPreparedStatement(String sqlString) throws SQLException {
- return getConnection().prepareStatement(sqlString);
- }
- /**
- * 根据条件取出前n条的方法
- *
- * @param sqlString
- * @param size
- * @return
- * @throws SQLException
- */
- protected PreparedStatement getPreparedStatement(String sqlString, int size) throws SQLException {
- return getConnection().prepareStatement(sqlString + " LIMIT " + size);
- }
- /**
- * 根据条件分页的方法
- *
- * @param sqlString
- * @param begin
- * @param size
- * @return
- * @throws SQLException
- */
- protected PreparedStatement getPreparedStatement(String sqlString, int begin, int size) throws SQLException {
- return getConnection().prepareStatement(sqlString + " LIMIT " + begin + "," + size);
- }
- /**
- * 释放PreparedStatement 资源,insert delete update 操作调用
- *
- * @param pstm
- */
- private void destoryResource(PreparedStatement pstm) {
- if (pstm != null) {
- try {
- pstm.close();
- }
- catch (SQLException e) {
- System.out.println(e.toString());
- }
- }
- }
- /**
- * 释放PreparedStatement 资源,select 调用
- *
- * @param pstm
- * @param rs
- */
- private void destoryResource(PreparedStatement pstm, ResultSet rs) {
- if (pstm != null) {
- try {
- pstm.close();
- }
- catch (SQLException e) {
- System.out.println(e.toString());
- }
- }
- if (rs != null) {
- try {
- rs.close();
- }
- catch (SQLException e) {
- System.out.println(e.toString());
- }
- }
- }
- /**
- * 执行sql insert delete update调用
- *
- * @param pstm
- * @throws SQLException
- */
- protected void execute(PreparedStatement pstm) throws SQLException {
- try {
- pstm.execute();
- }
- catch (SQLException e) {
- e.printStackTrace();
- throw new SQLException();
- }
- finally {
- this.destoryResource(pstm);
- }
- }
- /**
- * 批处理操作
- *
- * @param pstm
- * @throws SQLException
- */
- protected void executeBatch(PreparedStatement pstm) throws SQLException {
- try {
- pstm.executeBatch();
- }
- catch (SQLException e) {
- e.printStackTrace();
- throw new SQLException();
- }
- finally {
- this.destoryResource(pstm);
- }
- }
- /**
- * update 操作
- *
- * @param pstm
- * @return
- * @throws SQLException
- */
- protected int update(PreparedStatement pstm) throws SQLException {
- int flag = 0;
- try {
- flag = pstm.executeUpdate();
- }
- catch (SQLException e) {
- e.printStackTrace();
- throw new SQLException();
- }
- finally {
- this.destoryResource(pstm);
- }
- return flag;
- }
- /**
- * 获得一个对象的操作
- *
- * @param pstm
- * @param objCall
- * @return
- * @throws SQLException
- */
- protected T getObject(PreparedStatement pstm, ResultCall objCall) throws SQLException {
- T obj = null;
- ResultSet rs = null;
- try {
- rs = pstm.executeQuery();
- if (rs.next()) {
- obj = objCall.getResultObject(rs);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- throw new SQLException();
- }
- finally {
- this.destoryResource(pstm, rs);
- }
- return obj;
- }
- /**
- * 返回整形数据的操作
- *
- * @param pstm
- * @return
- * @throws SQLException
- */
- protected int getInt(PreparedStatement pstm) throws SQLException {
- int num = 0;
- ResultSet rs = null;
- try {
- rs = pstm.executeQuery();
- if (rs.next()) {
- num = rs.getInt(1);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- throw new SQLException();
- }
- finally {
- this.destoryResource(pstm, rs);
- }
- return num;
- }
- /**
- * 返回长整形数据的操作
- *
- * @param pstm
- * @return
- * @throws SQLException
- */
- protected long getLong(PreparedStatement pstm) throws SQLException {
- long num = 0;
- ResultSet rs = null;
- try {
- rs = pstm.executeQuery();
- if (rs.next()) {
- num = rs.getLong(1);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- throw new SQLException();
- }
- finally {
- this.destoryResource(pstm, rs);
- }
- return num;
- }
- protected boolean hasObject(PreparedStatement pstm) throws SQLException {
- boolean flag = false;
- ResultSet rs = null;
- try {
- rs = pstm.executeQuery();
- if (rs.next()) {
- flag = true;
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- throw new SQLException();
- }
- finally {
- this.destoryResource(pstm, rs);
- }
- return flag;
- }
- /**
- * 返回一个集合的操作
- *
- * @param pstm
- * @param rsObjCall
- * @return
- * @throws SQLException
- */
- protected List executeQuery(PreparedStatement pstm, ResultCall rsObjCall) throws SQLException {
- List list = new ArrayList ();
- ResultSet rs = null;
- try {
- rs = pstm.executeQuery();
- while (rs.next()) {
- list.add(rsObjCall.getResultObject(rs));
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- throw new SQLException();
- }
- finally {
- this.destoryResource(pstm, rs);
- }
- return list;
- }
- /**
- * 获得新插入数据的id
- *
- * @param pstm
- * @return
- * @throws SQLException
- */
- protected int getCurrentId(PreparedStatement pstm) throws SQLException {
- ResultSet rs = null;
- try {
- pstm.execute();
- rs = pstm.getGeneratedKeys();
- if (rs.next()) {
- return rs.getInt(1);
- }
- }
- catch (Exception e) {
- System.out.println(e.toString());
- throw new SQLException();
- }
- finally {
- this.destoryResource(pstm, rs);
- }
- return 0;
- }
- }
jdbc简单封装3
最新推荐文章于 2024-08-28 21:06:06 发布