gpt4 book ai didi

java - 关于将数据保存到sql数据库的问题

转载 作者:行者123 更新时间:2023-11-30 05:06:54 25 4
gpt4 key购买 nike

我正在尝试通过在文本框中输入信息并单击提交按钮来将数据添加到 SQL 数据库。但是,我无法让提交按钮正常工作。我对 html 不太熟悉,所以我可能做错了什么。这是一些代码。这是我的带有提交按钮的 jsp 页面。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Page</title>
</head>
<body>
<table border="1" align="center">
<tr><td>Student ID</td><td>First Name</td><td>Last Name</td><td>Degree Program</td><td>Gender</td></tr>
<%
List<Student> students = (List) request.getAttribute("student");
for (Student student : students) { %>
<tr><td><%=student.getStudentId()%></td><td><%=student.getFirstname()%></td><td><%=student.getLastname()%></td><td><%=student.getDegreeprogram()%></td><td><%=student.getGender()%></td></tr>
<% }
%>

<tr><td>&nbsp;</td><td>&nbsp;</td><td><a href="/myweb">Go To Home Page</a></td><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>

<form name="student" action="process" method="post">
<input type="hidden" name="view" value="addStudent" />
<table>
<tr><td>&nbsp;</td><td><Strong>Add a Student</Strong></td></tr>
<tr><td>Student ID: </td><td><input type="text" name="studentid" id="studentid"/></td></tr>
<tr><td>First Name: </td><td><input type="text" name="firstname" id="firstname"/></td></tr>
<tr><td>Last Name: </td><td><input type="text" name="lastname" id="lastname"/></td></tr>
<tr><td>Degree Program: </td><td><input type="text" name="degreeprogram" id="degreeprogram"/></td></tr>
<tr><td>Gender: </td><td><input type="text" name="gender" id="gender"/></td></tr>
<tr><td>&nbsp;</td><td><input type="submit"/></td></tr>
</table>
</form>

<form name="exit" action="process" method="post">
<input type="hidden" name="view" value="home" />
<p align="center"><input type="button" value="Exit"/></p>
</form>
</body>
</html>
<小时/>

这是我的 servlet,在其中进行查询并将数据保存到数据库中。

@WebServlet(name="regservlet", urlPatterns={"/registrar"})
public class regservlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");

try {
String view = request.getParameter("view");

if (view != null) {
String jspName="/index.jsp";

HibernateUtil util = new HibernateUtil();
Session hibernateSession = (Session) util.getHibernateSession();
String tableName = "";

if (view.equalsIgnoreCase("Student")) {
tableName="Student";
Query q = hibernateSession.createQuery("from " + tableName);
List<Student> students = q.list();
jspName="/student.jsp";
request.setAttribute("student", students);



} else if (view.equalsIgnoreCase("Course")) {
tableName="Course";
Query q = hibernateSession.createQuery("from " + tableName);
List<Course> courses = q.list();
request.setAttribute("course", courses);
jspName="/course.jsp";
}

else if (view.equalsIgnoreCase("Enrollment")) {
tableName="Enrollment";
Query q = (Query) hibernateSession.createQuery("from " + tableName);
List<Enrollment> enrollment = q.list();
request.setAttribute("enrollment", enrollment);
jspName="/enrollment.jsp";
}

else if (view.equalsIgnoreCase("addStudent")) {
String studentid = request.getParameter("studentid");
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String degreeprogram = request.getParameter("degreeprogram");
String gender = request.getParameter("gender");
Student myStudent = new Student(studentid, firstname, lastname, degreeprogram, gender);
hibernateSession.saveOrUpdate(myStudent);
Transaction t = hibernateSession.beginTransaction();
t.commit();
jspName="/index.jsp";
}

else if (view.equalsIgnoreCase("addCourse")) {
String courseid = request.getParameter("courseid");
String coursename = request.getParameter("course");
String coursesection = request.getParameter("coursesection");
String instructorId= request.getParameter("instructorid");
Course myCourse = new Course (courseid, coursename, coursesection, instructorId);
hibernateSession.saveOrUpdate(myCourse);
Transaction t = hibernateSession.beginTransaction();
t.commit();
jspName="/index.jsp";
}

else if (view.equalsIgnoreCase("addEnrollment")) {
String enrollmentid = request.getParameter("enrollmentid");
String course = request.getParameter("course");
String student = request.getParameter("student");
String semester= request.getParameter("semester");
Enrollment myEnrollment = new Enrollment(enrollmentid, course, student, semester);
hibernateSession.saveOrUpdate(myEnrollment);
Transaction t = hibernateSession.beginTransaction();
t.commit();
jspName="/index.jsp";
}

else if (view.equalsIgnoreCase("home")) {
jspName="/index.jsp";
}

hibernateSession.close();
System.out.println("JSP NAME "+jspName);
this.getServletContext().getRequestDispatcher(jspName).forward(request, response);

}

}catch (Exception ex) {
ex.printStackTrace();
System.err.println("Initial SessionFactory creation failed." + ex);
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}

有什么建议吗?

最佳答案

您的表单的操作设置为“处理”。您的 servlet 配置为作为“注册器”调用,因此它永远不会被调用。

关于java - 关于将数据保存到sql数据库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4808775/

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