gpt4 book ai didi

java - 使用servlet提交时数据库中没有数据

转载 作者:行者123 更新时间:2023-11-28 23:22:45 24 4
gpt4 key购买 nike

提交表单时,数据不会传入数据库。我正在使用 phpMyadmin。

这是提交表单时显示的内容。我不知道错误在哪里。

14-Dec-2016 17:37:54.895 WARNING [http-nio-8084-exec-146] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [YIP] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
14-Dec-2016 17:37:55.398 INFO [http-nio-8084-exec-146] org.apache.catalina.startup.HostConfig.undeploy Undeploying context [/YIP]
14-Dec-2016 17:37:55.438 INFO [http-nio-8084-exec-153] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor C:\Users\user\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\YIP.xml
14-Dec-2016 17:37:55.656 INFO [http-nio-8084-exec-153] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
14-Dec-2016 17:37:55.665 INFO [http-nio-8084-exec-153] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor C:\Users\user\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\YIP.xml has finished in 227 ms
14-Dec-2016 17:37:55.691 INFO [http-nio-8084-exec-147] org.apache.catalina.util.LifecycleBase.start The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/YIP]] after start() had already been called. The second call will be ignored.

这是我的表格:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Maklumat Sekolah</title>
</head>
<body>
<header style="height: 15%">
<h1 style="text-align: center">Young Innovate</h1>
</header>
<div>
<link rel="stylesheet" type="text/css" href="pendaftaran2.css"/>
<form class="form-3" name="frmsekolah" action="SekolahServlet" method="POST">
<h2 style="text-align: center">Kemaskini Maklumat Sekolah</h2>
<table border="0">
<tbody>
<tr>
<td>Id Sekolah :</td>
<td><input type="text"id="sekolahid" name="sekolahid" size="3"/></td>
</tr>
<tr>
<td>Nama Sekolah :</td>
<td><input type="text" id="snama" name="snama" size="30"/></td>
</tr>
<tr>
<td>Alamat Sekolah :</td>
<td><input type="text" id="salamat" name="salamat" size="50"/></td>
</tr>
<tr>
<td>No.Telefon Sekolah :</td>
<td><input type="text" id="stel" name="stel" size="11"/></td>
</tr>
<tr>
<td>Emel Sekolah :</td>
<td><input type="text" id="semel" name="semel" size="30"/></td>
</tr>
<tr>
<td><input type="hidden" name="action" value="edit"/></td>
</tr>
<tr>
<td class="button">
<button type="submit" name="submit" value="Submit"/>Submit
</td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>

这是我的 servlet:

package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;

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

import com.dao.SekolahDao;
import com.model.Sekolah;
public class SekolahServlet extends HttpServlet {
private static String INSERT = "/sekolahform.jsp";
private static String EDIT = "/editSekolah.jsp";
private static String LIST_SEKOLAH = "/listSekolah.jsp";
private SekolahDao dao;

public SekolahServlet() throws ClassNotFoundException{
super ();
dao = new SekolahDao();
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String forward = "";
String action = request.getParameter("action");

if (action.equalsIgnoreCase("delete")) {
String sekolahid = request.getParameter("sekolahid");
dao.deleteSekolah(sekolahid);
forward = LIST_SEKOLAH;
request.setAttribute("sekolah",dao.getAllUsers());
}
else if (action.equalsIgnoreCase("edit")) {
forward = EDIT;
String sekolahid = request.getParameter("sekolahid");
Sekolah sekolahs = dao.getUserById(sekolahid);
request.setAttribute("sekolah", sekolahs);
}
else if (action.equalsIgnoreCase("listSekolah")){
forward = LIST_SEKOLAH;
request.setAttribute("sekolah", dao.getAllUsers());
}
else if (action.equalsIgnoreCase("insert")){
forward = INSERT;
}

RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

String action = request.getParameter("action");

Sekolah sekolahs = new Sekolah();
sekolahs.setSekolahid(request.getParameter("sekolahid"));
sekolahs.setSnama(request.getParameter("snama"));
sekolahs.setSalamat(request.getParameter("salamat"));
sekolahs.setStel(Integer.parseInt(request.getParameter("stel")));
sekolahs.setSemel(request.getParameter("semel"));


if (action.equalsIgnoreCase("edit"))
{
dao.updateSekolah(sekolahs);
}
else
{
dao.addSekolah(sekolahs);
}

RequestDispatcher view = request.getRequestDispatcher(LIST_SEKOLAH);
request.setAttribute("sekolah", dao.getAllUsers());
view.forward(request, response);
}
}

这是我的 DAO

package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.ArrayList;
import java.util.List;
import com.model.Sekolah;
import com.controller.SekolahServlet;
import com.util.DBConnection;
public class SekolahDao {
private Connection connection;

public SekolahDao() throws ClassNotFoundException{
connection = DBConnection.getConnection();
}

public void addSekolah(Sekolah sekolahs) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("insert into sekolah(sekolahid, snama, salamat, stel, semel) values (?, ?, ?, ?, ?)");
//Parameter start with 1
preparedStatement.setString(1, sekolahs.getSekolahid());
preparedStatement.setString(2, sekolahs.getSnama());
preparedStatement.setString(3, sekolahs.getSalamat());
preparedStatement.setInt(4, sekolahs.getStel());
preparedStatement.setString(5, sekolahs.getSemel());
preparedStatement.executeUpdate();

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

public void deleteSekolah(String sekolahid) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("delete from sekolah where sekolahid=?");
//Parameters start with 1
preparedStatement.setString(1, sekolahid);
preparedStatement.executeUpdate();

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

public void updateSekolah(Sekolah sekolahs) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("update sekolah set snama=?, salamat=?, stel=?, semel=? " + "where sekolahid=?");
//Parameter start with 1

preparedStatement.setString(1, sekolahs.getSnama());
preparedStatement.setString(2, sekolahs.getSalamat());
preparedStatement.setInt(3, sekolahs.getStel());
preparedStatement.setString(4, sekolahs.getSemel());
preparedStatement.setString(5, sekolahs.getSekolahid());
preparedStatement.executeUpdate();

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

public List<Sekolah> getAllUsers() {
List<Sekolah> sekolah = new ArrayList<Sekolah>();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from sekolah");
while (rs.next()) {
Sekolah sekolahs = new Sekolah();
sekolahs.setSekolahid(rs.getString("sekolahid"));
sekolahs.setSnama(rs.getString("snama"));
sekolahs.setSalamat(rs.getString("salamat"));
sekolahs.setStel(rs.getInt("stel"));
sekolahs.setSemel(rs.getString("semel"));
sekolah.add(sekolahs);
}
} catch (SQLException e) {
e.printStackTrace();
}
return sekolah;
}

public Sekolah getUserById (String sekolahid) {
Sekolah sekolahs = new Sekolah();
try {
PreparedStatement preparedStatement = connection.prepareStatement("select * from sekolah where sekolahid=?");
preparedStatement.setString(1, sekolahid);
ResultSet rs = preparedStatement.executeQuery();

while (rs.next()){
sekolahs.setSekolahid(rs.getString("sekolahid"));
sekolahs.setSnama(rs.getString("snama"));
sekolahs.setSalamat(rs.getString("salamat"));
sekolahs.setStel(rs.getInt("stel"));
sekolahs.setSemel(rs.getString("semel"));

}
}catch (SQLException e) {
e.printStackTrace();
}
return sekolahs;
}}

最佳答案

如果没有正确的代码,就不可能解决这个问题...

关于java - 使用servlet提交时数据库中没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41139526/

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