(笔记) 在JDBC中用JAVA查询单条记录

public static Department getById(int department_id){
       Connection con=DMmanager.getConnection();
       PreparedStatement pst= null;
       String str="select * from department where department_id =?";
       pst=con.prepareStatement(str);
       (1)因为此处有一展位符,所以????
       pst.setInt(1,department_id);

       ResultSet rs=null;
       rs = pst.executeQuery();   
   
       while(rs.hasNext){
       Department dep = new Department();
       dep.setId(rs.getInt("department_id"));
       dep.setName(rs.getString("department_name"));
       dep.setSalary(rs.getFloat("salary"));
       dep.setLocation_id(rs.getInt("location_id"));
       }
       rs.close();
       pst.close();
       con.close();
       return dep;
}

与查询department所有数据相比,为什么在简单数据查询时会要有(1)在了,其作用是什么?

ps:此代码有出入.....只用于做解构式模块分析

你可能感兴趣的:(java,jdbc)