public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
Connection dbCon;
req.setCharacterEncoding("utf-8");
...................
...................
try
{
Class.forName(driver);
dbCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
PreparedStatement st = dbCon.prepareStatement("INSERT INTO Students (id, fname, lname, age, email) VALUES (? ,? ,? ,?, ?)");
st.setString(1, "id");
st.setString(2, "fname");
st.setString(3, "lname");
st.setString(4, "email");
st.setString(5, "age");
int j = st.executeUpdate();
res.sendRedirect("ShowTable"); //doGet
} catch (Exception e)
{
res.sendRedirect("ShowTable?errormsg=" + e.getMessage());
}
}
void printForm1(PrintWriter out)
{
out.println("<b> id: </b> <input type=\"text\" name=\"id\" ><br>");
out.println("<form action=\"ShowTable\" method=\"POST\">");
out.println("<b> fname : </b> <input type=\"text\" name=\"fname\" ><br>");
out.println("<b> lname : </b> <input type=\"text\" name=\"lname\" ><br>");
out.println("<b> age: </b> <input type=\"text\" name=\"age\" ><br>");
out.println("<b> email: </b> <input type=\"text\" name=\"email\" ><br>");
out.println("<input type=\"submit\" value=\"Save Data\"> ");
out.println("<input type=\"submit\" name=\"del\" value=\"Delete Data\"> ");
out.println("</form>");
}
void printAnyError(PrintWriter out, HttpServletRequest req)
{
String errorMessage = req.getParameter("errormsg");
if (errorMessage != null)
out.println("<br><strong style=\"color:red\"> Error: " + errorMessage + "</strong>");
}