Java 如何通过连接到JSP的servlet插入带有自动递增外键的MySQL表

Java 如何通过连接到JSP的servlet插入带有自动递增外键的MySQL表,java,mysql,servlets,prepared-statement,forms,http-post,Java,Mysql,Servlets,Prepared Statement,Forms,Http Post,我试图让servlet执行与以下SQL语句相同的操作: 在事件(标题、描述、开始、结束、客人编号)值中插入('someTitle','someDescription','2018-02-02 20:00:00','2018-02-02 21:00:00',6) 插入班次(事件id、开始、结束、位置)值(最后一次插入id(),'2018-02-02 20:00:00','2018-02-02 21:00:00',2) 我现在使用的表单从html中获取输入,并使用一个单独的servlet将信息插入到

我试图让servlet执行与以下SQL语句相同的操作:

在事件(标题、描述、开始、结束、客人编号)值中插入('someTitle','someDescription','2018-02-02 20:00:00','2018-02-02 21:00:00',6)

插入班次(事件id、开始、结束、位置)值(最后一次插入id(),'2018-02-02 20:00:00','2018-02-02 21:00:00',2)

我现在使用的表单从html中获取输入,并使用一个单独的servlet将信息插入到SQL表中。但我似乎无法像上面的语句那样,同时将其添加到两个表中?我已经尝试过批处理,并考虑过做一个事务,但我只需要知道如何使LAST_INSERT_ID()在java post方法中工作



事件名称
描述 客人人数 开始日期和时间(yyyy-MM-dd HH:MM:ss) 结束日期和时间(yyyy-MM-dd HH:MM:ss) 加班次 事件ID 班次开始(yyyy-MM-dd HH:MM:ss) 班次结束(yyyy-MM-dd HH:MM:ss) 所需工作人员人数

提交
通常我们将数据库逻辑与servlet分开。因为如果您有许多servlet,那么您的代码将更难维护(您必须为每个servlet反复编写数据库连接)。从安全角度来看,也不建议这样做。除其他原因外

这里有一些你可以做的事情,让事情变得更容易。创建另一个仅用于数据库连接的类,并且每当您想对数据库执行某些操作时,都可以调用该类(下面我将向您演示如何执行该操作):

还有一件很有用的事要知道,那就是。。。这是一种将软件划分为三个相互关联的部分的方法,同样,原因之一是使其更易于管理。在这种设置中,您可以将视图视为JSP/html页面,控制器是servlet,模型是用于在两者之间进行接口的

让我们创建一个模型来处理您的事件和轮班(但实际上类似的事情应该是分开的)

因此,让我们创建另一个类,在这里我将其称为
events和shift
。在这个类中,我们将处理各种数据库操作。通常我们会为eventsandshift数据库连接创建另一个类(简称DAO),但在本例中,我们将在同一个类中执行此操作

public class EventsAndShifts{    

  //i noticed you wanted to insert the event id for a shift, you can't do it the way you wanted to, you have to do it like this (this method will return an int)
public int setEventInfo(String title, String description, String start, String end, String guestNo){
     int eventID = 0; //initialize
    //get connection from our DBConneciton class we created earlier
    try(Connection conn= DBConnection.getConnection()){
//returning the generated keys lets us get the row id of what we insert
   PreparedStatement pst = conn.prepareStatement("INSERT INTO event(title, description, start, end, guest_no) VALUES(?, ?, ?, ?, ?);", Statement.RETURN_GENERATED_KEYS); 

         pst.setString(1, title);
         pst.setString(2, description);
         pst.setString(3, start); 
         pst.setString(4, end);
         pst.setInt(5, Integer.parseInt(guestNo));

         pst.executeUpdate();   
       //now get the eventID
    ResultSet rs = pst.getGeneratedKeys();
    while (rs.next()) {
    eventID = rs.getInt(1); //get the id of the inserted event
      }

        } catch (SQLException e) {
            e.printStackTrace();
        }
     //we don't need to close our db connection because the try statement does it for us   
return eventID; //return the eventID
}   




public void setShiftInfo(int eventID, String ss, String es, String pos){
    //get connection from our DBConneciton class we created earlier
    try(Connection conn= DBConnection.getConnection()){
   PreparedStatement pst = conn.prepareStatement("INSERT INTO shift(event_id, start, end, positions)VALUES(?, ?, ?, ?);"); 

         pst.setInt(1, eventID);    
         pst.setString(2, ss);
         pst.setString(3, es);
         pst.setInt(4, Integer.parseInt(pos));

         pst.executeUpdate();   

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



}
现在,我们有了所有这些设置,让我们实现一些神奇的效果:

@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

    //get our parameters
    String title = request.getParameter("title");
    String description = request.getParameter("description");
    String start = request.getParameter("start");
    String end = request.getParameter("end");
    String guest_no = request.getParameter("guest_no");       
    String ss = request.getParameter("startshift");
    String es = request.getParameter("endshift");
    String pos = request.getParameter("positions");

    EventsAndShifts eas = new EventsAndShifts(); //instantiate our EventsAndShifts class

    //insert into event table and return eventID
    int eventID = eas.setEventInfo(title,description,start,end,guest_no);

    //use eventID and insert into event table shift table
    eas.setShiftInfo(eventID,ss,es,pos);

    //all done
  response.sendRedirect("/viewEvents.jsp");     
}

希望这能帮助你更好地理解如何把它们放在一起,如果有些东西不起作用或者你有问题,请告诉我

见阿萨德的回答:魔法确实有效。谢谢你,乔纳森:(()()()())很高兴认识你,很乐意帮忙。如果对您有效,请随意选择正确答案:)
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

    //get our parameters
    String title = request.getParameter("title");
    String description = request.getParameter("description");
    String start = request.getParameter("start");
    String end = request.getParameter("end");
    String guest_no = request.getParameter("guest_no");       
    String ss = request.getParameter("startshift");
    String es = request.getParameter("endshift");
    String pos = request.getParameter("positions");

    EventsAndShifts eas = new EventsAndShifts(); //instantiate our EventsAndShifts class

    //insert into event table and return eventID
    int eventID = eas.setEventInfo(title,description,start,end,guest_no);

    //use eventID and insert into event table shift table
    eas.setShiftInfo(eventID,ss,es,pos);

    //all done
  response.sendRedirect("/viewEvents.jsp");     
}