I have been creating an android app. I designed a signup form . Here when people click the signup button the details are added to the mysql table(contains confirmation code as a field.) and the confirmation code is initially set to NULL and then a servlet is called . This servlet contains the code that generates a 4 digit random number and adds it to the mysql table for the particular entry. Then the servlet calls a method in another class that sends confirmation email to the user with the code.
Now when I run the app and servlet a confirmation code is generated and it is sent to the user vie email. But the problem is the mysql table is not getting updated. Note: The code for updation of mysql table is given above the code that send email. However the same servlet updates the mysql table and sends email on submission of a HTML form.
Don't know how to solve this.. For info I am hosting the application on Amazon Elastic Beanstalk.
This is the Servlet part:
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); System.out.println("fdsfsdf"); mBean = new MailSenderBean(); Random n=new Random(); int no=n.nextInt(); no%=10000; if(no<0) { no=-no; } String code="0000"+no , name = request.getParameter("name"), email = request.getParameter("email"); code=code.substring(code.length()-4, code.length()); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(AppConfig.dbUrl,AppConfig.dbUname, AppConfig.dbPwd); PreparedStatement pst=con.prepareStatement("update signup_users set code=\'"+code+"\' where name=\'"+name+"\'"); pst.execute(); mBean.sendMail(name, email, code); System.out.println("name="+name+",email="+email+",code="+code); }catch(Exception e) { System.out.println(""+e); } }
and my client side code in android app is
DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(AppConfig.serverUrl); List lst =new ArrayList(); lst.add(new BasicNameValuePair("name", name)); lst.add(new BasicNameValuePair("email", email)); httpPost.setEntity(new UrlEncodedFormEntity(lst)); HttpResponse httpResponse = httpClient.execute(httpPost);
PreparedStatement
correctly. You should be paramatizing your inputs to it, with?
characters...mkyong.com/jdbc/… – SnakeDoc