JDBC封装增删改查,增加后返回主键等方法

来源:互联网 发布:湖南软件职业学院新闻 编辑:程序博客网 时间:2024/06/04 13:48
/**
* 查询
* @param sql
* @return List<Map>结果集
* @throws SQLException
*/
    @SuppressWarnings("rawtypes")
    public static List query(String sql) throws SQLException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
        try {
        con = C3P0Utils.getConnection();
        stmt = con.prepareStatement(sql);

        rs = stmt.executeQuery();

//结果集封装为List<Map> 见↓

            return ResultToListMap(rs);
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            
            C3P0Utils.closeResultSet(rs);
            C3P0Utils.closeStatement(stmt);
            C3P0Utils.closeConnection(con);
        }

    }

 /**
     * 插入值后返回主键值
     * 
     * @param sql
     *            插入sql语句
     * @return 返回结果
     * @throws Exception
     */
    public static Object insertWithReturnPrimeKey(String sql)
            throws SQLException {
    Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
        Object result = null;
        try {
            conn = C3P0Utils.getConnection();
            preparedStatement = conn.prepareStatement(sql,
                    PreparedStatement.RETURN_GENERATED_KEYS);
            preparedStatement.execute();
            rs = preparedStatement.getGeneratedKeys();
            if (rs.next()) {
                result = rs.getObject(1);
            }
            return result;
        } catch (SQLException e) {
            throw new SQLException(e);
        }finally {
        C3P0Utils.closeResultSet(rs);
             C3P0Utils.closeStatement(preparedStatement);
             C3P0Utils.closeConnection(conn);
}
    }
  /**
     * 用于增删改
     * 
     * @param sql
     *            sql语句
     * @param paramters
     *            sql语句
     * @return 影响行数
     * @throws SQLException
     */
    public static int update(String sql) throws SQLException {
    Connection conn = null;
PreparedStatement preparedStatement = null;
        try {
        conn = C3P0Utils.getConnection();
        preparedStatement = conn.prepareStatement(sql);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            C3P0Utils.closeStatement(preparedStatement);
            C3P0Utils.closeConnection(conn);
        }
    }


 /* 用于增删改  带参数

* @param sql
* @param paramters
* @return
* @throws SQLException
*/
public static int update(String sql, Object... paramters) throws SQLException {
try {
getPrepareStatement(sql);
for (int i = 0; i < paramters.length; i++) {
prepareStatement.setObject(i + 1, paramters[i]);
}
return prepareStatement.executeUpdate();
} catch (SQLException e) {
throw new SQLException(e);
} finally {
free(null);
}


}

/**
* Result类型转换将结果集封装为Lsit<Map>
* @param rs 
* @return List
* @throws SQLException
*/
@SuppressWarnings({ "unchecked", "rawtypes"})
    public static List ResultToListMap(ResultSet rs) throws SQLException {
        List list = new ArrayList();
        while (rs.next()) {
            ResultSetMetaData md = rs.getMetaData();
            Map map = new HashMap();
            for (int i = 1; i <= md.getColumnCount(); i++) {
                map.put(md.getColumnLabel(i), rs.getObject(i));
            }
            list.add(map);
        }
        return list;
    }

原创粉丝点击