0

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();
    }          
} 
3
  • Replace ex.printStackTrace() by throw 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. Commented Aug 23, 2014 at 11:13
  • @JB Nizet... The error is..... java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'a' in 'field list' AddEmp.processRequest(AddEmp.java:57) AddEmp.doPost(AddEmp.java:87) Commented Aug 23, 2014 at 11:21
  • What did I tell you...? Now read docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html. Commented Aug 23, 2014 at 11:23

1 Answer 1

2

Assuming a,b,c,d,e all are string Replace

insert into employee(eid,emp_name,emp_age,emp_desig,emp_salary) values(a,b,c,d,e)

with

insert into employee(eid,emp_name,emp_age,emp_desig,emp_salary) 
values('"+a+"','"+b+"','"+c+"','"+d+"','"+e+"')

Instead of something like this

dt.st.execute

You dont you make the datab class to return connectionObject and using that object create the Statement Object and execute the statements.

Please stop using Statement and take advantage of PreparedStatement something like this

String query = "insert into employee(eid,emp_name,emp_age,emp_desig,emp_salary) 
    values(?,?,?,?,?)"
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, a);
statement.setString(2, b);
statement.setString(3, c);
statement.setString(4, d);
statement.setString(5, e);
statement.executeUpdate();
Sign up to request clarification or add additional context in comments.

3 Comments

u r correct.... dt.st.execute("insert into employee(eid,emp_name,emp_age,emp_desig,emp_salary) values('"+a+"','"+b+"','"+c+"','"+d+"','"+e+"')"); ... This is working...
Dont read only the first part please read the whole answer and follow that instead of the first part
@SparkOn next time, don't even advise the first way. Newbies inevitably choose to do it that way because it's simpler to understand than the second. They don't realize it's also completely wrong. My 2 cents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.