I'm trying to make a servlet do the same thing as this SQL statement:
INSERT INTO event(title, description, start, end, guest_no) VALUES('someTitle', 'someDescription', '2018-02-02 20:00:00', '2018-02-02 21:00:00', 6);
INSERT INTO shift(event_id, start, end, positions) VALUES(LAST_INSERT_ID(), '2018-02-02 20:00:00', '2018-02-02 21:00:00', 2);
The forms I have at the moment take input from html and the information is inserted into the SQL table using a separate servlet for each table. But I can't seem to get it to add to both tables at the same time as I would with the above statement? I've already tried batches and thought about doing a transaction but I just need to know how to make LAST_INSERT_ID() work in java post method.
<section>
<form name="create" action="${pageContext.request.contextPath}/createEventShift" method="post">
<hr>
<label for="title"><b>Event Name</b></label>
<input type="text" placeholder="Enter title of the event" name="title" required>
<hr>
<label for="description"><b>Description</b></label>
<input type="text" placeholder="Describe your event" name="description" required>
<label for="guest_no"><b>Number of Guests</b></label>
<input type="number" placeholder="Write how many guests" name="guest_no" required>
<label for="start"><b>Start Date & Time (yyyy-MM-dd HH:mm:ss)</b></label>
<input type="datetime-local" placeholder="Start" name="start" step="2">
<label for="end"><b>End Date & Time (yyyy-MM-dd HH:mm:ss)</b></label>
<input type="datetime-local" placeholder="End" name="end" step="2">
<div class="expansive-button v2">
<div><i></i></div>
</div>
<h3>Add Shift</h3>
<label for="event_id"><b>Event ID</b></label>
<input type="" name="event_id" value="LAST_INSERT_ID()">
<label for="startshift"><b>Shift Start (yyyy-MM-dd HH:mm:ss)</b></label>
<input type="datetime-local" placeholder="Start date and time of shift" name="startshift" step="2">
<label for="endshift"><b>Shift End (yyyy-MM-dd HH:mm:ss)</b></label>
<input type="datetime-local" placeholder="End date and time of shift" name="endshift" step="2">
<label for="positions"><b>Number of Staff Needed</b></label>
<input type="number" placeholder="How many staff do you need for this shift" name="positions">
</div>
<br/><br />
<button>Submit</button>
</form>
And the Servlet looks like this:
@WebServlet("/createEventShift")
public class createEventShift extends HttpServlet {
private static final String URL = "jdbc:mysql://localhost:3306/e_manager";
private static final String USER = "root";
private static final String PASSWORD = "2timeLearning!";
private static Connection conn = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
}
}
public static Connection getConnection() {
return conn;
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String insertSQL = "INSERT INTO event(title, description, start, end, "
+ "guest_no) VALUES(?, ?, ?, ?, ?)";
String insertSQL2 = "INSERT INTO shift(event_id, start, end, positions)"
+ " VALUES(LAST_INSERT_ID(), ?, ?, ?)";
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");
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(createEventShift.class.getName()).log
(Level.SEVERE, null, ex);
}
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement ce = conn.prepareStatement(insertSQL);
PreparedStatement cs = conn.prepareStatement(insertSQL2);
ce.setString(1, title);
ce.setString(2, description);
ce.setString(3, start);
ce.setString(4, end);
ce.setInt(5, Integer.parseInt(guest_no));
ce.executeUpdate(insertSQL);
ce.close();
cs.setString(2, ss);
cs.setString(3, es);
cs.setInt(4, Integer.parseInt(pos));
cs.executeUpdate(insertSQL2);
cs.close();
response.sendRedirect("/viewEvents.jsp");
conn.close();
} catch (SQLException ex) {
}
}}
Can anyone tell me how to make this work please? (yes I am new to all this)