/**
* 执行一条需要Connection的SQL语句
* @param conn 数据库连接
* @param sql SQL语句
* @param params SQL语句的参数
* @return 执行语句后影响的记录的行数
* @throws SQLException
*/
public static int executeUpdate(Connection conn,String sql,Object...params)
throws SQLException{
PreparedStatement pstmt = null;
int rows = 0;
//重组SQL语句执行
try
{
pstmt = conn.prepareStatement(sql);
for(int i=0; i<params.length; i++){
pstmt.setObject(i+1, params[i]);
}
rows = pstmt.executeUpdate();
}finally{
IOUtil.close(pstmt);
}
return rows;
}
/**
* 执行需要Connection的查询语句
* @param conn 数据库连接
* @param sql SQL语句
* @param params SQL语句参数列表
* @return 查询结果集
* @throws SQLException
*/
public static ResultSet executeQuery(Connection conn,String sql,Object...params)
throws SQLException{
PreparedStatement pstmt = null;
try
{
//执行SQL语句预编译
pstmt = conn.prepareStatement(sql);
//赋值参数
for(int i=0; i<params.length; i ++){
pstmt.setObject(i+1, params[i]);
}
//执行查询
return pstmt.executeQuery();
}catch(SQLException e){
IOUtil.close(pstmt);
throw new RuntimeException("SQL查询出错!",e);
}
}
/**
* 关闭数据库连接
* @param conn 需要关闭的数据库连接对像
*/
public static void close(Connection conn)
{
if(conn != null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务