package com.YY.util;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class BaseConnection { private static String conURL = ""; private static String cname = ""; private static String dbA = ""; private static String dbpassword = ""; static { try { //获取外部配置的数据库链接信息 InputStream ips =BaseConnection.class.getClassLoader().getResourceAsStream("MyJDBC.properties"); Properties props = new Properties(); try { props.load(ips); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { ips.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } conURL = props.getProperty("conURL"); cname = props.getProperty("cname"); dbA = props.getProperty("dbA"); dbpassword = props.getProperty("dbpassword"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // public static Connection getConnection() { Connection conn = null; try { Class.forName(cname); conn = DriverManager.getConnection(conURL, dbA, dbpassword); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void closeRes(Connection conn, PreparedStatement ps){ try { if ( conn != null ) { conn.close(); } if ( ps != null ) { ps.close(); } } catch ( Exception e ) { e.printStackTrace(); } } public static void closeRes(Connection conn, PreparedStatement ps,ResultSet rs){ try { if ( conn != null ) { conn.close(); } if ( ps != null ) { ps.close(); } if ( rs != null ) { rs.close(); } } catch ( Exception e ) { e.printStackTrace(); } }}package com.YY.util;import java.lang.reflect.Field;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;public class BaseDAO { //万能的获取全部内容 public static ArrayList getList(Class c1){ ArrayList ar = new ArrayList(); Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; ResultSet rs = null; String sql = "Select * from "+ c1.getSimpleName(); Field[] fi = c1.getDeclaredFields(); try{ ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ Object ob = c1.newInstance();//实例化类对象 for(Field ff :fi){ ff.setAccessible(true); ff.set(ob, rs.getObject(ff.getName())); } ar.add(ob); } }catch(Exception e){ e.printStackTrace(); }finally{ BaseConnection.closeRes(conn, ps, rs); } return ar; } //万能的获取ID主键的一条数据。 public static Object getObById(Class c1,int id){ Object ob = null; Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; ResultSet rs = null; Field[] fi = c1.getDeclaredFields(); String sql = "Select * from "+ c1.getSimpleName()+" where "+ fi[0].getName()+" = "+id; try{ ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ ob = c1.newInstance();//实例化类对象 for(Field ff :fi){ ff.setAccessible(true); ff.set(ob, rs.getObject(ff.getName())); } } }catch(Exception e){ e.printStackTrace(); }finally{ BaseConnection.closeRes(conn, ps, rs); } return ob; } //万能的获取Where 条件后 的数据。 public static ArrayList getList(Class c1,String whereSql){ ArrayList ar = new ArrayList(); Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; ResultSet rs = null; String sql = "Select * from "+ c1.getSimpleName() +" "+ whereSql; Field[] fi = c1.getDeclaredFields(); try{ ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ Object ob = c1.newInstance();//实例化类对象 for(Field ff :fi){ ff.setAccessible(true); ff.set(ob, rs.getObject(ff.getName())); } ar.add(ob); } }catch(Exception e){ e.printStackTrace(); }finally{ BaseConnection.closeRes(conn, ps, rs); } return ar; } //插入的万能方法。 public static boolean Insert(Object ob){ boolean f = false; Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; Class c1 = ob.getClass(); Field[] fi = c1.getDeclaredFields(); StringBuffer Sql = new StringBuffer(); StringBuffer Sql1 = new StringBuffer(); Sql.append("insert into ").append(c1.getSimpleName()).append(" ("); for(int i =1 ;i0){ f = true; } }catch(Exception e){ e.printStackTrace(); }finally{ BaseConnection.closeRes(conn, ps); } return f; } //万能更新 public static boolean update(Object ob){ boolean b = false; Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; Class c1 = ob.getClass(); Field[] fi = c1.getDeclaredFields(); StringBuffer sb = new StringBuffer(); sb.append("update ").append(c1.getSimpleName()).append(" set "); for(int i = 1; i 0){ b=true; } }catch(Exception e){ e.printStackTrace(); } return b; } //万能删除 public static boolean delete(Class c1 ,int id){ boolean b = false; Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; Field[] fi = c1.getDeclaredFields(); String Sql = "Delete from "+c1.getSimpleName()+" Where "+fi[0].getName()+" = ?"; try{ ps = conn.prepareStatement(Sql); ps.setObject(1, id); int a = ps.executeUpdate(); if(a>0){ b=true; } }catch(Exception e){ e.printStackTrace(); }finally{ BaseConnection.closeRes(conn, ps); } return b; } //万能删除 public static boolean delete(Class c1 ,String WhereSql){ boolean b = false; Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; Field[] fi = c1.getDeclaredFields(); String Sql = "Delete from "+c1.getSimpleName()+" "+WhereSql; try{ ps = conn.prepareStatement(Sql); int a = ps.executeUpdate(); if(a>0){ b=true; } }catch(Exception e){ e.printStackTrace(); }finally{ BaseConnection.closeRes(conn, ps); } return b; }}