android 클 라 이언 트 연결 infomix 데이터베이스 로그 인

android 
//       
	public String queryLogin(String inputUsername, String inputPwd) {
		String url = "http://192.168.1.105:8080/epay_server/LoginServlet?username="
				+ inputUsername + "&pwd=" + inputPwd;
		//     
		HttpPost request = HttpUtil.getHttpPost(url);
		//     
		HttpResponse response = HttpUtil.getHttpResponse(request);
		//     
		return HttpUtil.getValue(response);
	}
//   post
	public static HttpPost getHttpPost(String url) {
		HttpPost request = new HttpPost(url);
		return request;
	}
// post  
	public static HttpResponse getHttpResponse(HttpPost request) {
		try {
			System.out.println("   request==>" + request);
			HttpResponse response = new DefaultHttpClient().execute(request);
			System.out.println("   response==>" + response);
			return response;
		} catch (ClientProtocolException e) {
			System.out.println("clientProtocol  ");
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			System.out.println("io  ");
			e.printStackTrace();
			return null;
		}
	}
//       
	public static String getValue(HttpResponse response) {
		String result = "";
		if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
			//       
			try {
				result = EntityUtils.toString(response.getEntity());
				System.out.println("    ==>" + result);
			} catch (ParseException e) {
				System.out.println("ParseException  ");
				e.printStackTrace();

			} catch (IOException e) {
				System.out.println("io  ...");
				e.printStackTrace();
			}
		} else {
			System.out.println("null...");
		}
		return result;
	}

로그 인 버튼 클릭:
					//        
					String inputUsername = etUsername.getText().toString()
							.trim();
					String inputPwd = etPwd.getText().toString().trim();
					// connectNet.start(); //         
					String userID = queryLogin(inputUsername, inputPwd);
					System.out.println("id===>" + userID);
					if (!userID.equals("")) {
						Toast.makeText(mContext, "    !", Toast.LENGTH_SHORT)
								.show();
						//   id
						editor.putString(GlobalConstant.SHA_USER_ID, userID);
						//        
						editor.commit();
						Intent intent = new Intent(mContext, MainActivity.class);
						startActivity(intent);
						cleanEditText(); //      
					} else {
						Toast.makeText(mContext, "        !",
								Toast.LENGTH_SHORT).show();
					}
				

서버 쪽:
dao.java:
/**
 *         :    
 * @author smalt
 *
 */
public interface DBDao {
	public void getUpdate();
}

daoImpl:
public class LoginDaoImpl implements LoginDao {
	public Login getLogin(String username, String password) {
		//             ,    ,     
		Login login = null;
//		  MD5      
		String pwdMD5=MD5Util.getMD5_Fouction1(password);
		//      
		DBUtil db = new DBUtil();
		Connection con = db.getConnectionInformix();
		if (con != null) {

			try { //     
				String sql = "select login,password from ec_pcuser where login=? and password=?";
				PreparedStatement pstmt = con.prepareStatement(sql);
				pstmt.setString(1, username);
				pstmt.setString(2, pwdMD5);
				ResultSet rs = pstmt.executeQuery();
				if (rs.next()) {
					login = new Login();
					login.setUsername(username);
					login.setPwd(password);
				}
				if (login!=null) {
					System.out.println("username=" + login.getUsername() + ",pwd="
							+ login.getPwd());
				}else {
					System.out.println("null...");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("sql    !");
			} finally {
				db.closeConnection(con);
			}
		}
		//     
		return login;
	}

Servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=GBK");

		//      
		// http://192.168.1.105:8080/epay_server/LoginServlet?username=pc&pwd=pc
		String userName = request.getParameter("username");
		String userPwd = request.getParameter("pwd");
		PrintWriter out = response.getWriter();
		DBQueryImpl db = new DBQueryImpl();
		//          id,     ,     
		String result = db.getID(userName, userPwd);
		out.write(result);
		out.close();

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response); //           doGet()    ...
	}

좋은 웹페이지 즐겨찾기