android客户端连接infomix数据库登录

来源:互联网 发布:怎么利用网络博客赚钱 编辑:程序博客网 时间:2024/06/01 10:48

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);}


// 获取postpublic 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();// 存储ideditor.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 blocke.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=pcString 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()方法一样...}


原创粉丝点击