I add full code Here. Check the following code which I am using to insert values in MySql. While executing the program, Values are not inserting.
Database datab.java
Statement st=null;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root", "1234");
st=con.createStatement();
Servlet Code
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String s = "http://localhost:" + request.getServerPort() + ""
+ request.getContextPath()+"/Emp";
String a,b,c,d,e;
try {
a=request.getParameter("t1");
b=request.getParameter("t2");
c=request.getParameter("t3");
d=request.getParameter("t4");
e=request.getParameter("t5");
System.out.println(a);
datab dt = new datab();
dt.st.execute("insert into employee(eid,emp_name,emp_age,emp_desig,emp_salary) values(a,b,c,d,e)");
response.sendRedirect(s);
} catch(Exception ex){ex.printStackTrace();
out.close();
}
}
ex.printStackTrace()bythrow new RuntimeException(ex), and read the error message and stack trace you get. Your SQL query is obviously syntaxically incorrect (what are a, b, c, d and e for the database?). Then read docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html. In fact, read the whole JDBC tutorial, because your way of opening but never closing statements and connections is really bad.