gpt4 book ai didi

java - 无法使用 JSP/DAO/Servlet 更新表

转载 作者:行者123 更新时间:2023-12-02 08:06:47 27 4
gpt4 key购买 nike

您好,我正在尝试创建一个页面来更新数据库表中的一行,我正在将 DAO/Servlet 与 JSP 页面结合使用

DAO 代码:

public static AnimalUpdateBean updateAnimal(AnimalUpdateBean bean) {

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

String id = bean.getAnimalId();
String aname = bean.getAnimalName();
String dob = bean.getAnimalDob();
String gender = bean.getAnimalGender();
String breedid = bean.getAnimalBreed();
String remark = bean.getAnimalRemark();
try
{
//connect to DB
currentCon = dbConnection.getConnection();

up = currentCon.prepareStatement("update animal set aname = '"+aname+"' , gender = '"+gender+"', specie_id = '"
+breedid+"' , remark = '"+remark+"' where animal_id = '"+id+"'");

up.executeUpdate();

if (up.executeUpdate()>=1){
stmt=currentCon.createStatement();
rs = stmt.executeQuery("select aname , dob, gender, specie_id , remark from animal where animal_id = '"+id+"'");
}


System.out.println("done");
}


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;

}
}

UpdateAnimal.jsp代码:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page import="java.util.ArrayList" %>
<%@page import="content.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page session="true"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>
<%@ page language="java"
contentType="text/html; charset=windows-1256"
pageEncoding="windows-1256" %>


<!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>Update Animal</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
</head>
<body>
<table class="title">
<tr><th>Zoo keeper</th></tr>
</table>


<h1>Update Animal</h1>

<form action="Relay" >
<fieldset>
Animal new name: <input type= "text" name = "aname"><br>
Animal new DOB: <input type= "text" name = "dob"><br>

<br>
Animal new gender:
<select name="gender" id="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<br>

Animal new Breed: <input type= "text" name = "breedid" ><br>
Animal new remarks: <textarea name = "remark" rows="4" cols="20">

</textarea> <br /> <br/>


<input type="submit" value="submit">
<input type="hidden" name="animal_id" value="<%= request.getParameter("animal_id") %>">
<input type="hidden" name="command" value="UpdateAnimalServlet" >

</fieldset>
</form>
</body></html>

正在更新的记录来自选择页面以查看记录,当用户单击名称时,他将被重定向到更新页面 CheckAnimal.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page import="java.util.ArrayList" %>
<%@page import="content.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page session="true"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>
<%@ page language="java"
contentType="text/html; charset=windows-1256"
pageEncoding="windows-1256" %>


<!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>
Click on animal name to update it!
<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="13">
<div align="left">Age</div>
</td>
<td class="tab-highlighted-2" width="13">
<div align="left">Gender</div>
</td>
<td class="tab-highlighted-2" width="13">
<div align="left">Status</div>
</td>
<td class="tab-highlighted-2" width="13">
<div align="left">Breed</div>
</td>
<td class="tab-highlighted-2" width="13">
<div align="left">Pen #</div>
</td>
<td class="tab-highlighted-2" width="15">
<div align="left">Zoo</div>
</td>
<td class="tab-highlighted-2" width="20">
<div align="left">Remarks</div>
</td>

</tr>

<c:forEach items="${beans}" var="view">
<tr>

<td><a href="/oosd/UpdateAnimal.jsp?animal_id=${view.animalId}&breed=${view.breedId}">${view.animalName}</a></td>
<td>${view.animalDob}</td>
<td>${view.animalGender}</td>
<td>${view.animalSource}</td>
<td>${view.animalBreed}</td>
<td>${view.sectionId}</td>
<td>${view.zooId}</td>
<td>${view.animalRemark}</td>

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

</center>

</body></html>

AnimalUpdateBean.java代码:

package content;

public class AnimalUpdateBean {
private String animalId;
private String animalName;
private String animalDob;
private String animalGender;
private String animalBreed;
private String animalRemark;

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

public void setAnimalId(String animalId) {this.animalId = animalId;}
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 setAnimalBreed(String animalBreed) {this.animalBreed = animalBreed;}
public void setAnimalRemark(String animalRemark) {this.animalRemark = animalRemark;}


}

负责启动更新的servlet是UpdateAnimalServlet:

package content;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class LoginServlet
*/
public class UpdateAnimalServlet extends HttpServlet {


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

try
{

AnimalUpdateBean animal = new AnimalUpdateBean();
animal.setAnimalId(request.getParameter("animal_id"));
animal.setAnimalName(request.getParameter("aname"));
// animal.setAnimalDob(request.getParameter("dob"));
animal.setAnimalGender(request.getParameter("gender"));
animal.setAnimalBreed(request.getParameter("breedid"));
animal.setAnimalRemark(request.getParameter("remark"));
String test = request.getParameter("animal_id");
System.out.println(test);
System.out.println(animal);

animal = DAO.updateAnimal(animal);


response.sendRedirect("/oosd/member.jsp");


}


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

我正在使用一个调用 AnimalUpdateServlet 来执行的 Relay servlet:

package content;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class LoginServlet
*/
public class Relay extends HttpServlet {


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

try
{



String command = request.getParameter("command");
if (command.equals("LoginServlet")){
RequestDispatcher rd =request.getRequestDispatcher("/"+command);
rd.forward(request, response);
//for testing
System.out.println("Request forwarded to " + command + " servlet");
} else if (command.equals("UpdateAnimalServlet")){
RequestDispatcher rd =request.getRequestDispatcher("/"+command);
rd.forward(request, response);
//for testing
System.out.println("Request forwarded to " + command + " servlet");
}

else
System.out.println("=> command='" + command + "'");
String url = "/oosd/" + command;
String encodedUrl = response.encodeRedirectURL(url);
System.out.println(" url=" + url);
System.out.println(" encodedUrl=" + encodedUrl);
response.sendRedirect(encodedUrl);



}


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


and last thing is the web.xml page:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>content.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>UpdateAnimalServlet</servlet-name>
<servlet-class>content.UpdateAnimalServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SelectAnimalServlet</servlet-name>
<servlet-class>content.SelectAnimalServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Relay</servlet-name>
<servlet-class>content.Relay</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelectAnimalServlet</servlet-name>
<url-pattern>/SelectAnimalServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateAnimalServlet</servlet-name>
<url-pattern>/UpdateAnimalServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Relay</servlet-name>
<url-pattern>/Relay</url-pattern>
</servlet-mapping>
</web-app>

我不断收到这些错误:登录失败:发生异常! java.sql.SQLException:[Microsoft][ODBC Microsoft Access Driver]条件表达式中的数据类型不匹配。

java.lang.IllegalStateException:提交响应后无法调用 sendRedirect()

我已经检查了数据输入和所有正确的姓名文本、性别文本、specie_id 编号、备注文本,我什至将 dob 置于保留状态,直到我使用 Access 数据库进行这项工作,这是连接代码:

package content;


import java.sql.*;


public class dbConnection {

static Connection con;
static String url;

public static Connection getConnection()
{

try
{
String url = "jdbc:odbc:oosd";
// assuming "DataSource" is your DataSource name

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

try
{
con = DriverManager.getConnection(url,"","");

// assuming your SQL Server's username is "username"
// and password is "password"

}

catch (SQLException ex)
{
ex.printStackTrace();
}
}

catch(ClassNotFoundException e)
{
System.out.println(e);
}

return con;
}
}

知道我的错误在哪里吗?

最佳答案

因为specie_id是一个数字,这就是抛出的异常,指出“条件表达式中的数据类型不匹配”。并且由于此异常,由于 SQLException 数据已写入响应,因此发生了 sendRedirect 错误。 你的查询应该类似于

  "update animal set aname = '"+aname+"' , gender = '"+gender+"', specie_id = "
+Integer.parseInt(breedid)+" , remark = '"+remark+"' where animal_id = '"+id+"'"

如果animal_id也是一个数字,则需要同样的操作。

关于java - 无法使用 JSP/DAO/Servlet 更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8032886/

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