bss系统管理的片段 package bbs.xzit.dbc; import java.sql.*; import java.util.Iterator; import java.util.LinkedHashMap; public class DatabaseUtil { private DatabaseConnection dbc; public DatabaseUtil(){ dbc = new DatabaseConnection(); } // DML DDL ---insert update delete //insert into table(f1,f2...) values(?,?...?) public int executeUpdate(String sql,Object[] param) throws Exception { Connection connect = dbc.getConn(); int num = 0; PreparedStatement pstmt = null; try { pstmt = connect.prepareStatement(sql); for(int i=0;i<param.length;i++){ pstmt.setObject(i+1, param[i]); } num = pstmt.executeUpdate(); } catch (SQLException ex) { throw new Exception("错误提示:请检查SQL语法是否有误"); //throw ex ; } finally{ close(connect,pstmt,null); } return num; } //select public ResultSet executeQuery(String sql,Object[] param) throws Exception { ResultSet rs = null; Connection connect = dbc.getConn(); PreparedStatement pstmt = null; try { pstmt = connect.prepareStatement(sql); for(int i=0;i<param.length;i++){ pstmt.setObject(i+1, param[i]); } rs = pstmt.executeQuery(); } catch (SQLException ex) { throw new Exception("错误提示:请检查SQL语法是否有误,"+ex.getMessage()); } return rs; } // oder by f1 asc,f2 dsc public ResultSet executeQuery(String sql,Object[] param,LinkedHashMap order) throws Exception { ResultSet rs = null; Connection connect = dbc.getConn(); PreparedStatement pstmt = null; String orderStr = ""; Iterator keys = order.keySet().iterator(); if(order.size()>0) orderStr = "order by "; while(keys.hasNext()){ String key =(String)keys.next(); String keyValue = (String)order.get(key); orderStr = orderStr + key +" " + keyValue +","; } orderStr.substring(0, orderStr.length()-2); sql = sql + orderStr; try { pstmt = connect.prepareStatement(sql); for(int i=0;i<param.length;i++){ pstmt.setObject(i+1, param[i]); } rs = pstmt.executeQuery(); } catch (SQLException ex) { throw new Exception("错误提示:请检查SQL语法是否有误,"+ex.getMessage()); } return rs; } public int executeCount(String sql,Object[] param) throws Exception { ResultSet rs = null; Connection connect = dbc.getConn(); PreparedStatement pstmt = null; int cnt = 0; try { pstmt = connect.prepareStatement(sql); for(int i=0;i<param.length;i++){ pstmt.setObject(i+1, param[i]); } rs = pstmt.executeQuery(); if(rs!=null){ cnt =rs.getInt(1); } } catch (SQLException ex) { throw new Exception("错误提示:"+ex.getMessage()); }finally{ close(connect,pstmt,rs); } return cnt; } public void close(Connection conn,PreparedStatement pstmt,ResultSet rs){ try{ if(rs!=null ) rs.close(); if(pstmt!=null) pstmt.close(); if(conn!=null) conn.close(); }catch(Exception ex){ } } }
- 粉丝: 0
- 资源: 1
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (GUI框架)Matlab设计_手势识别.zip
- (GUI框架)Matlab设计_手势识别设计.zip
- (GUI框架)Matlab设计_手势识别系统.zip
- (GUI框架)Matlab设计_数字信号处理.zip
- (GUI框架)Matlab设计_数字信号处理GUI设计.zip
- (GUI框架)Matlab设计_水果分级系统.zip
- (GUI框架)Matlab设计_水果识别.zip
- (GUI框架)Matlab设计_图像去雾.zip
- (GUI框架)Matlab设计_雾霾车牌识别GUI设计.zip
- (GUI框架)Matlab设计_图像去雾系统.zip
- (GUI框架)Matlab设计_指纹识别.zip
- (GUI框架)Matlab设计_指纹识别系统.zip
- 腾讯云代理商返点政策v1.5-20160701(PPT版)(2).pdf
- 腾讯云代理商系统使用指引 7.5(2).pptx
- 腾讯云产品介绍--全面版.pdf
- 5自由度座椅悬架: 详情请csdn搜索博客:3、5自由度座椅悬架,以及5自由度座椅人体悬架仿真研究 模型保证正确架构清晰有对应参考文献 3自由度座椅悬架: 5自由度座椅悬架: