根据jsp中的输入选择表

时间:2016-07-30 21:50:49

标签: java html mysql jdbc

我在mysql数据库中有两个表,一个用于学生,一个用于教师。两个表都具有相同的列。

以下代码仅将数据插入教师的表格。如果用户选择教师的单选按钮,则注册数据应存储在教师的表中。如果用户选择学生的收音机,则注册数据应存储在学生的表格中。什么是解决方案?

enter image description here

的index.jsp

<form  method="GET " action="statement2.jsp" autocomplete="on"> 
  <input type="radio" name="gender" value="Teacher" checked/> Teacher
  <input type="radio" name="gender" value="Student"/>Student
  <input type="submit" value="Login" /> 
  <input type="button" onclick="location.href='reg.jsp';" value="Sign Up" />
</form>

statement2.jsp

<%
   String name=request.getParameter("first");
   String abc=request.getParameter("last");
   String cde=request.getParameter("user");
   String pass=request.getParameter("password");
   String confpass=request.getParameter("confirmpass");
   String emails=request.getParameter("email");
   String months=request.getParameter("month");
   String day=request.getParameter("day");
   String year=request.getParameter("year");
   String gender=request.getParameter("gender");

   String Sql="insert into teacher2(firstname,lastname,username,password,confirmpassword,email,month,day,year,gender) values('"+name+"','"+abc+"','"+cde+"','"+pass+"','"+confpass+"','"+emails+"','"+months+"','"+day+"','"+year+"','"+gender+"')";
   st.executeUpdate(Sql);
%>

更正后 statement2.jsp

<%
   String name=request.getParameter("first");
   String abc=request.getParameter("last");
   String cde=request.getParameter("user");
   String pass=request.getParameter("password");
   String confpass=request.getParameter("confirmpass");
   String emails=request.getParameter("email");
   String months=request.getParameter("month");
   String day=request.getParameter("day");
   String year=request.getParameter("year");
   String gender=request.getParameter("gender");

   if (gender != null) {
   String table = gender.equals("teacher") ? "teacher2" : "student";
   // replace dots with your values
   String query = "INSERT INTO " + table + "(firstname,lastname,username,password,confirmpassword,email,month,day,year,gender) VALUES ('"+name+"','"+abc+"','"+cde+"','"+pass+"','"+confpass+"','"+emails+"','"+months+"','"+day+"','"+year+"','"+gender+"')";

   st.executeUpdate(query); 
  %>

此查询有什么问题。为什么此查询仅在学生表中插入数据

的index.jsp

   <input type="radio" name="gender" value="teacher" checked/> Teacher
   <input type="radio" name="gender" value="Student"/>Student

statement2.jsp

   String gender = request.getParameter("gender");
   if (gender != null) {
   String table = gender.equals("teacher") ? "teacher2" : "student";
// replace dots with your values
   String query = "INSERT INTO " + table + "(firstname,lastname,username,password,confirmpassword,email,month,day,year,gender) VALUES ('"+name+"','"+abc+"','"+cde+"','"+pass+"','"+confpass+"','"+emails+"','"+months+"','"+day+"','"+year+"','"+gender+"')";
  st.executeUpdate(query);

1 个答案:

答案 0 :(得分:0)

只需选择一个合适的表,然后使用

将其传递给查询语句
<%
  String gender = request.getParameter("gender");
  if (gender != null) {
    String table = gender.equals("teacher") ? "teacherTable" : "studentTable";
    // replace dots with your values
    String query = "INSERT INTO " + table + "(...) VALUES (...)";
    st.executeUpdate(query); 
  }
%>

请注意,重定向到另一个jsp并在那里使用scriplets(不受规范阻止)处理提交的数据不是最佳选择。我宁愿建议您将表单提交给Servlet并在那里编写所有Java代码。

此外,您可能希望PreparedStatement优先于Statement

如果您有兴趣,请查看这些答案

How to avoid Java code in JSP files?

When should we use a PreparedStatement instead of a Statement?