public class JDBC {
	  private static Connection conn=null;
	    static{
		      try {
			        Class.forName("com.mysql.jdbc.Driver");
		      } catch (ClassNotFoundException e) {
			        e.printStackTrace();
		      }
	    }
	  public Connection getConnection(){
		    try {
			      conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/calendar","root","root");
		    } catch (SQLException e) {
			      e.printStackTrace();
		    }
		      return conn;
	    }
	  public void closeConn(Connection conn,PreparedStatement ps,ResultSet rs){
		    try {
			      if(rs!=null){
				        rs.close();
				        rs=null;
			      }
			      if(ps!=null){
				        ps.close();
				        ps=null;
			      }
			      if(conn!=null){
				        conn.close();
				        conn=null;
			      }
		      } catch (SQLException e) {
			        e.printStackTrace();
		      }
	  }
  //查询方法返回对象集合
	  public <T>List<T> qeury(String sql,List<Object> lp,Class clazz){
		    List<T> list=new ArrayList<>();
		    Connection conn=getConnection();
		    PreparedStatement ps=null;
		    ResultSet rs=null;
		    if(conn!=null){
			      try {
				        ps=conn.prepareStatement(sql);
				        if(lp.size()>0){
					          for(int i=0;i<lp.size();i++){
						            ps.setObject(i+1, lp.get(i));
					          }
				        }
				        rs=ps.executeQuery();
				        while(rs.next()){
					          Object obj=clazz.newInstance();
					          ResultSetMetaData rm=rs.getMetaData();
					          int num=rm.getColumnCount();
					          if(num>0){
						            for(int i=1;i<=num;i++){
							              String name=rm.getColumnName(i);
							              Field f=clazz.getDeclaredField(name);
							              f.setAccessible(true);
							              Object o=rs.getObject(i);
							              f.set(obj, o);
						            }
					          }
					          list.add((T) obj);
				        }
			      } catch (SQLException e) {
        e.printStackTrace();
			      } catch (InstantiationException e) {
        e.printStackTrace();
			      } catch (IllegalAccessException e) {
        e.printStackTrace();
			      } catch (NoSuchFieldException e) {
        e.printStackTrace();
			      } catch (SecurityException e) {
        e.printStackTrace();
			      }finally{
				        closeConn(conn, ps, rs);
			      }
		    }
		    return list;
	  }
  //更新方法(增、删、改)返回布尔类型
	  public boolean update(String sql,List<Object> lp){
		    boolean isFlag=false;
		    Connection conn=getConnection();
		    PreparedStatement ps=null;
		    ResultSet rs=null;
		    if(conn!=null){
			      try {
				        ps=conn.prepareStatement(sql);
				        if(lp.size()>0){
					          for(int i=0;i<lp.size();i++){
						            ps.setObject(i+1, lp.get(i));
					          }
				        }
				        int num=ps.executeUpdate();
				        if(num>0){
					          isFlag=true;
				        }
			      } catch (SQLException e) {
				        e.printStackTrace();
			      }  catch (SecurityException e) {
				        e.printStackTrace();
			      }finally{
				        closeConn(conn, ps, rs);
			      }
		    }
		    return isFlag;
	  }
}
2019-03-13
原文:https://www.cnblogs.com/li19941999/p/10520574.html