将行数据从数据库获取到JSP

时间:2015-02-15 12:56:14

标签: java mysql jsp

我试图使用连接显示数据库中的数据。我设法显示第一个表,但在尝试显示其他数据时出现空异常错误。我是通过Java类做到这一点的。

这是函数......

public List<administration> fullList() throws Exception{

    List<administration> result = new ArrayList<administration>();

    try{
        Class.forName("com.mysql.jdbc.Driver");
        connectMe = DriverManager.getConnection(url+dbName, userNameDB, passwordDB);

        String query = "SELECT  \n" + 
                " ControlAccess.UserName, \n" +
                " ControlAccess.Pass, \n" +
                " Users.First_Name,\n" +
                " Users.Last_Name, \n" +
                " UserInfo.Age, \n" +
                " UserInfo.Country,\n" +
                " UserInfo.Address,\n" +
                " UserInfo.ZipCode,\n" +
                " Sessions.Matrix1,\n" +
                " Sessions.Matrix2,\n" +
                " Sessions.Result,\n" +
                " FilePath.LocationFiles\n" +
                " FROM MatrixUsers.UserInfo \n" +
                " INNER JOIN MatrixUsers.Users\n" +
                " ON UserInfo.idUserInfo = Users.idUsers\n" +
                " INNER JOIN MatrixUsers.ControlAccess \n" +
                " ON ControlAccess.idControlAccess = UserInfo.idUserInfo\n" +
                " INNER JOIN MatrixUsers.Sessions \n" +
                " ON Sessions.idSessions = ControlAccess.idControlAccess\n" +
                " INNER JOIN MatrixUsers.FilePath \n" +
                " ON FilePath.idFilePath = Sessions.idSessions";

        selectUsers = connectMe.prepareStatement(query);
        results = selectUsers.executeQuery();

        while(results.next()) {

            administration admin = new administration();

            admin.setUserName(results.getString("UserName"));
            admin.setPassword(results.getString("Pass"));
            admin.setFirstname(results.getString("First_Name"));
            admin.setLastname(results.getString("Last_Name"));
            admin.setAge(results.getInt("Age"));
            admin.setCountry(results.getString("Country"));
            admin.setAddress(results.getString("Address"));
            admin.setZipcode(results.getInt("ZipCode"));
            admin.setMatrix1(results.getString("Matrix1"));
            admin.setMatrix2(results.getString("Matrix2"));
            admin.setResult(results.getString("Result"));
            admin.setLocation(results.getString("LocationFiles"));
            result.add(admin);

        }

       results.close();
       connectMe.close();

    }catch(Exception e) {
        e.printStackTrace();
    }

    return result;
}

以下是套装和获取:

 public String getUsername() {
    return username;    
}

public String getPassword() {
    return password;
}

public String getFirstname() {
    return firstName;
}

public String getLastname() {
    return lastName;
}

public int getAge() {
    return age;
}

public String getCountry() {
    return country;
}

public String getAddress() {
    return address;
}

public int getZipcode() {
    return zipcode;
}

public String getMatrix1() {
    return Matrix1;
}

public String getMatrix2() {
    return Matrix2;
}

public String getResult() {
    return Result;
}

public String getLocation() {
    return location;
}

public int getID() {
    return id;
}

public void setUserName(String aUser) {
    username = aUser;
}

public void setPassword(String aPass) {
    password = aPass;
}

public void setFirstname(String aFirstname) {
    firstName = aFirstname;
}

public void setLastname(String aLastname) {
    lastName = aLastname;
}

public void setAge(int anAge) {
    age = anAge;
}

public void setCountry(String aCountry) {
    country = aCountry;
}

public void setAddress(String anAddress) {
    address = anAddress; 
}

public void setZipcode(int aZipcode) {
    zipcode = aZipcode;
}

public void setMatrix1(String aMatrix) {
    Matrix1 = aMatrix;
}

public void setMatrix2(String aMatrix) {
    Matrix2 = aMatrix;
}

public void setResult(String aResult) {
    Result = aResult;
}

public void setLocation(String aLocation) {
    location = aLocation;
}

我在JSP中使用迭代显示。您需要的任何额外信息!我会看看自己是否能找到错误。另外,我为巨大的查询语句,分配的一部分及其必要条件道歉。

JSP代码:

<table>
        <c:forEach var="admin" items="${result}">
            <tr>

                <td><c:out value = "${admin.username}" /> </td>
                <td>${admin.password} </td>
                <td>${admin.firstName} </td>
                <td>${admin.lastName} </td>
                <td>${admin.age} </td>
                <td>${admin.country} </td>
                <td>${admin.address} </td>
                <td>${admin.zipcode} </td>
                <td>${admin.Matrix1} </td>
                <td>${admin.Matrix2} </td>
                <td>${admin.Result} </td>
                <td>${admin.location} </td>
            </tr>


        </c:forEach>

    </table>

堆栈错误是:

javax.el.PropertyNotFoundException: Property 'firstName' not found on type matrixcalculator.administration
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:229)
javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:206)
javax.el.BeanELResolver.property(BeanELResolver.java:317)
javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:182)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
org.apache.jsp.showFullList_jsp._jspx_meth_c_005fforEach_005f0(showFullList_jsp.java:149)
org.apache.jsp.showFullList_jsp._jspService(showFullList_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:750)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:720)
org.apache.jsp.processInfo_jsp._jspService(processInfo_jsp.java:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

-Silvestrini

2 个答案:

答案 0 :(得分:1)

异常消息非常明确:

  

在类型matrixcalculator.administration

上找不到属性'firstName'

这意味着类matrixcalculator.administration中没有具有以下签名的方法:

public String getFirstName()

答案 1 :(得分:1)

  

javax.el.PropertyNotFoundException:找不到属性'firstName'   type matrixcalculator.administration

这只意味着给定的类没有以下方法:

public SomeObject getFirstName() {
    return firstName;

如果此方法存在,请确保签名与public完全相同,然后执行no arguments

<强>更新

问题是你的getterName不在camelCase而你的变量名在camelCase中

public String getFirstname() {
    return firstName;
}

将其更改为

public String getFirstName() {  // N should be capital
    return firstName;
}