2012-08-03 77 views
-1

我已經給了一個項目來開發一個登錄頁面,檢查用戶是否存在postgres數據庫,並允許他登錄,否則要求他作爲新用戶登錄,我已經進入if子句也但它不工作....我的代碼是 -檢票登錄頁面與數據庫連接

enter code here 

package in.login; 

import java.sql.Connection; 

public class Page3 extends WebPage{ 

    public Page3(){ 
     final TextField uname = new TextField("uname", new Model()); 
     final TextField password = new PasswordTextField("password", new Model()); 
     add(new FeedbackPanel("feedback")); 

     Form form = new Form("f"){ 
      protected void onSubmit() { 
       String v1= uname.getDefaultModelObjectAsString(); 
       String v2 = password.getDefaultModelObjectAsString(); 
       System.out.println(uname.getDefaultModelObjectAsString()); 
       System.out.println(password.getDefaultModelObjectAsString()); 
       try { 

        Class.forName("org.postgresql.Driver"); 
        Connection conn = DriverManager.getConnection(
          "jdbc:postgresql://localhost/registration:5432", 
          "postgres", 
          "ashneel"); 
        try { 
         conn.setAutoCommit(false); 
         PreparedStatement st = (PreparedStatement) conn.prepareStatement("select * from registration where uname='"+v1+"' and password='"+v2+"' "); 
         try{ 
          if(password.equals(v2)) { 
           System.out.print("aaaaa"); 
           setResponsePage(Inbox.class); 
          } 
          else { 
           System.out.print("new user"); 
          } 
          setResponsePage(Inbox.class); 
         } finally { 
          st.close(); 
         } 
        } catch (Exception e) { 
         conn.rollback(); 
         throw e; 
        } 

       } catch (Exception e) { 
        throw new RuntimeException(e); 
       } 

      } 

     }; 

     form.add(uname); 
     password.add(StringValidator.minimumLength(5)); 
     password.add(StringValidator.maximumLength(10)); 
     form.add(password); 
     add(form); 

     form.add(new Link("link") { 
      public void onClick() { 
       Registr registr = new Registr(); 
       setResponsePage(registr); 
      } 
     }); 
    } 
} 
+0

什麼的一部分是不工作?你有堆棧跟蹤嗎?它會編譯? – Makoto 2012-08-03 13:01:41

+0

你使用'wicket-auth-roles'嗎?你的'Application'類是否擴展'AuthenticatedWebApplication'? – tetsuo 2012-08-04 13:49:51

回答

1

你應該使用:

PreparedStatement st = (PreparedStatement) conn.prepareStatement("select * from registration where uname=? and password=? "); 
st.setString(1,v1); 
st.setString(1,v2); 
ResultSet rs=st.executeQuery(); 
if(rs.next()){ 
          System.out.print("aaaaa"); 
}else{ 
          System.out.print("new user"); 
}