Java的SqlHelper 工具类

原文地址

  1. <pre name="code" class="java"></pre><pre name="code" class="java"><pre name="code" class="java"></pre><pre name="code" class="java">context.xml中添加以下内容  
  2. <Resource name="jdbc/TestDB" auth="Container"   
  3.                type="javax.sql.DataSource"  
  4.                maxActive="100" maxIdle="30" maxWait="10000"  
  5.                username="root" password="hufan.88"   
  6.                driverClassName="com.mysql.jdbc.Driver"  
  7.                url="jdbc:mysql://127.0.0.1:3306/hoofan"/>  
  8.   
  9. import java.sql.*;  
  10. import java.util.ArrayList;  
  11. import javax.naming.*;  
  12. import javax.sql.DataSource;  
  13.   
  14.   
  15. public class SqlHelper {  
  16.     private static DataSource ds;  
  17.       
  18.     static{  
  19.         try {  
  20.             //1.获取命名上下文接口  
  21.             Context context = new InitialContext();  
  22.             //2.根据名称查询服务器上的DataSource  代表前缀: java:/comp/env    
  23.             ds = (DataSource)context.lookup("java:/comp/env/jdbc/TestDB");  
  24.         } catch (Exception e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28.       
  29.     /** 
  30.      * 获取数据库连接 
  31.      * @return Connection 
  32.      * */  
  33.     public static Connection getConnection(){  
  34.         try {  
  35.             return ds.getConnection();  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.             return null;  
  39.         }  
  40.     }  
  41.       
  42.     /** 
  43.      * 关闭资源 
  44.      * @param Connection con,Statement stm,ResultSet rs 
  45.      * */  
  46.     public static void close(ResultSet rs,PreparedStatement ps,Connection ct){  
  47.         try {  
  48.             if(rs!=null)rs.close();  
  49.             if(ps!=null)ps.close();  
  50.             if(ct!=null)ct.close();  
  51.         } catch (Exception e) {  
  52.             // TODO: handle exception  
  53.             e.printStackTrace();  
  54.         }  
  55.     }  
  56.     public ArrayList executeQuery(String sql,String[] arr){  
  57.         Connection ct=null;  
  58.         PreparedStatement ps=null;  
  59.         ResultSet rs=null;  
  60.         try {  
  61.             ct=getConnection();  
  62.             ps=ct.prepareStatement(sql);  
  63.             if(arr!=null&&!arr.equals("")){  
  64.                 for(int i=0;i<arr.length;i++){  
  65.                     ps.setString(i+1, arr[i]);  
  66.                 }  
  67.             }  
  68.             rs=ps.executeQuery();  
  69.             ArrayList al=new ArrayList();  
  70.             ResultSetMetaData rsmd=rs.getMetaData();  
  71.             int column=rsmd.getColumnCount();  
  72.             while(rs.next()){  
  73.                 Object[] ob=new Object[column];  
  74.                 for(int i=0;i<ob.length;i++){  
  75.                     ob[i]=rs.getObject(i+1);  
  76.                 }  
  77.                 al.add(ob);  
  78.             }  
  79.             return al;  
  80.         } catch (Exception e) {  
  81.             // TODO: handle exception  
  82.             e.printStackTrace();  
  83.             return null;  
  84.         }finally{  
  85.             close(rs,ps,ct);  
  86.         }  
  87.     }  
  88.     public void executeUpdate(String sql,String arr[]){  
  89.         Connection ct=null;  
  90.         PreparedStatement ps=null;  
  91.         ResultSet rs=null;  
  92.         try {  
  93.             ct=getConnection();  
  94.             ps=ct.prepareStatement(sql);  
  95.             if(arr!=null&&!arr.equals("")){  
  96.                 for(int i=0;i<arr.length;i++){  
  97.                     ps.setString(i+1, arr[i]);  
  98.                 }  
  99.             }  
  100.             ps.executeUpdate();  
  101.         } catch (Exception e) {  
  102.             // TODO: handle exception  
  103.             e.printStackTrace();  
  104.         }  
  105.     }  
  106. }</pre><br><br></pre><pre name="code" class="java">//</pre><pre name="code" class="java">package com.utils;  
  107. import java.sql.*;  
  108. import java.util.*;  
  109. public class SqlHelper {  
  110.       
  111.     static{  
  112.         try {  
  113.             Class.forName("com.mysql.jdbc.Driver");  
  114.         } catch (ClassNotFoundException e) {  
  115.             // TODO Auto-generated catch block  
  116.             e.printStackTrace();  
  117.         }  
  118.     }  
  119.     private Connection getConnection(){  
  120.         try {  
  121.             return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hufan","root","hufan.88");  
  122.         } catch (SQLException e) {  
  123.             // TODO Auto-generated catch block  
  124.             e.printStackTrace();  
  125.             return null;  
  126.         }  
  127.     }  
  128.     public static void close(ResultSet rs,PreparedStatement ps,Connection ct){  
  129.         try {  
  130.             if(rs!=null)rs.close();  
  131.             if(ps!=null)ps.close();  
  132.             if(ct!=null)ct.close();  
  133.         } catch (Exception e) {  
  134.             // TODO: handle exception  
  135.             e.printStackTrace();  
  136.         }  
  137.     }  
  138.     public ArrayList executeQuery(String sql,String[] arr){  
  139.         Connection ct=null;  
  140.         PreparedStatement ps=null;  
  141.         ResultSet rs=null;  
  142.         try {  
  143.             ct=getConnection();  
  144.             ps=ct.prepareStatement(sql);  
  145.             if(arr!=null&&!arr.equals("")){  
  146.                 for(int i=0;i<arr.length;i++){  
  147.                     ps.setString(i+1, arr[i]);  
  148.                 }  
  149.             }  
  150.             rs=ps.executeQuery();  
  151.             ArrayList al=new ArrayList();  
  152.             ResultSetMetaData rsmd=rs.getMetaData();  
  153.             int column=rsmd.getColumnCount();  
  154.             while(rs.next()){  
  155.                 Object[] ob=new Object[column];  
  156.                 for(int i=0;i<ob.length;i++){  
  157.                     ob[i]=rs.getObject(i+1);  
  158.                 }  
  159.                 al.add(ob);  
  160.             }  
  161.             return al;  
  162.         } catch (Exception e) {  
  163.             // TODO: handle exception  
  164.             e.printStackTrace();  
  165.             return null;  
  166.         }finally{  
  167.             close(rs,ps,ct);  
  168.         }  
  169.     }  
  170.     public void executeUpdate(String sql,String arr[]){  
  171.         Connection ct=null;  
  172.         PreparedStatement ps=null;  
  173.         ResultSet rs=null;  
  174.         try {  
  175.             ct=getConnection();  
  176.             ps=ct.prepareStatement(sql);  
  177.             if(arr!=null&&!arr.equals("")){  
  178.                 for(int i=0;i<arr.length;i++){  
  179.                     ps.setString(i+1, arr[i]);  
  180.                 }  
  181.             }  
  182.             ps.executeUpdate();  
  183.         } catch (Exception e) {  
  184.             // TODO: handle exception  
  185.             e.printStackTrace();  
  186.         }  
  187.     }  
  188. }  
  189. </pre><br><br>  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值