方法中finally返回null
记录一个学习IDEA操作数据库中遇到的小bug
finally中有return null 那么前面返回的数据或对象就会变成null
方法:
package?dao.impl;
import?bean.Student;
import?bean.Teacher;
import?dao.TeacherDao;
import?java.sql.*;
import?java.util.ArrayList;
import?java.util.List;
public?class?TeacherDaoImpl?implements?TeacherDao?{
????@Override
????public?Teacher?getById(int?id)?{
????????Connection?connection?=?null;
????????PreparedStatement?pps?=?null;
????????ResultSet?resultSet?=?null;
????????try?{
????????????//1.加载驱动
????????????Class.forName("com.mysql.cj.jdbc.Driver");
????????????//2.获得链接
????????????String?userName?=?"root";
????????????String?password?=?"123456";
????????????String?url?=?"jdbc:mysql://localhost:3306/yhp2?serverTimezone=UTC";
????????????connection?=?DriverManager.getConnection(url,?userName,?password);
????????????//3.定义sql,创建状态通道(进行sql语句的发送)
????????????String?sql?=?"select?*?from?student?s,teacher?t?where?s.teacherid=t.teacherid?and?s.teacherid=?";
????????????pps?=?connection.prepareStatement(sql);
????????????pps.setInt(1,?id);
????????????//4.取出结果集信息
????????????/*while?(resultSet.next()){???//判断是否有下一条数据
????????????????//取出数据:?resultSet.getXXX("列明")?XXX是数据类型
????????????????System.out.println(resultSet.getString("name")+"的职称是"+resultSet.getString("title"));
????????????}*/
????????????List<Student>?students?=?new?ArrayList<>();
????????????Teacher?teacher?=?new?Teacher();
????????????resultSet?=?pps.executeQuery();
????????????while?(resultSet.next())?{
????????????????//1.获得教师信息
????????????????teacher.settName(resultSet.getString("tname"));
????????????????teacher.setTeacherId(resultSet.getInt("teacherid"));
????????????????//2.获得学生信息
????????????????Student?student?=?new?Student();
????????????????student.setStuName(resultSet.getString("stuname"));
????????????????student.setStuId(resultSet.getInt("stuid"));
????????????????students.add(student);
????????????}
????????????teacher.setStudents(students);
????????????return?teacher;
????????}?catch?(ClassNotFoundException?e)?{
????????????e.printStackTrace();
????????}?catch?(SQLException?throwables)?{
????????????throwables.printStackTrace();
????????}?finally?{
????????????try?{
????????????????if?(connection?!=?null)?{
????????????????????connection.close();
????????????????}
????????????????if?(pps?!=?null)?{
????????????????????pps.close();
????????????????}
????????????????/*if?(resultSet?!=?null)?{
????????????????????resultSet.close();
????????????????}*/
????????????}?catch?(SQLException?throwables)?{
????????????????throwables.printStackTrace();
????????????}
???????????//return?null;
???????????//开始我的null在这的,测试的时候一直找不到bug,把方法的里代码放到新的Demo里发没问题,应该是方法不知道怎么返回了null,就找到了这个问题
????????}
????????return?null;
????}
}
测试方法
package?com;
import?bean.Student;
import?bean.Teacher;
import?dao.impl.TeacherDaoImpl;
public?class?Demo6?{
????public?static?void?main(String[]?args)?{
????????TeacherDaoImpl?dao?=?new?TeacherDaoImpl();
????????Teacher?teacher?=?dao.getById(2);
????????if?(teacher?==?null)?{
????????????System.out.println("没有这个教师");
????????????return;
????????}
????????System.out.println(teacher.gettName());
????????for(Student?s?:?teacher.getStudents()){
????????????System.out.println(s.getStuName());
????????}
????}
}
|