我想在java中显示从数据库获取值的记录,其中id = id(从getter方法中将值插入数据库不起作用)

时间:2017-08-21 16:54:01

标签: java mysql jsp

我创建一个java文件名Attend.java

public class Attend {
String attend_id,studname,owner;

public String getOwner() {
    return owner;
}

public void setOwner(String owner) {
    this.owner = owner;

}public String getStudname() {
    return Studname;
}

public void setStudname(String Studname) {
    this.Studname = Studname;
}

public String getAttend_id() {
    return attend_id;
}

public void setAttend_id(String attend_id) {
    this.attend_id = attend_id;
}} ;

其他文件DAO文件AttendDao.java

, public class AttendDao {public static List<Attend> getAllRecords(){
    List<Attend> list=new ArrayList<Attend>();
        //Attend u1=null; 
    try{
        Connection con=DbConnector.getConnection();
        PreparedStatement ps=con.prepareStatement("select attend_id,studname,owner from attend_detail where owner=?");
        Attend u1 = null;
        //System.out.println(u1.getOwner());
        ps.setString(1,u1.getOwner());
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            Attend u=new Attend();
            u.setAttend_id(rs.getString("attend_id"));
            u.setStudname(rs.getString("studname"));
            u.setOwner(rs.getString("owner"));
            list.add(u);
        }
    }catch(SQLException e1){System.out.println("sql error:"+e1);
    }catch(Exception e){System.out.println(e);}
    return list;
    //}
}},

如果owner =&#34; xyz@xyz.com"我想从数据库中获取此所有者值那么它只会显示行数据

这是我的数据库及其值..数据库名称Attend_detail。

attend_id  studname     owner 
ATN001     abcd       xyz@xyz.com
ATN002     efgh       null       

这是我的jsp view.jsp

<body>
<%@page import="com.attend.detail.AttendDao,com.attend.detail.Attend,java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div class="container">

<h1>Attendance List</h1>

<%
 List<Attend> list=AttendDao.getAllRecords();
 request.setAttribute("list",list);
 %>

 <div class="table-responsive" width="100%">
 <table id="myTable" class="display table table-bordered" width="99%" >
 <thead>  
      <tr>  
        <th>Attend_id</th>  
        <th>Student_id</th>   
        <th>owner</th>
      </tr>  
    </thead>  
    <tbody>
    <c:forEach items="${list}" var="u">  
      <tr>  
        <td>${u.getAttend_id()}</td>  
        <td>${u.getStudentid()}</td> 
        <td>${u.getOwner()}</td>  
      </tr> 
      </c:forEach> 
    </tbody>  

    </table>
    </div></body>

它显示NullpointerException 怎么解决..请rply。

1 个答案:

答案 0 :(得分:0)

您需要在Attend u1 = null之后指定一些值,该值会使用null进行初始化。

    Attend u1 = null; 
    //System.out.println(u1.getOwner());
    ps.setString(1,u1.getOwner());

所以,基本上,u1null,您需要指定Attend个对象才能解决此问题。