gpt4 book ai didi

java - 数组列表+数据库+servlet+DAO

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:26 28 4
gpt4 key购买 nike

您好,我是 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);
}
}
}

动物 bean :

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>

两天以来我一直在努力解决这个问题,我检查了很多网站并遵循了很多指南,但仍然没有任何效果:(我感谢任何形式的帮助

最佳答案

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

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

另请参阅:

<小时/>

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

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

关于java - 数组列表+数据库+servlet+DAO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8020714/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com