2017-10-04 16 views
-1

ログインクラスSQL文で別のクラスから文字列変数を使用することができます。はどのように我々は

public static String n=request.getParameter("userName"); 

のpublic static String型のp = request.getParameter( "userPass");

サーブレットクラス:それは名前を示し、それがLoginpsクラスによって彼のパスを検証し、私が置かれ、サーブレットのログインに戻り、ログイン時、これまでユーザーpassword..so htmlページを使用してログインクラスイムで

PreparedStatement ps1= con.prepareStatement("insert into "+Login.n+" values(?,?,?)"); 

htmlフォームはドレス、ドゥー、パターンfiledsです。サーブレットクラスのリクエストパラメータは、サーブレットクラスのリクエストパラメータdressing、dou、pattern ... preparedstatement( "sqlクエリは" +ここに挿入する必要がありますstring n value + "値(?、?、?))

文字列nLoginクラスから使いたいと思いますユーザーが自分のアカウントを作成したときのダイナミックテーブル。 助けていただければ幸いです。

+0

あなたの問題は今何ですか?エラーはありますか? – soorapadman

+0

フロントエンドからパラメータを挿入しようとすると、無効なテーブル名が表示されています... –

+0

テーブル名が間違っています。あなたは 'Login.n'をコンソールに表示しましたか? – soorapadman

答えて

0

ログインクラス:

String n=request.getParameter("userName"); 
String p=request.getParameter("userPass"); 

if("YOURSERVLETCLASS".checkUser(n, p)) 
{ 
    // anything you like for example forwarding to welcome page 
    RequestDispatcher rs = request.getRequestDispatcher("welcome.html"); 
    rs.forward(request, response); 
}else { 
     // if not correct back to index page 
     out.println("Username or Password incorrect"); 
     RequestDispatcher rs = request.getRequestDispatcher("index.html"); 
     rs.include(request, response); 
    } 

サーブレットクラス:

public static boolean checkUser(String n,String p){ 
    boolean status=false; 
    try{ 

    // set your connection here Connection con = ..... 

    PreparedStatement ps=con.prepareStatement( 
    "INSERT INTO userdetails(username,password)"+"VALUES(?,?)"); 
    ps.setString(1,n); 
    ps.setString(2,p); 

    ResultSet rs=ps.executeQuery(); 
    status=rs.next(); 

    }catch(Exception e){System.out.println(e);} 
    return status; 
} 
+0

これは答えません。 OPの質問 – soorapadman

+0

兄弟これは正確ではない.. –