-1
package car;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

@WebServlet(name = "detail", urlPatterns = {"/detail"})

public class detail extends HttpServlet {
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)

       throws ServletException, IOException, ClassNotFoundException, SQLException {

    response.setContentType("text/html;charset=UTF-8");

    PrintWriter out = response.getWriter();

    try {

        String name=request.getParameter("name");

        String emailid=request.getParameter("emailid");

        int mobile=Integer.parseInt(request.getParameter("mobile"));  

        String carname=request.getParameter("carname");

        int regno=Integer.parseInt(request.getParameter("regno"));

        int manufacturingyear=Integer.parseInt(request.getParameter("manufacturingyear"));

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con=DriverManager.getConnection("jdbc:odbc:userdetail","","");

        Statement st=con.createStatement();

        st.executeUpdate("insert into userdetail (name,emailid,mobile,carname,regno,manufacturingyear) values('"+name+"','"+emailid+"','"+mobile+"','"+carname+"','"+regno+"','"+manufacturingyear+"',)");

        getServletContext().getRequestDispatcher("index.jsp").forward(request, response);

        out.println("<html>");

        out.println("<head>");

        out.println("<title>Servlet detail</title>");            

        out.println("</head>");

        out.println("<body>");

        out.println("<h1>FILL DETAILS</h1>");

        out.println("</body>");

        out.println("</html>");

    } catch (Exception e) { 
       e.printStackTrace();
    } finally {            
       out.close();
    }

}

The values are not inserting the given values into the database(userdetail).In database fields are showing null.

Field Datatype

name Text

emailid Text

mobile Int

carname Text

regno Int

manufacturingyear Int

manuell
  • 7,528
  • 5
  • 31
  • 58
user3245648
  • 13
  • 1
  • 5
  • 1
    Learn from your mistakes and improve your question. – Sotirios Delimanolis Jan 29 '14 at 14:33
  • value is not inserting into the table – user3245648 Jan 29 '14 at 14:35
  • @SotiriosDelimanolis Think i done mistake in try part plz verify that – user3245648 Jan 29 '14 at 14:36
  • That's really too bad. We are not your debuggers. Figure it out yourself. If you don't understand why, give us an explanation of exactly what happens and we might help you. In the mean time, format your code. – Sotirios Delimanolis Jan 29 '14 at 14:37
  • @SotiriosDelimanolis only the first field(name) is alone inserted into the table other fields are not inserted – user3245648 Jan 29 '14 at 14:41
  • I'm surprised that the call `Connection con=DriverManager.getConnection("jdbc:odbc:userdetail","","")` isn't failing (it probably is, with the exception being written out in your far too general `catch` block) because that connection string looks like nonsense to me. You need to indicate what database you're trying to write to, so that we can advise about that. You should also read about SQL Inject Attacks and correct your code accordingly. – Nick Holt Jan 29 '14 at 14:48
  • @NickHolt database is ms access – user3245648 Jan 29 '14 at 14:51
  • How to connect to MS Access with JDBC - http://stackoverflow.com/questions/20090994/insert-data-to-ms-access-from-java – Nick Holt Jan 29 '14 at 14:54
  • st.executeUpdate("insert into userdetail (name,emailid,mobile,carname,regno,manufacturingyear) values('"+name+"','"+emailid+"','"+mobile+"','"+carname+"','"+regno+"','"+manufacturingyear+"')"); – user3245648 Jan 29 '14 at 15:09

1 Answers1

0

You must extract a method for saving in the db, and put that method in a different class. Then add a main method to your db dedicated class and try inserting a row from the main method (not from your servlet).

Mixing form reading with db persistance and application logic is a too bad practice: you have the proof in your example: it's impossible to test your app without a server, and impossible to understand where is the problem

Zied Hamdi
  • 2,400
  • 1
  • 25
  • 40
  • st.executeUpdate("insert into userdetail (name,emailid,mobile,carname,regno,manufacturingyear) values('"+name+"','"+emailid+"','"+mobile+"','"+carname+"','"+regno+"','"+manufacturingyear+"')"); – user3245648 Jan 29 '14 at 15:06