tags:

views:

164

answers:

1

Hi, I want to validate if input exist in database in netbeans IDE

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {

               Transaction1 t = new Transaction1();
               //t.setTransactionID(12345);
               t.setFromAccNo(Integer.parseInt(request.getParameter("fromAccNo")));
               t.setFromSortCodeNo(request.getParameter("fromSortCodeNo"));
               t.setToAccNo(Integer.parseInt(request.getParameter("toAccNo")));
               t.setToSortCodeNo(request.getParameter("toSortCodeNo"));
               t.setName(request.getParameter("name"));
               t.setAmount(Double.parseDouble(request.getParameter("amount")));


               Accounts a = new Accounts();
               a.setFromAccNo(Integer.parseInt(request.getParameter("fromAccNo")));
               a.setToAccNo(Integer.parseInt(request.getParameter("toAccNo")));

               Integer youraccinput = Integer.parseInt(request.getParameter("fromAccNo"));

               EntityManager em = null;
               Query checkbothno = em.createQuery("SELECT a FROM Accounts a WHERE a.fromAccNo =:" + youraccinput);
                checkbothno.setParameter(youraccinput, a);

                if (checkbothno.getResultList().size() != 0) {
                    out.println ("Found");
                } else  {
                    out.println ("Not found");
                }

               try{
                    EntityManagerFactory emf = Persistence.createEntityManagerFactory("SWSXXPU");
                   // emf = Persistence.createEntityManagerFactory("SWSXXPU");
                    em = emf.createEntityManager();
                    em.getTransaction().begin();
                    em.persist(t);
                    em.persist(a);
                    em.getTransaction().commit();
                    request.getRequestDispatcher("ListTransaction").forward(request, response);
                 }

This cannot work, please give advice what went wrong asap. also if possible give sample codes as well. thanks

+1  A: 

Use this:

Query checkbothno = em.createQuery("SELECT a FROM Accounts a WHERE a.fromAccNo=:youraccinput");
checkbothno.setParameter("youraccinput", youraccinput);
Romain
i tried, and debugged it, it still keeps skipping this line. something is wrong with this line. and the rest of the if statement cant be run either
kobrakai
Doh, you changed your question's code way too much.
Romain
i tried to catch the error, it says "null"
kobrakai
Query checkbothno = em.createQuery("SELECT a FROM Accounts a WHERE a.fromAccNo=:youraccinput");checkbothno.setParameter("youraccinput", youraccinput);doesnt work either, same err msg.
kobrakai
Then we'll need the stack trace for the error.
Romain
how do i obtain that please?
kobrakai
The JVM should display it somehow either in the console or in a log file when the exception is hit (and not caught).
Romain