JDBC封装增删改查,增加后返回主键等方法
2017-07-26 11:57
387 查看
/**
* 查询
* @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;
}
* 查询
* @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;
}
相关文章推荐
- 怎样在JAVA 中封装数据库操作(增删改查)运用反射机制!已贴出删除和查询方法,求增加和修改!
- JDBC连接MySQL 方法(封装方法,把增删改查操作封装在函数中)
- 用hibernate的save()方法返回的是新增加数据的主键
- 关于MySQL返回插入的自增主键的几种方法
- jdbc封装mySQL数据库增删改查
- Mybatis中,当插入数据后,返回最新主键id的几种方法,及具体用法
- JDBC常用方法封装
- DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)
- Jdbc封装的增删查改
- Jdbc 增删改查工具方法
- 两种方法在插入数据的时候,返回直增字段的主键
- SqliteOpenHelper封装的增删改查方法(16)
- 转摘 JDBC 插入数据返回数据主键
- JDBC_操纵MySQL最底层的方法封装详解
- MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)
- JDBC 工厂封装返回一个Connection对象
- 数据库插入数据返回当前主键ID值方法
- 封装LDAP 增删改查 方法
- mybatis主键自增返回id值的方法
- jdbc连接Mysql方法封装