I am trying to create a data inserting part using crud. I am not getting any errors but the data is not inserting into the database. the database connection is working correctly. these are the things that i tried. the table has 5 fields id, doctor,patient,date and venue. I am not getting any errors in the console but error while inserting data is displaying this line
$("#alertError").text("Error while saving.");
jsp file
<html>
<head>
<meta charset="ISO-8859-1">
<title>Appointments</title>
<link rel="stylesheet" href="Views/bootstrap.min.css">
<script src="Components/jquery-3.2.1.min.js"></script>
<script src="Components/appointment.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-6">
<h1>Appointment Management</h1>
<br>
<form id="formAppointment" name="formAppointment"
action="view.jsp">
Appointment ID <input id="appointmentId" name="appointmentId"
type="text" class="form-control form-control-sm"> <br>
Assign Doctor <input id="assignDoctor" name="assignDoctor"
type="text" class="form-control form-control-sm"> <br>
Assign Patient <input id="assignPatient" name="assignPatient"
type="text" class="form-control form-control-sm"> <br>
Appointment Date <input id="appointmentDate" name="appointmentDate"
type="text" class="form-control form-control-sm"> <br>
Appointment Venue <input id="appointmentVenue"
name="appointmentVenue" type="text"
class="form-control form-control-sm"> <br>
<input
id="btnSave" name="btnSave" type="button" value="Insert Appointment"
class="btn btn-primary">
<input type="hidden" id="hide" name="hide" value="">
<input type="submit"
id="btnView" name="btnView" type="button" value="View Appointment"
class="btn btn-primary">
</form>
<br>
</body>
</html>
Java file
public class Appointment {
public String insertAppointment(String date, String venue, String docName, String patientId) {
String output = "";
try {
Connection con = connect();
if (con == null) {
return "Error while connecting to the database for inserting appoinment details.";
}
String insert = " insert into appointment (`app_Date`,`app_Venue`,`app_Doctor_Id`,`app_Patient_Id`) values (?,?,?,?)";
PreparedStatement preparedStatement = con.prepareStatement(insert);
preparedStatement.setString(1, date);
preparedStatement.setString(2, venue);
preparedStatement.setString(3, docName);
preparedStatement.setString(4, patientId);
preparedStatement.execute();
output = "Inserted successfully";
//output = "{\"status\":\"success\", \"data\": \"\"}";
} catch (Exception e) {
output = "Error while inserting the Appoinment.";
System.err.println(e.getMessage());
}
return output;
}
}
API
/**
* Servlet implementation class AppointmentAPI
*/
@WebServlet("/AppointmentAPI")
public class AppointmentAPI extends HttpServlet {
private static final long serialVersionUID = 1L;
Appointment appointmentObj = new Appointment();
/**
* @see HttpServlet#HttpServlet()
*/
public AppointmentAPI() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String output = appointmentObj.insertAppointment(request.getParameter("appointmentDate"),
request.getParameter("appointmentVenue"), request.getParameter("assignDoctor"),
request.getParameter("assignPatient"));
response.getWriter().write(output);
}
}
Validation is done by JavaScript
$(document).ready(function() {
$("#alertSuccess").hide();
$("#alertError").hide();
});
$(document).on("click", "#btnSave", function(event) {
$("#alertSuccess").text("");
$("#alertSuccess").hide();
$("#alertError").text("");
$("#alertError").hide();
var status = validateItemForm();
if (status != true) {
$("#alertError").text(status);
$("#alertError").show();
return;
}
var type = ($("#hide").val() == "") ? "POST" : "PUT";
$.ajax({
url : "AppointmentsAPI",
type : type,
data : $("#formAppointment").serialize(),
dataType : "text",
complete : function(response, status) {
saveAppointment(response.responseText, status);
}
});
});
function saveAppointment(response, status) {
if (status == "success") {
var resultSet = JSON.parse(response);
if (resultSet.status.trim() == "success") {
$("#alertSuccess").text("Successfully saved.");
$("#alertSuccess").show();
$("#divAppointmentsGrid").html(resultSet.data);
} else if (resultSet.status.trim() == "error") {
$("#alertError").text(resultSet.data);
$("#alertError").show();
}
} else if (status == "error") {
$("#alertError").text("Error while saving.");
$("#alertError").show();
} else {
$("#alertError").text("UnKnown error while saving..");
$("#alertError").show();
}
$("#hide").val("");
$("#formAppointment")[0].reset();
}
function validateItemForm()
{
/*if ($("#appointmentId").val().trim() == "")
{
return "Enter Appointment ID";
}*/
if ($("#assignDoctor").val().trim() == "")
{
return "Enter Doctor Id";
}
if ($("#assignPatient").val().trim() == "")
{
return "Enter Patient Id";
}
if ($("#appointmentVenue").val().trim() == "")
{
return "Enter Appointment Venue";
}
return true;
}