zoukankan      html  css  js  c++  java
  • 数据库通用Jdbc操作

    public class JdbcUtil {
    
        /**
         * 关闭一个数据库链接
         * @param conn
         */
        public static void closeConnection(Connection conn) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
            }
        }
    
        /**
         * 回滚一个数据库链接
         * @param conn
         */
        public static void rollbackConnection(Connection conn) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException e) {
            }
        }
    
        /**
         * 关闭一个StateMent
         * @param stmt
         */
        public static void closeStatement(Statement stmt) {
            try {
                if (stmt != null) {
                    clearStatement(stmt);  // FIXME 测试
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
    
        /**
         * 关闭一个ResultSet
         * @param rs
         */
        public static void closeResultSet(ResultSet rs) {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
            }
        }
        
        /**
         * 清除一个StateMent里面的批量信息。
         * @param stmt
         */
        public static void clearStatement(Statement stmt) {
            try {
                if (stmt != null) {
                    stmt.clearBatch();
                }
            } catch (SQLException e) {
            }
        }
        
        /**
         * Integer型数据设置
         * @param pstmt 
         * @param index 
         * @param value Integer型数据
         * @throws SQLException
         */
        public static void setValue(PreparedStatement pstmt, int index, Integer value) throws SQLException {
            if (null != value) {
                pstmt.setInt(index, value);
            } else {
                pstmt.setNull(index, Types.INTEGER);
            }
        }
    
        /**
         * Long型数据设置
         * @param pstmt
         * @param index
         * @param value Long型数据
         * @throws SQLException
         */
        public static void setValue(PreparedStatement pstmt, int index, Long value) throws SQLException {
            if (null != value) {
                pstmt.setLong(index, value);
            } else {
                pstmt.setNull(index, Types.BIGINT);
            }
        }
    
        /**
         * String型数据设置
         * @param pstmt
         * @param index
         * @param value String型数据
         * @throws SQLException
         */
        public static void setValue(PreparedStatement pstmt, int index, String value) throws SQLException {
            if (StringUtils.isNotBlank(value)) {
                pstmt.setString(index, value);
            } else {
                pstmt.setNull(index, Types.VARCHAR);
            }
        }
    
        /**
         * Timestamp型数据设置
         * @param pstmt
         * @param index
         * @param value Timestamp型数据
         * @throws SQLException
         */
        public static void setValue(PreparedStatement pstmt, int index, Timestamp value) throws SQLException {
            if (null != value) {
                pstmt.setTimestamp(index, value);
            } else {
                pstmt.setNull(index, Types.TIMESTAMP);
            }
        }
        
        /**
         * Date型数据设置
         * @param pstmt
         * @param index
         * @param value java.sql.Date型数据
         * @throws SQLException
         */
        public static void setValue(PreparedStatement pstmt, int index, java.util.Date value) throws SQLException {
            if (null != value) {
                pstmt.setTimestamp(index, new Timestamp(value.getTime()));
            } else {
                pstmt.setNull(index, Types.TIMESTAMP);
            }
        }
    
        /**
         * byte型数据设置
         * @param pstmt
         * @param index
         * @param value byte型数据
         * @throws SQLException
         */
        public static void setValue(PreparedStatement pstmt, int index, byte[] value) throws SQLException {
            if (null != value) {
                pstmt.setBytes(index, value);
            } else {
                pstmt.setNull(index, Types.BINARY);
            }
        }
    }
  • 相关阅读:
    a和b互换的2种方式
    spring cloud 方法调用 feign
    spring boot redis 五种类型使用实例
    springboot引入properties文件 yml文件
    Spark-Streaming结合Redis
    Spark-Streaming结合Mysql案例
    Springboot与scala编写第一个web程序
    Springboot工程Mybatis二级缓存配置
    小奇画画
    saf
  • 原文地址:https://www.cnblogs.com/mcahkf/p/4766318.html
Copyright © 2011-2022 走看看