数据库查询后怎么接收查询结果?

首先看个例子

第一步:先创建一个与数据库表对应的javabean类,例如创建一个宿舍楼信息表:

package com.stu.bean;

public class DormitoryBuildingInfo {
     
    private String dormitoryBuildingNo;
    private String buildingDate;
    private String dormitorySynopsis;
    public DormitoryBuildingInfo(String dormitoryBuildingNo, String buildingDate, String dormitorySynopsis) {
     
        super();
        this.dormitoryBuildingNo = dormitoryBuildingNo;
        this.buildingDate = buildingDate;
        this.dormitorySynopsis = dormitorySynopsis;
    }
    public String getDormitoryBuildingNo() {
     
        return dormitoryBuildingNo;
    }
    public void setDormitoryBuildingNo(String dormitoryBuildingNo) {
     
        this.dormitoryBuildingNo = dormitoryBuildingNo;
    }
    public String getBuildingDate() {
     
        return buildingDate;
    }
    public void setBuildingDate(String buildingDate) {
     
        this.buildingDate = buildingDate;
    }
    public String getDormitorySynopsis() {
     
        return dormitorySynopsis;
    }
    public void setDormitorySynopsis(String dormitorySynopsis) {
     
        this.dormitorySynopsis = dormitorySynopsis;
    }
    

}

查询宿舍楼信息的方法(可以在servlet写该方法)然后调用

public List<DormitoryBuildingInfo> listDormitoryBuildingNo() {
     
        List<DormitoryBuildingInfo> dBNo = new ArrayList<>();//创建一个List数组来存放查询后的值
        ConnectionDate cd = new ConnectionDate();
        conn = cd.getConn();
        sql = "select dormitory_building_no from dormitory_building_info";//sql语句
        try {
     
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            if(psmt!=null) {
     
               while(rs.next()) {
     
                   String dormitoryBuildingNo =rs.getString(1);//这里可以填与数据库相对应的属性名
                   DormitoryBuildingInfo d = new DormitoryBuildingInfo(dormitoryBuildingNo, rs.getString(2), rs.getString(3));
                   //需要注意的是,如果数据库对应属性的类型为其他类型,例如double 则要写成rs.getDouble(2)来获取查询后的值
                   dBNo.add(d);//把查询到的表的第一行数据存放到List数组内
               }
            }
            rs.close();
            psmt.close();
            cd.closeConn(conn);
        } catch (SQLException e) {
     
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return dBNo;
        
    }

怎么取出List里面的数据

for(DormitoryBuildingInfo d: dbNo){
     
    System.out.println(d.getDormitoryBuildingNo());
}

如何把数据在jsp页面显示呢

//首先,先在servlet里面 request.setAttribute("dBNo", dBNo); 
//第一参数代表设置请求属性的名字,第二个参数是要传给前端的值
//jsp页面接收
 <% List<DormitoryBuildingInfo> dbno = (List<DormitoryBuildingInfo>)request.getAttribute("dBNo"); %>
           <%for(DormitoryBuildingInfo dbi: dbno){
      %>
           <tr>
          <td><%= dbi.getDormitoryBuildingNo()%></td>
          <td><%= dbi.getBuildingDate()%></td>
          <td><%= dbi.getDormitorySynopsis()%></td>
          <tr>
          <%} %>
其他方法接收

其他方法:
DormitoryBuildingInfo[] dormitoryBuildingInfos = new DormitoryBuildingInfo[10];//这个方法不太好,建议不使用

你可能感兴趣的:(java,jsp,java,mysql,数据库)