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;
}
- JDBC封装增删改查,增加后返回主键等方法
- php增删改查等常用方法封装
- java增删改查JDBC封装类
- Jdbc封装的增删查改
- Jdbc封装的增删查改
- jdbc封装mySQL数据库增删改查
- JDBC连接MySQL 方法(封装方法,把增删改查操作封装在函数中)
- 连接数据库执行增删改查(返回行数、主键值)所有方法
- 封装LDAP 增删改查 方法
- Jdbc 增删改查
- jdbc增删改查
- JDBC 增删改查
- jdbc增删改查
- jdbc增删查改
- JDBC 增删改查
- jdbc:增删改查
- jdbc---增删改查
- springmvc-mybatis的增删改查以及主键返回
- Selenium2+Python--Mac上安装Selenium+Python+PyCharm
- scala代码风格指南--<缩进>
- 抽象方法和接口
- MyBatis打印SQL的配置方法
- shell获取进程ID的方法: pidof | pgrep | ps-A+grep+awk
- JDBC封装增删改查,增加后返回主键等方法
- 欢迎使用CSDN-markdown编辑器
- ITK spacing的问题
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener问题
- linux6.4搭建mysql主从复制
- 百练_4015:邮箱验证
- 深度学习优化函数详解(3)-- mini-batch SGD 小批量随机梯度下降
- mybatis Example条件查询
- ZooKeeper客户端脚本zkCli.sh的节点操作