java查询结果的处理
为了方便使用sql的数据,在查询后mysql会把数据封装到java里面的resultset对象,然后我们需要把resultset里的数据再封装到我们自己创建的对象中,比如Student对象:
public class Student {int number;String name;String gender;Date birthday;String phone;double height;Timestamp reg_time;public void setNumber(int number) {this.number = number;}public void setName(String name) {this.name = name;}public void setGender(String gender) {this.gender = gender;}public void setBirthday(Date birthday) {this.birthday = birthday;}public void setPhone(String phone) {this.phone = phone;}public void setHeight(double height) {this.height = height;}public void setReg_time(Timestamp reg_time) {this.reg_time = reg_time;}public int getNumber() {return number;}public String getName() {return name;}public String getGender() {return gender;}public Date getBirthday() {return birthday;}public String getPhone() {return phone;}public double getHeight() {return height;}public Timestamp getReg_time() {return reg_time;}@Overridepublic String toString() {return "Student{" +"number=" + number +", name=" + name +", gender=" + gender +", birthday=" + birthday +", phone=" + phone +", height=" + height +", reg_time=" + reg_time +'}';}
}
把数据从resultset转到student中要使用rs的get的一系列函数,可以有getint,getstring等等,它的参数是对应的sql的列
public static Student findStudentByNumber(int number) throws SQLException {DriverManager.registerDriver(new Driver());String url="jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai";String user="root"; //账户String psd="root"; //密码//建立与数据库的连接Connection connection=DriverManager.getConnection(url,user,psd);PreparedStatement ps=connection.prepareStatement("select number,name,gender,birthday,height,phone,reg_time from student where number=?");ps.setObject(1,number);//换行查询后,mysql将数据封装到一个resultset对象中ResultSet rs=ps.executeQuery();////将resultset中的数据重新包装到我没自己的对象Student中,使用起来更方便Student student=new Student();while(rs.next()){ //查看resultset集合中下一组数据存在与否,存在则返回true,循环继续student.setNumber(rs.getInt("number"));student.setName(rs.getString("name"));student.setGender(rs.getString("gender"));student.setBirthday(rs.getDate("birthday"));student.setHeight(rs.getDouble("height"));student.setPhone(rs.getString("phone"));student.setReg_time(rs.getTimestamp("reg_time"));}return student;}
上面只是放在一个student中,然而会有很多的student所以要创建一个student数组,返回的时候返回student数组。
ArrayList students = new ArrayList();while (rs.next()) { //查看resultset集合中下一组数据存在与否,存在则返回true,循环继续Student student = new Student(); //放在循环里可以保证每次set的是不同的studentstudent.setNumber(rs.getInt("number"));student.setName(rs.getString("name"));student.setGender(rs.getString("gender"));student.setBirthday(rs.getDate("birthday"));student.setHeight(rs.getDouble("height"));student.setPhone(rs.getString("phone"));student.setReg_time(rs.getTimestamp("reg_time"));students.add(student);}
students放在循环外面,student类放在循环里面,每次在循环里新创建一个student对象然后添加进students里。
如果要检查sql中是否存在某个数据,需要设置一个返回值boolean的方法,返回的时候返回resultset.next()这个方法把resultset指向第一组数据,再次调用会指向第二组数据,以此类推,然后返回当前组数据是否存在
private static boolean checkNum(int number) throws SQLException {DriverManager.registerDriver(new Driver());String url="jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai";String user="root"; //账户String psd="root"; //密码//建立与数据库的连接Connection connection=DriverManager.getConnection(url,user,psd);PreparedStatement ps=connection.prepareStatement("select number from student where number = ?");ps.setObject(1,number);ResultSet resultSet=ps.executeQuery();return resultSet.next();}
也可以改变查询方法,比如查询number出现的次数count(*) as c,大于等于1说明number存在于这个表中,为0的话说明不存在,返回值返回rs.getint(“c”)
private static int checkNum(int number) throws SQLException {DriverManager.registerDriver(new Driver());String url="jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai";String user="root"; //账户String psd="root"; //密码//建立与数据库的连接Connection connection=DriverManager.getConnection(url,user,psd);PreparedStatement ps=connection.prepareStatement("select count(*) c from student where number = ?");ps.setObject(1,number);ResultSet resultSet=ps.executeQuery();resultSet.next();return resultSet.getInt("c");}
但是next方法是必须要加上的,必须要将指针指向rs的第一组数据