Home > Database > Mysql Tutorial > 网上发现的一个不错的使用jdbc进行增删改查的封装类,(没有测试,

网上发现的一个不错的使用jdbc进行增删改查的封装类,(没有测试,

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:55:42
Original
1169 people have browsed it

package com.sj.utils;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.S

package com.sj.utils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

public class BaseDao {
	/**
	*查询方法
	*/
	public static List findAll(Object obj,Connection conn) throws Exception{
		Class clazz=obj.getClass();
		//获取传入的实体中的所有的方法
		Method[] m=clazz.getMethods();
		//获取传入实体中的所有的属性
		Field[] f=clazz.getDeclaredFields();
		//建立结果集List接收对象
		List list=new ArrayList();
		//创建sql语句
		String sql="select * from  "+obj.getClass().getSimpleName().toLowerCase();
		System.out.println(sql);
		//System.out.println(sql);
		//预编译sql语句
		PreparedStatement pst=conn.prepareStatement(sql);
		//执行预编译的语句,获取结果集
		ResultSet rs=pst.executeQuery();
		//从结果集中循环取出放入结果集List
		while(rs.next()){
			Object obj2=clazz.newInstance();
			
			for(int i=0;i<f.length;i++){
				//取出属性名
				String n=f[i].getName();
				//获取结果集的单个的对象
				Object ob=rs.getObject(i+1);
				if(ob==null){
					ob="a";//防止数据为null时引发空指针异常
				}
				//判断单个结果的对象类型
				if(ob.getClass().equals(java.lang.String.class)){
					
					/*
					 * 拼接Set方法。
					 */
					for(int j=0;j<m.length;j++){
						if(("set"+n).equalsIgnoreCase(m[j].getName())){
							//执行指定的对象中的方法,并肩参数传入
							m[j].invoke(obj2, rs.getString(n));
						}
					}
					
				}else{
					for(int j=0;j<m.length;j++){
						if(("set"+n).equalsIgnoreCase(m[j].getName())){
							m[j].invoke(obj2, rs.getInt(n));
						}
					}
				}
				
			}
			list.add(obj2);
			
		}
		
		return list;
	}
	
	/**
	*保存方法
	*/
	public static void save(Object obj,Connection conn) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException{
		Class clazz=obj.getClass();
		Method[] m=clazz.getDeclaredMethods();
		Field[] f=clazz.getDeclaredFields();

		String table=clazz.getSimpleName().toLowerCase();

		String sql="";
		for(int i=0;i<f.length;i++){
			sql=sql+f[i].getName()+",";
		}
		sql=sql.substring(0, sql.length()-1);
		String sql2="";
	
		for(int j=0;j<f.length;j++){
			
			String str="get"+f[j].getName();
			
			for(int k=0;k<m.length;k++){
				
				if(str.equalsIgnoreCase(m[k].getName())){
					//判断。这个地方有bug
					/*
					 * new 出来到对象没有这个方法需要的属性
					 */
			
					Object p=m[k].invoke(obj);
			
					if(p==null){
						p="null";
						sql2=sql2+p+",";
					}else{
					sql2=sql2+"&#39;"+p+"&#39;"+",";
					}
				}
			}	
			
		}
		sql2=sql2.substring(0,sql2.length()-1);
		sql="insert into "+table+"("+sql+")"+" values"+"("+sql2+")";
		System.out.println(sql);
		PreparedStatement pst=conn.prepareStatement(sql);
		pst.executeUpdate();
	}	
	
	/**
	*更新数据
	*/
	public static void update(Object obj,Connection conn) throws Exception{
        //修改
        Class clazz=obj.getClass();
        Method[] m=clazz.getMethods();
        Field[] f=clazz.getDeclaredFields();
        /*
         * 拼接Sql
         */
        String str1="";
        String str3="";
        for(int i=0;i<f.length;i++){
            //str1=str1+","+f[i].getName()+"="+"?";
            for(int j=0;j<m.length;j++){
                String str2="get"+f[i].getName();
                if(str2.equalsIgnoreCase(m[j].getName())){
                    str1=str1+f[i].getName()+"="+"&#39;"+m[j].invoke(obj)+"&#39;"+",";
                    if("getId".equalsIgnoreCase(m[j].getName())){
                     str3=m[j].invoke(obj).toString();
                    }
                }
            }
        }
        str1=str1.substring(0,str1.length()-1);
        String sql="update "+clazz.getSimpleName().toLowerCase()+" set "+str1+" where id="+"&#39;"+str3+"&#39;";
        System.out.println(sql);
      
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.execute();
       
    }
	
	/**
	*根据id删除数据
	*/
	public static void delById(Object obj,String id,Connection conn) throws SQLException{
		 Class clazz=obj.getClass();
		 String table=clazz.getSimpleName();
		 String sql="delete from "+table+" where id="+"&#39;"+id+"&#39;";
		 System.out.println(sql);
		 conn.prepareStatement(sql).executeUpdate();
	}
	
}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template