带有数据库连接的wicket登录页面

时间:2012-08-03 13:00:31

标签: java jdbc wicket

我已经获得了一个开发登录页面的项目,该页面检查用户是否存在于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);
            }
        });
    }
}

1 个答案:

答案 0 :(得分: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");
}