封装数据库方法

  1 package com.eshop.dao;
  2 
  3 import java.lang.reflect.Field;
  4 import java.lang.reflect.InvocationTargetException;
  5 import java.lang.reflect.Method;
  6 import java.sql.Connection;
  7 import java.sql.DriverManager;
  8 import java.sql.PreparedStatement;
  9 import java.sql.ResultSet;
 10 import java.sql.SQLException;
 11 import java.util.ArrayList;
 12 import java.util.List;
 13 
 14 
 15 public class BaseDao {
 16     private final String URL="jdbc:oracle:thin:@localhost:1521:orcl";//数据库连接
 17     private final String dRIVERString="oracle.jdbc.driver.OracleDriver";
 18     private final String USER="eshop";
 19     private final String PASSWORD="tiger";
 20     
 21     Connection conn=null;
 22     PreparedStatement pstmt =null;
 23     
 24     public void getConnection(){
 25         try {
 26             //1.加载驱动
 27             Class.forName(dRIVERString);
 28             //2.连接数据库  
 29             //只建立一次连接
 30             //问题:如果多个人同时进入数据库(待解决多线程的问题)
 31             if(conn==null){
 32                  conn= DriverManager.getConnection(URL,USER,PASSWORD);
 33             }
 34         
 35         } catch (Exception e) {
 36             // TODO Auto-generated catch block
 37             e.printStackTrace();
 38         }
 39     }
 40     
 41     //封装增删改的方法,返回受影响的行数
 42     public int executeUpdate(String sql,List<Object> parm){
 43         getConnection();
 44         int count=0;
 45         try {
 46             pstmt = conn.prepareStatement(sql) ;
 47             if(parm!=null){
 48                 for(int i=0;i<parm.size();i++){
 49                     pstmt.setObject(i+1, parm.get(i));
 50                 }
 51             }
 52             //执行
 53             count=pstmt.executeUpdate();
 54             
 55         } catch (SQLException e) {
 56             // TODO Auto-generated catch block
 57             e.printStackTrace();
 58         }
 59         return count;
 60         
 61     }
 62     
 63     //封装查询的方法,获取结果集
 64     public ResultSet executeQuery(String sql,List<Object> parm){
 65         getConnection();
 66         try {
 67             PreparedStatement pstmt = conn.prepareStatement(sql) ;
 68             if(parm!=null){
 69                 for(int i=0;i<parm.size();i++){
 70                     pstmt.setObject(i+1, parm.get(i));
 71                 }
 72             }
 73             //执行
 74             ResultSet res=pstmt.executeQuery();
 75             return res;
 76             
 77         } catch (SQLException e) {
 78             // TODO Auto-generated catch block
 79             e.printStackTrace();
 80         }
 81         return null;
 82         
 83     }
 84     public  <T> List<T>  findModelListBySqlAndParam(String sql,List<Object> param,T t) {
 85         //定义一个集合来存放需要转成的对象集合
 86         List<T> list=new ArrayList<T>();
 87         //获取当前类
 88         Class<?> c=t.getClass();
 89         //遍历结果集,封装成外界用户所需要的对象集合
 90         //1>获取结果集
 91         ResultSet rs=executeQuery(sql, param);
 92         //2>开始遍历
 93         try {
 94             while(rs.next()){
 95                 //初始化对象
 96                 @SuppressWarnings("unchecked")
 97                 T obj= (T)c.newInstance();
 98                 //获取当前类一共多少个属性啊
 99                 Field[] fields=c.getDeclaredFields();
100                 for(Field f:fields){
101                     //获取当前属性的属性名子
102                     String fname=f.getName();
103                     //获取当前的属性的类型(简称)  java.lang.String
104                     String type=f.getType().getSimpleName();
105 
106                     //*****************  取出来当前属性对应的数据库的值了 ****************
107                     //在此方法名中要求类的属性名和数据库的字段名相同
108                     Object value=null;
109                     if(type.equalsIgnoreCase("string")){
110                         value=rs.getString(fname);
111                     }else if(type.equalsIgnoreCase("int")){
112                         value=rs.getInt(fname);
113                     }else if(type.equalsIgnoreCase("Integer")){
114                         value=rs.getInt(fname);
115                     }else if(type.equalsIgnoreCase("Double")){
116                         value=rs.getDouble(fname);
117                     }else if(type.equalsIgnoreCase("Float")){
118                         value=rs.getFloat(fname);
119                     }else if(type.equalsIgnoreCase("date")){
120                         value=rs.getDate(fname);
121                     }else if(type.equalsIgnoreCase("long")){
122                         value=rs.getLong(fname);
123                     }
124                     //*****************  将取出来当前属性的值设置给当前对象obj****************
125                     //1>获取当前对象的所有set方法,并找到当前取出来的属性对应的set方法
126                     Method[] methods=c.getDeclaredMethods();//获取所有的方法
127                     for(Method m:methods){
128                         //获取当前方法名
129                         String methodName=m.getName();
130                         //判断是不是当前属性
131                         if(methodName.equalsIgnoreCase("set"+fname)){
132                             //执行该方法
133                             m.invoke(obj, value);
134                         }
135                     }
136                 }
137                 list.add(obj);
138             }
139         } catch (InstantiationException e) {
140             // TODO Auto-generated catch block
141             e.printStackTrace();
142         } catch (IllegalAccessException e) {
143             // TODO Auto-generated catch block
144             e.printStackTrace();
145         } catch (SecurityException e) {
146             // TODO Auto-generated catch block
147             e.printStackTrace();
148         } catch (IllegalArgumentException e) {
149             // TODO Auto-generated catch block
150             e.printStackTrace();
151         } catch (InvocationTargetException e) {
152             // TODO Auto-generated catch block
153             e.printStackTrace();
154         } catch (SQLException e) {
155             // TODO Auto-generated catch block
156             e.printStackTrace();
157         }
158         return list;
159     }
160     
161     
162     
163     //获取订单编号
164     public int getSequenceIndex(String seq){
165         String sql="select "+seq+".nextval num from dual";
166         getConnection();
167         ResultSet rs=executeQuery(sql, null);
168         try {
169             if(rs.next()){
170                 return rs.getInt("num");
171             }
172         } catch (SQLException e) {
173             // TODO Auto-generated catch block
174             e.printStackTrace();
175         }
176         
177         return 0;
178         
179     }
180 
181     
182     
183         
184     //关闭所有资源
185     public void closeAll(){
186         try {
187             if(pstmt!=null){
188                 pstmt.close();
189                 pstmt=null;//促进资源快速回收
190                 }
191             if(conn!=null) conn.close();
192             
193             
194         } catch (SQLException e) {
195             // TODO Auto-generated catch block
196             e.printStackTrace();
197         }
198     }
199     
200 }
原文地址:https://www.cnblogs.com/ztt0918/p/8854846.html