/** * @param args */ public static void main(String[] args) { String sql="select * from user where name=1"; try { User user=BaseDao.getClass(sql, User.class); System.out.println(user.getName()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 使用泛型类封装查询结果 * @param * @throws Exception */ public static T getClass(String sql, Class clazz) throws Exception { PreparedStatement ps = null; ResultSet rs = null; Connection con=JDBCUtil.getConnection(); ps=con.prepareStatement(sql); rs=ps.executeQuery(); //结果集的字段 ResultSetMetaData md=rs.getMetaData(); //将字段添加到string数组 String[] mdString=new String[md.getColumnCount()]; for(int i=0;i