【问题标题】:Arraylist + database + servlet + DAOArraylist + 数据库 + servlet + DAO
【发布时间】:2011-12-22 16:07:29
【问题描述】:

您好,我是 hava 新手,我在查看 JSP 页面中的数组列表中的记录时遇到问题, 每当我加载页面时,我都会得到: [content.animalBean@1e8614a, content.animalBean@14b52aa, content.animalBean@2026f3, content.animalBean@dd20b6, content.animalBean@18eb00c] 1 不是数据库记录 这是我的代码: 选择AnimalServlet:

package content;

import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class selectAnimalServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
                       throws ServletException, java.io.IOException {

try
{       

    List<animalBean> beans = DAO.selectListAnimal(); 
    request.setAttribute("beans", beans); 
    request.getRequestDispatcher("checkAnimal.jsp").forward(request, response); 


}

catch (Throwable theException)      
{
     System.out.println(theException); 
}
       }
    }

动物豆:

package content;

public class animalBean {

    private String animalName;
    private String animalDob;
    private String animalGender;
    private String animalSource;
    private String animalBreed;
    private String animalRemark;

    public String getAnimalName() {return animalName;}
    public String getAnimalDob() {return animalDob;}
    public String getAnimalGender() {return animalGender;}
    public String getAnimalSource() {return animalSource;}
    public String getAnimalBreed() {return animalBreed;}
    public String getAnimalRemark() {return animalRemark;}

    public void setAnimalName(String animalName) {this.animalName = animalName;}
    public void setAnimalDob(String animalDob) {this.animalDob = animalDob;}
    public void setAnimalGender(String animalGender) {this.animalGender = animalGender;}
    public void setAnimalSource(String animalSource) {this.animalSource = animalSource;}
    public void setAnimalBreed(String animalBreed) {this.animalBreed = animalBreed;}
    public void setAnimalRemark(String animalRemark) {this.animalRemark = animalRemark;}


}

DAO 类:

package content;


import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class DAO    
{
   static Connection currentCon = null;
   static ResultSet rs = null;  



   public static loginAuth login(loginAuth bean) {

      //preparing some objects for connection 
      Statement stmt = null;    

      String username = bean.getUsername();    
      String password = bean.getPassword();   

      String searchQuery =
            "select * from user where username='"
                     + username
                     + "' AND password='"
                     + password
                     + "'";

   // "System.out.println" prints in the console; Normally used to trace the process
   System.out.println("Your user name is " + username);          
   System.out.println("Your password is " + password);
   System.out.println("Query: "+searchQuery);

   try 
   {
      //connect to DB 
      currentCon = dbConnection.getConnection();
      stmt=currentCon.createStatement();
      rs = stmt.executeQuery(searchQuery);          
      boolean more = rs.next();

      // if user does not exist set the isValid variable to false
      if (!more) 
      {
         System.out.println("Sorry, you are not a registered user! Please sign up first");
         bean.setValid(false);
      } 

      //if user exists set the isValid variable to true
      else if (more) 
      {
         String firstName = rs.getString("FirstName");
         String lastName = rs.getString("LastName");

         System.out.println("Welcome " + firstName);
         bean.setfname(firstName);
         bean.setlname(lastName);
         bean.setValid(true);
      }
   } 

   catch (Exception ex) 
   {
      System.out.println("Log In failed: An Exception has occurred! " + ex);
   } 

   //some exception handling
   finally 
   {
      if (rs != null)   {
         try {
            rs.close();
         } catch (Exception e) {}
            rs = null;
         }

      if (stmt != null) {
         try {
            stmt.close();
         } catch (Exception e) {}
            stmt = null;
         }

      if (currentCon != null) {
         try {
            currentCon.close();
         } catch (Exception e) {
         }

         currentCon = null;
      }
   }

return bean;

   }    




   public static List<animalBean> selectListAnimal() throws SQLException {

       Statement stmt = null;
       List<animalBean> beans = new ArrayList<animalBean>(); 
       try { 
           currentCon = dbConnection.getConnection();
           String animalSearchQuery = "select a.aname ,a.dob,  a.gender , a.source, s.sname, a.remark from animal as a , specie as s where a.specie_id = s.specie_id and a.available ='y'";        
           stmt=currentCon.createStatement(); 
           rs = stmt.executeQuery(animalSearchQuery);       

           while (rs.next()) { 
               animalBean bean = new animalBean();
               bean.setAnimalName(rs.getString("aname"));
               bean.setAnimalDob(rs.getString("dob"));
               bean.setAnimalGender(rs.getString("gender"));
               bean.setAnimalSource(rs.getString("source"));
               bean.setAnimalBreed(rs.getString("sname"));
               bean.setAnimalRemark(rs.getString("remark"));
               beans.add(bean);
                 }


       } finally { 
           if (rs != null) try { rs.close(); } catch (SQLException logOrIgnore) {} 
           if (stmt != null) try { stmt.close(); } catch (SQLException logOrIgnore) {} 
           if (currentCon != null) try { currentCon.close(); } catch (SQLException logOrIgnore) {} 
       } 
       return beans; 


       }



   }

最后一个JSP页面animalCheck.jsp:

<%@ page language="java" 
         contentType="text/html; charset=windows-1256"
         pageEncoding="windows-1256"
         import="content.animalBean"
         import="content.DAO"




   %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Animal list</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>
</head>
<body>
<table class="title">
  <tr><th>Zoo keeper</th></tr>
</table>


<h1>Animal list</h1>
 <center>
 <table width="100 % " id='table1'  border="1" cellspacing="2" cellpadding="2"> 
    <tr class="tab-highlighted-2"> 
        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Name</div> 
        </td> 
        <td class="tab-highlighted-2" width="20"> 
          <div align="left">Age</div> 
        </td>
         <td class="tab-highlighted-2" width="15"> 
          <div align="left">Gender</div> 
        </td>
        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Status</div> 
        </td>    
        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Breed</div> 
        </td>  
        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Remarks</div> 
        </td> 

    </tr> 

    <c:forEach items="${beans}" var="view"> 
        <tr> 
            <td>${view.animalName} </td> 
            <td>${view.animalDob}</td>
            <td>${view.animalGender}</td>
            <td>${view.animalSource}</td>
            <td>${view.animalBreed}</td>
            <td>${view.animalRemark}</td>

        </tr> 
    </c:forEach> 
</table> 

         </center>

</body></html>

我已经为此苦苦挣扎了 2 天,我检查了许多网站并遵循了许多指南,但仍然没有对我有用 :( 我感谢任何形式的帮助

【问题讨论】:

  • jsp 中的 taglib 指令在哪里?那些不再需要了吗?此外,您不需要导入。
  • 你能不能在你的页面顶部尝试这样的事情 animals = (animalBean)request.getAttribute("beans"); %> 然后更改您的 c:forEach 标记以将其指向动物而不是豆子?
  • 我真的很陌生,所以我不知道什么是 taglib,但如果你的意思是标准、JSP、JSTL jar 文件,我添加了它们,然后页面开始疯狂一次打开,另一个不是这样我 Ctrz + z

标签: java arrays jsp foreach dao


【解决方案1】:

您忘记声明 JSTL 核心标记库。将以下内容添加到您的 JSP 顶部:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

另见:


与具体问题无关,您的代码中还有其他几个问题:

  • 您永远不应将数据库资源声明为static。这不是线程安全的,并且容易发生资源泄漏。在执行 SQL 查询时在同一个方法块中声明它们。
  • login() 方法中存在 SQL 注入漏洞。 Use PreparedStatement
  • 如果您不使用任何 scriptlet,则无需在 JSP 中使用 @page import
  • 类名是supposed,以大写开头。

【讨论】:

  • 现在我在 WEB-INF/lib 下添加 jstl-1.2.jar 后收到 java.lang.NoClassDefFoundError: javax/servlet/jsp/tagext/TagLibraryValidator
  • 同样适用于 jstl-1.1.0.jar
  • 您似乎已经为此提出了另一个问题,所以请继续:stackoverflow.com/questions/8021370/…
  • 感谢您的帮助,现在它正在工作并且问题已解决
【解决方案2】:

你可以在你的页面顶部尝试这样的事情

 <% List<animalBean> animals = (animalBean)request.getAttribute("beans"); %> 

然后更改您的 c:forEach 标记以将其指向动物而不是豆子?

【讨论】:

  • 在这一行找到多个注释: - 类型不匹配:无法从 animalBean 转换为 List - 类型不匹配:无法从 animalBean 转换为 List (animalBean)request.getAttribute 出现此错误
  • -1 用于在 OP 尝试使用 taglibs 时建议 scriptlets
猜你喜欢
  • 1970-01-01
  • 2012-05-16
  • 2016-06-19
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 2013-02-23
  • 1970-01-01
相关资源
最近更新 更多