example of well-written jdbc code

来源:互联网 发布:app网络请求错误 编辑:程序博客网 时间:2024/06/05 03:40
public Employee getEmployee(int id) throws SQLException{Employee employee=null;String sql="SELECT * FORM EMPLOYEE WHERE EMPLOYEE_NUMBER=?";Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;try{    conn=dataSource.getConnection();  ps=conn.prepareStatement(sql);  ps.setInt(1,id);  rs=ps.executeQuery();    while(rs.next())  {    employee=new Employee();employee.setId(rs.getInt("ID"));    employee.setEmployeeNumber(rs.getInt("EMPLOYEE_NUMBER"));    employee.setFirstName(rs.getString("FIRST_NAME");    employee.setLastName(rs.getString("LAST_NAME"));    employee.setTitle(rs.getString("TITLE"));     }} finally{ try{      if(rs!=null) rs.close();  } finally{    try{        if(ps!=null) ps.close();    } finally{    if(conn!=null) conn.close();  }  }}return employee;}

原创粉丝点击