初学java,这是自己写的mysqlhelper

  1 package servlet;
  2 import java.sql.*;
  3 import java.util.*;
  4 
  5 import javax.sql.*;
  6 import javax.naming.InitialContext;
  7 import javax.naming.Context;
  8 
  9 public class Mysqlhelper {
 10  //public Connection con=null;
 11  /*public static PreparedStatement pst=null;
 12  public static ResultSet res=null;
 13  public static CallableStatement cst=null;
 14  */
 15  
 16  /*
 17   * CallableStatement 存储过程
 18   * PreparedStatement.executeUpdate() 处理insert、update、delete
 19   * PreparedStatement.executeQuery() 处理select
 20   * */
 21  //获取connection
 22  public Connection getconnection(){
 23   Connection con=null;
 24   try{
 25    Context c=new InitialContext();
 26    Context et=(Context)c.lookup("java:/comp/env");
 27    DataSource ds=(DataSource)et.lookup("jdbc/mysqldb");
 28    con= ds.getConnection();
 29    con.setAutoCommit(false);//禁止自动提交事务
 30   }catch(Exception e){
 31    System.out.println(e);
 32   }
 33   return con;
 34  }
 35  /*
 36   * 获取preparedstatement对象---执行sql语句
 37   * */
 38  public PreparedStatement getprepare(Connection conn,String sql){
 39   PreparedStatement pst=null;
 40   try{
 41    pst=conn.prepareStatement(sql);
 42   }catch(SQLException e){
 43    e.printStackTrace();
 44   }
 45   return pst;
 46  }
 47  /*
 48   * 获取callablestatement对象---执行存储过程*
 49   */
 50  public CallableStatement getcallable(Connection conn,String proc,String[] parm){
 51   CallableStatement cst=null;
 52   try{
 53    cst=conn.prepareCall(proc);
 54    SetParm(parm,cst);
 55   }catch(SQLException e){
 56    e.printStackTrace();
 57   }
 58   return cst;
 59  }
 60  /*
 61   * 执行sql语句,返回影响的记录数*
 62   */
 63  public Boolean ExecuteMysql(PreparedStatement pst){
 64   int re=0;
 65   try{
 66    re=pst.executeUpdate();
 67   }catch(Exception e){
 68    e.printStackTrace();
 69   }
 70   return re==0?false:true;
 71  }
 72  /*
 73   * 执行sql语句,返回结果集*
 74   */
 75  public ResultSet GetDatatable(PreparedStatement pst){
 76   ResultSet rspart=null;
 77   try{
 78    rspart=pst.executeQuery();
 79   }catch(Exception e){
 80    System.out.println(e);
 81   }
 82   return rspart;
 83  }
 84  /*
 85   * 执行查询存储过程*
 86   */
 87  public ResultSet Getcallparm(CallableStatement cst){
 88   ResultSet res=null;
 89   try{
 90    res=cst.executeQuery();
 91   }catch(Exception e){
 92    e.printStackTrace();
 93   }
 94   return res;
 95  }
 96  /*
 97   * 执行增删改存储过程*
 98   */
 99  public Boolean Getautocallparm(CallableStatement cst,Connection conn){
100   int t=0;
101   try{
102    t=cst.executeUpdate();
103    conn.commit();//统一提交
104   }catch(Exception e){
105    try{
106     conn.rollback();//操作失败,事务回滚
107    }catch(SQLException ex){
108     ex.printStackTrace();
109    }
110   }
111   return t==0?false:true;
112  }
113  /*
114   * 通过execute执行存储过程 返回影响的记录数*
115   */
116  public int ExecuteAuto(CallableStatement cst,Connection conn){
117   int i=0;
118   try{
119    i=cst.getUpdateCount();
120    conn.commit();
121   }catch(Exception e){
122    try{
123     e.printStackTrace();
124     conn.rollback();
125    }catch(SQLException ex){
126     ex.printStackTrace();
127    }
128   }
129   return i;
130  }
131  //使用execute()方法执行存储过程-可返回多个结果集- /*
132   * 使用execute执行存储过程,可返回多个结果集
133   * *
134   */
135  public ResultSet ExecuteSelect(CallableStatement cst,Connection conn){
136   ResultSet res=null;
137   try{
138    cst.execute();
139    res=cst.getResultSet();
140    conn.commit();
141   }catch(Exception e){
142    try{
143     e.printStackTrace();
144     conn.rollback();
145    }catch(SQLException ex){}
146   }
147   return res;
148  }
149  //设置存储过程的参数
150  public void SetParm(String[] parm,CallableStatement cst){
151   try{
152    if(parm.length>0){
153     for(int i=0;i<parm.length;i++){
154      String s=parm[i];
155      cst.setObject(i+1, s);
156     }
157    }
158   }catch(Exception e){
159    e.printStackTrace();
160   }
161  }
162  //关闭连接
163  public void closecon(Connection con){
164   try{
165    if(con!=null){
166     con.close();
167    }
168   }catch(SQLException e){
169    e.printStackTrace();
170   }
171  }
172  public void closepst(PreparedStatement pst){
173   try{
174    if(pst!=null){
175     pst.close();
176    }
177   }catch(SQLException e){
178    e.printStackTrace();
179   }
180  }
181  public void closecst(CallableStatement cst){
182   try{
183    if(cst!=null){
184     cst.close();
185    }
186   }catch(SQLException e){
187    e.printStackTrace();
188   }
189  }
190 }

 

 posted on 2015-11-13 14:13  HYRUI  阅读(681)  评论(0编辑  收藏  举报