I have been assigned a task to develop an online store.I am facing a problem at the time of insertion of records into SQL Database. I am using Type4 driver. The name of the DNS is SQL. here is my servlet code
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out= response.getWriter();
String Fname= request.getParameter("txtfname");
String Lname= request.getParameter("txtlname");
String Uname=request.getParameter("txtuname");
String Pass=request.getParameter("txtpass");
String Email=request.getParameter("txtemail");
String Gender=request.getParameter("group1");
String Add=request.getParameter("txtadd");
//out.println(""+Fname+Lname+Uname+Pass+Email+Gender+Add);
Connection con= null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
out.println("Class not found");
}
try
{
con=DriverManager.getConnection("jdbc:odbc:SQL","sa","mith1234");
}
catch(Exception e)
{
out.println("Connection not found");
}
try
{
String insert="insert into sign_up values(?,?,?,?,?,?,?)";
PreparedStatement ps= con.prepareStatement(insert);
ps.setString(1,Fname);
ps.setString(2,Lname);
ps.setString(3,Uname);
ps.setString(4,Pass);
ps.setString(5,Email);
ps.setString(6,Gender);
ps.setString(7,Add);
int rt= ps.executeUpdate();
if(rt==0)
{
out.println("Data is not inserted");
}
}
catch(Exception e)
{
out.println("Data Cannot be inserted");
}
}
Since there is no problem in retrieving values from the JSP page and no problem with the request.getParameter. The only problem is at the last try catch block. I am getting an exception "Data Cannot be inserted". Do help me in fixing this insertion problem...