关于arraylist,就助各位一下

public class HandleEmp {

     public ArrayList<E> selectEmp(String up) {
         try {
             Class.forName("com.mysql.jdbc.Driver");
             Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/empsys", "root", "root");
             Statement stm=connection.createStatement();
             String sqlString="select * from users where 1=1";
             if(!up.equals(""))
                 sqlString=sqlString+" and username like '%"+up+"%'";
             ResultSet rs=stm.executeQuery(sqlString);
             ArrayList<Emp> arrayList=new ArrayList<Emp>();
             while(rs.next()) {
                 Emp e=new Emp();
                 e.setId(rs.getInt("id"));
                 e.setUsername(rs.getString("username"));
                 e.setPass(rs.getString("pass"));
                 arrayList.add(e);
             }
             return arrayList;
         }catch(Exception e) {
             e.printStackTrace();
             return null;
         }
     }
}

上面是代码

我想问一下这 public ArrayList selectEmp(String up)为什么要用ArrayList呢

因为你需要查询emp的结果并返回。
返回类型为ArrayList,这个方法的作用就是查询emp的集合数据,然后将查询到的集合数据返回给调用的地方。

返回数据可能多条,用可变数组 ArrayList