需求:AndroID客户端连接服务器端MysqL数据库中的内容
环境部署
服务器:apache-tomcat-8.5.9
语言版本:Java 1.8.0_101
编译环境:Eclipse
androID Studio
调用jar包:httpclIEnt-4.2.5,httpcore-4.2.4 //httpClIEnt父类
mysql-connector-java-5.1.40-bin //用于连接MysqL数据库
思路:涉及到服务器端MysqL数据库安装、web应用部分开发和AndroID客户端开发三个部分
步骤:
1、MysqL数据库安装
a、先安装MysqL-installer-community-5.7.17.0,其中在Setup Type上选择“Server only”,然后记住数据库端口号和账号(例如:root)密码(例如:123456),如下图:
b、安装成功验证。命令行窗口输入密码,然后输入显示所有数据库命令:show databases; 一定要有分号,并按回车。
c、NavicatforMysqL下载及使用。注册,然后连接数据库,输入密码后,能够看到已存在的数据库,可以在其中进行相关数据库和数据表的创建 *** 作。
(具体以参考资料中的内容为主)
2、web应用部分开发
a、新建servlet,并且配置好web.xml中的相应信息(在WebContent下的WEB-INF文件夹下加入web.xml文件来连接servlet与Jsp前端),此外还需在libs中添加mysql-connector-java-5.1.37-bin.jar文件,代码如下:
package com.Servlet; import java.io.IOException; import javax.servlet.servletexception; import javax.servlet.annotation.WebServlet; import javax.servlet.http.httpServlet; import javax.servlet.http.httpServletRequest; import javax.servlet.http.httpServletResponse; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import com.DBTool.DBUtil; @WebServlet("/Servlet") public class Login extends httpServlet { private static final long serialVersionUID = L; /** * @see httpServlet#httpServlet() */ public Login() { super(); // Todo auto-generated constructor stub } /** * @see httpServlet#doGet(httpServletRequest request,httpServletResponse response) */ protected voID doGet(httpServletRequest request,httpServletResponse response) throws servletexception,IOException { // Todo auto-generated method stub response.getWriter().append("Served at: ").append(request.getcontextpath()); } /** * @see httpServlet#doPost(httpServletRequest request,httpServletResponse response) */ protected voID doPost(httpServletRequest request,IOException { String ID = request.getParameter("ID"); String PW= request.getParameter("PW"); boolean type=false; response.setContentType("text/HTML; charset=UTF-8"); PrintWriter out = response.getWriter(); try { Connection con=DBUtil.getConnection(); Statement stmt=con.createStatement(); //MysqL数据库中的数据表,表名叫:demotable ,需要自己预先在数据库中进行创建,包含相应的字段和记录。 String sql="select * from MysqL.demotable where uID="+ID+" and pwd="+PW; ResultSet rs=stmt.executequery(sql); while(rs.next()) { type=true; } } catch(Exception ex) { ex.printstacktrace(); } finally { DBUtil.Close(); out.print(type); out.flush(); out.close(); } } }
web.xml内容如下:
<?xml version="1.0" enCoding="UTF-8"?><web-app ID="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>web</display-name> <servlet> <display-name>Login</display-name> <servlet-name>Login</servlet-name> <servlet-class>com.Servlet.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/Login</url-pattern> </servlet-mapping> <welcome-file-List> <welcome-file>index.HTML</welcome-file> <welcome-file>index.Jsp</welcome-file> </welcome-file-List> </web-app>
b、前端界面设计(TestPage.Jsp)如下:
<%@ page language="java" ContentType="text/HTML; charset=UTF-8" pageEnCoding="UTF-8"%> <!DOCTYPE HTML PUBliC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/HTML4/loose.dtd"> <HTML> <head> <Meta http-equiv="Content-Type" content="text/HTML; charset=UTF-8"> <Title>Insert Title here</Title> </head> <body> <form ID="from" action="Login" method="post"> <table> <tr><td>用户名</td><td><input type="text" name="ID"></td></tr> <tr><td>密码</td><td><input type="password" name="PW"></td></tr> <tr><td colspan="2" align="center"><input type="submit" value="登陆"/></td></tr> </table> </form> </body> </HTML>
c、在java Resources下的src文件夹中新建com.DBTool包,用作数据池来连接数据库,在包中建立DBUtil类实现功能,代码如下:
package com.DBTool; import java.sql.*; public class DBUtil { //其中MysqL是数据库名称,在MysqL57版本的数据库中已经预先新建完成;3306是MysqL数据库的端口号。 private static String url="jdbc:MysqL://localhost:3306/MysqL"; //com.MysqL.jdbc.Driver是mysql-connector-java-5.1.40中的驱动包路径 private static String driverClass="com.MysqL.jdbc.Driver"; //MysqL的账号和密码是在安装MysqL中进行设置的,这里拿来用即可。 private static String username="root"; private static String password="123456"; private static Connection conn; //装载驱动 static{ try{ Class.forname(driverClass); } catch(ClassNotFoundException e){ e.printstacktrace(); } } //获取数据库连接 public static Connection getConnection(){ try{ conn=DriverManager.getConnection(url,username,password); } catch(sqlException e){ e.printstacktrace(); } return conn; } //建立数据库连接 public static voID main(String[] args){ Connection conn=DBUtil.getConnection(); if(conn!=null){ System.out.println("数据库连接成功"); } else{ System.out.println("数据库连接失败"); } } //关闭数据库连接 public static voID Close(){ if(conn!=null){ try{ conn.close(); } catch(sqlException e){ e.printstacktrace(); } } } }
d、运行服务器,测试是否成功搭建。
3、AndroID部分开发
仅附上核心部分代码,如下:
public voID SendByhttpClIEnt(final String ID,final String pw){ new Thread(new Runnable() { @OverrIDe public voID run() { try { httpClIEnt httpclIEnt=new DefaulthttpClIEnt(); httpPost httpPost=new httpPost("http://web应用部署服务器上的IP地址:/httpClIEntDemo/Login");//服务器地址,指向Servlet List<nameValuePair> params=new ArrayList<nameValuePair>();//将ID和pw装入List params.add(new BasicnameValuePair("ID",ID)); params.add(new BasicnameValuePair("PW",pw)); final UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"utf-8");//以UTF-8格式发送 httpPost.setEntity(entity); httpResponse httpResponse= httpclIEnt.execute(httpPost); if(httpResponse.getStatusline().getStatusCode()==200)//在200毫秒之内接收到返回值 { httpentity entity=httpResponse.getEntity(); String response=EntityUtils.toString(entity1,"utf-8");//以UTF-8格式解析 Message message=new Message(); message.what=USER_LOGIN; message.obj=response; handler.sendMessage(message);使用Message传递消息给线程 } } catch (Exception e) { e.printstacktrace(); } } }).start(); }
最终,测试结果图,如下:
参考资料:
http://transcoder.tradaquan.com/from=1017649e/bd_page_type=1/ssid=0/uid=0/pu=usm%401%2Csz%401320_2001%2Cta%40iphone_1_9.2_3_601/baiduid=3B77D44CFFB44688FD602EAD8A663022/w=0_10_/t=iphone/l=3/tc?ref=www_iphone&lid=9684581900815139314&order=2&fm=alhm&h5ad=1&srd=1&dict=32&tj=h5_mobile_2_0_10_title&w_qd=IlPT2AEptyoA_ykzv39b7vOxASxPcYSfDwWEKKelwb6TYslhS_&sec=22021&di=4d89010ccd0ca0f7&bdenc=1&tch=124.133.103.675.1.561&nsrc=IlPT2AEptyoA_yixCFOxXnANedT62v3IEQGG_ytK1DK6mlrte4viZQRAUSD8L7qYZpPPtCPQpxkCwnWh_7YskNYWgK&eqid=86668bed7c43800010000003594fbeac&wd=&clk_info=%7B%22srcid%22%3A%221599%22%2C%22tplname%22%3A%22h5_mobile%22%2C%22t%22%3A1498398423079%2C%22sig%22%3A%2242687%22%2C%22xpath%22%3A%22div-a-h3%22%7DMysqL数据库安装与配置详解
http://blog.csdn.NET/qq_14923661/article/details/50461696 AndroID平台实现与Apache Tomcat服务器数据交互(MysqL数据库)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android实现与Apache Tomcat服务器数据交互(MySql数据库)全部内容,希望文章能够帮你解决Android实现与Apache Tomcat服务器数据交互(MySql数据库)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)