2010-02-02 16 views
0

我想,如果輸入數據庫在NetBeans IDEJPA查詢驗證,如果輸入數據庫

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); 
       } 

這可不行,請給建議了什麼問題儘快存在驗證是否存在。如果可能的話也提供示例代碼。感謝

+0

任何???請幫忙!!! – kobrakai 2010-02-02 16:23:21

回答

1

使用此:

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

我嘗試過,並調試它,它仍然不斷跳過這一行。這條線有問題。並且if語句的其餘部分無法運行 – kobrakai 2010-02-02 16:03:07

+0

Doh,您改變了您的問題的代碼方式太多了。 – Romain 2010-02-02 16:05:02

+0

我試圖捕捉錯誤,它說「空」 – kobrakai 2010-02-02 16:08:00