さて、この問題を解決するためにさまざまなことを行うことができます
a)ここでDAO のプラクティスを使用する 基本的に、コードの実装をさまざまなセグメントとクラスに分割して簡単に理解できるように読んでください。しかし、そのすべての問題を経験したくない場合は、次のようなことができます。a.) insert、select などの Db を処理するすべてのメソッドを記述する個別の Java クラスを作成します。たとえば、ユーザーのログインの場合、メソッドは次のようになります。
public int AuthenticateUser(String un, String pw, String phn) {
System.out.println("I m in this method");
Connection conn = (Connection) DBConnection.getInstance().getConnection();
Statement pstmt = null;
ResultSet rs = null;
if (conn == null) {
System.out.println("Connection error");
check = -1;
}
try {
String authenticatequery = "SELECT * FROM users WHERE uname = '"+un+"' && password = '"+pw+"' && phoneno = '"+phn+"'";
System.out.println(authenticatequery);
pstmt = conn.createStatement();
rs = pstmt.executeQuery(authenticatequery);
System.out.println("I m in above try catch");
// pstmt = conn.prepareStatement(authenticatequery);
// rs = pstmt.executeQuery(authenticatequery);
if (rs.next()) {
System.out.println("I am in rs method");
UserInfo info = new UserInfo();
rs.getString(1);
rs.getString(2);
rs.getString(3);
//info.setUsername(rs.getString(un));
System.out.println("i have the username" + un);
//info.setPassword(rs.getString(un));
System.out.println("I have got the password " + pw);
System.out.println("I have got the password " + phn);
System.out.println("User Exist");
check = 1;
} else {
System.out.println("No user found");
check = 0;
}
// rs.close();
// pstmt.close();
// conn.close();
} catch (SQLException e) {
// TODO: handle exception
System.out.println("Exception-internal error");
} finally {
if (conn != null) {
try {
conn.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return check;
}
メソッドが正しく実行された場合、int 変数「check」に注意してください。「1」、そうでない場合は「0」が返されます。これで、このチェックを有利に使用して、このようにサーブレット経由で Android アプリに送信できます。
protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//String username = request.getParameter("username");
// String password = request.getParameter("password");
//UserInfo uinfo = new UserInfo();
// uinfo.setUsername(username);
// uinfo.setPassword(password);
System.out.println("I am in process");
PrintWriter out = response.getWriter();
String un, pw,phn;
un = request.getParameter("Usrnm");
System.out.println(un);
pw = request.getParameter("pwd");
System.out.println(pw);
phn = request.getParameter("phn");
System.out.println(phn);
Authenticate auth = new Authenticate();
int check = auth.AuthenticateUser(un, pw,phn);
System.out.println("My result is" + check);
if (check==1){
out.print(1);
} else{
out.print(0);
}
}
}
Android アプリでは、以下のコードを使用して応答オブジェクトを呼び出すことができます。通信用のカスタム HTTP クライアント クラスを追加してください。
response = CustomHttpClient.executeHttpPost("http://192.1..11../HelloWorldServlet/LoginDriverServlet", postParameters);
これで、応答オブジェクトを操作して、ニーズに合わせて成形できます
if (res.equals("1")) {
Log.e("CHeck4 response","i m here");
Log.e("CHeck response", res);
//error.setText("Correct Username or Password");
Intent intent = new Intent(getApplicationContext(), SetStatus.class);
startActivity(intent);
setContentView(R.layout.activity_set_status);
Toast.makeText(getApplicationContext(), "Loggin In", Toast.LENGTH_LONG).show();
} else if(res.equals("0")) {
//error.setText("Sorry!! Incorrect Username or Password");
Log.e("CHeck3 response", "i m here");
Toast.makeText(getApplicationContext(), "Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
Intent intent = new Intent();
//Username or Password",Toast.LENGTH_LONG).show();
}
Hope you get it..otherwise coments are welcomed :)