- package test;
- import java.sql.*;
- import static java.lang.System.out;
- /**
- * Created by ${fooxe} on 2016/2/2.
- * 仅仅针对oracle,
- * 功能定义:Blob,Clob数据处理,
- * 待定功能: 格式化查询语句,格 stmt);//关闭相关资源
- }
- return rs;
- }
- /**
- * 执行prepareStatement
- *
- * @param sql
- * @return
- */
- public ResultSet pstmtExecute(String sql) {
- try {
- conn = getConn();//获取数据库连接
- conn.setAutoCommit(false);//开始事务
- pstmt = conn.prepareStatement(sql);
- int a = pstmt.executeUpdate(sql);
- conn.commit();//提交事务
- conn.setAutoCommit(true);//事务结束
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- release(conn, pstmt, stmt);
- }
- return rs;
- }
- /**
- * 执行存储过程callableStatement
- *
- * @param sql
- * @return
- */
- public boolean psExecute(String sql, String... params) throws SQLException {
- conn = getConn();
- conn.setAutoCommit(false);
- cs = conn.prepareCall(sql);//执行存储过程
- boolean b = cs.execute();//执行结果
- if (null != params) {
- for (int i = 0; i < params.length; i++) cs.setString(8, params[i]);
- }
- cs.execute(sql);
- conn.setAutoCommit(true);
- closeConn(conn);
- return b;
- }
- /**
- * 获取查询结果集,获取ResultSet对象
- *
- * @param sql
- * @return
- * @throws SQLException
- */
- public ResultSet getRs(String sql) throws SQLException {
- return rs;
- }
- /**
- * 这里可能需要将3个资源释放拆开。
- *
- * @param conn
- * @param pstmt
- * @param stmt
- */
- private void release(Connection conn, PreparedStatement pstmt, Statement stmt) {
- try {
- if (pstmt != null) {
- pstmt.close();
- pstmt = null;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- if (stmt != null) {
- stmt.close();
- stmt = null;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- closeConn(conn);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1209201615065.html
来源: http://www.codesnippet.cn/detail/1209201615065.html