jdbc简单封装
来源:互联网 发布:vmware nat 端口 编辑:程序博客网 时间:2024/06/05 00:44
- 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
- jdbc简单封装
- jdbc简单封装 1
- jdbc简单封装 2
- jdbc简单封装 4
- 简单JDBC封装类
- DatabaseSession 简单jdbc封装!
- jdbc简单封装类
- DbUtils简单封装JDBC
- JDBC及简单封装
- jdbc连接池简单封装
- java的jdbc简单封装
- 针对JDBC的简单封装
- j2ee规范-jdbc-简单封装
- jdbc 简单通用JDBC辅助类封装
- 简单的jdbc封装+servlet的封装
- JDBC的一个简单封装类DBUtil
- 一个简单封装的jdbc工具类
- 用I/O流建立一个text.txt并写入字符。
- 烦恼歌
- I have a CSDN blog today!
- ASP.net的url重写
- Linux 常用命令
- jdbc简单封装
- CSDN2007年英雄会札记 - 聚会众生相(兄弟篇)
- 准备GCT开题报告
- 一道恶心的JAVA面试题
- 数据库 内连接与外连接
- PL/SQL之入门篇B
- 孟岩、王大力、孙勇畅谈ROR
- java中输入输出的总括(初学必看)
- 优化C++的代码方法