java连接sql执行删除_SQLUtil,java JDBC 实现sql语句execute执行插入、更新、删除dml操作,查询queryList数据库单列List数据,查询queryObject单记...

本文摘自

http://www.xwood.net/_site_domain_/_root/5870/5874/t_c277906.html

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public final class SQLUtil {

private static Log logger = LogFactory.getLog(SQLUtil.class);

/**

* 执行插入、更新、删除操作

*

* @param dataSource

* @param sql

* @param params

* @throws SQLException

*/

public static int execute(DataSource dataSource, String sql, Object... params) throws SQLException {

logger.debug("start execute sql:" + sql);

long startTime = System.currentTimeMillis();

Connection conn = null;

Statement stat = null;

int updateCount = 0;

try {

conn = dataSource.getConnection();

if (params == null || params.length == 0) {

stat = conn.createStatement();

stat.execute(sql);

} else {

stat = conn.prepareStatement(sql);

PreparedStatement pstat = (PreparedStatement) stat;

int parameterIndex = 1;

for (Object param : params) {

pstat.setObject(parameterIndex, param);

parameterIndex++;

}

pstat.execute();

}

updateCount = stat.getUpdateCount();

logger.debug("end execute sql:" + sql + " UpdateCount:" + updateCount + " "

+ (System.currentTimeMillis() - startTime) + "ms");

} finally {

close(stat);

close(conn);

}

return updateCount;

}

/**

* 查询数据库单列List数据

*

* @param dataSource

* @param sql

* @param params

* @return

* @throws SQLException

*/

public static List queryList(DataSource dataSource, String sql, Object... params) throws SQLException {

logger.debug("start execute sql:" + sql);

long startTime = System.currentTimeMillis();

Connection conn = null;

Statement stat = null;

List resultList = new ArrayList();

try {

conn = dataSource.getConnection();

stat = conn.createStatement();

if (params == null || params.length == 0) {

stat = conn.createStatement();

ResultSet rs = stat.executeQuery(sql);

int i = 1;

while (rs.next()) {

resultList.add(rs.getObject(i));

i++;

}

return resultList;

} else {

stat = conn.prepareStatement(sql);

PreparedStatement pstat = (PreparedStatement) stat;

int parameterIndex = 1;

for (Object param : params) {

pstat.setObject(parameterIndex, param);

parameterIndex++;

}

ResultSet rs = pstat.executeQuery();

int i = 1;

while (rs.next()) {

resultList.add(rs.getObject(i));

i++;

}

}

logger.debug("end query sql:" + sql + " Result:" + resultList + " "

+ (System.currentTimeMillis() - startTime) + "ms");

} finally {

stat.close();

conn.close();

}

return resultList;

}

public static Object queryObject(DataSource dataSource, String sql, Object... params) throws SQLException {

List resultList = queryList(dataSource, sql, params);

if (resultList == null || resultList.size() == 0)

return null;

return resultList.get(0);

}

public static void close(Connection con) {

if (con != null)

try {

con.close();

} catch (SQLException ex) {

logger.debug("Could not close JDBC Connection", ex);

} catch (Throwable ex) {

logger.debug("Unexpected exception on closing JDBC Connection", ex);

}

}

public static void close(Statement stmt) {

if (stmt != null)

try {

stmt.close();

} catch (SQLException ex) {

logger.trace("Could not close JDBC Statement", ex);

} catch (Throwable ex) {

logger.trace("Unexpected exception on closing JDBC Statement", ex);

}

}

public static void close(ResultSet rs) {

if (rs != null)

try {

rs.close();

} catch (SQLException ex) {

logger.trace("Could not close JDBC ResultSet", ex);

} catch (Throwable ex) {

logger.trace("Unexpected exception on closing JDBC ResultSet", ex);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值