欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫)

 更新時間:2017年06月27日 09:27:54   作者:gislaozhang  
本篇文章主要介紹了Android實現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫),具有一定的參考價值,感興趣的小伙伴們可以參考一下

需求:Android客戶端連接服務(wù)器端MySQL數(shù)據(jù)庫中的內(nèi)容

環(huán)境部署

服務(wù)器:apache-tomcat-8.5.9

語言版本:Java 1.8.0_101

編譯環(huán)境:Eclipse

                  android Studio

調(diào)用jar包:httpclient-4.2.5,httpcore-4.2.4 //HttpClient父類

                   mysql-connector-java-5.1.40-bin //用于連接mysql數(shù)據(jù)庫

思路:涉及到服務(wù)器端mysql數(shù)據(jù)庫安裝、web應(yīng)用部分開發(fā)和Android客戶端開發(fā)三個部分

步驟:

1、mysql數(shù)據(jù)庫安裝

a、先安裝mysql-installer-community-5.7.17.0,其中在Setup Type上選擇“Server only”,然后記住數(shù)據(jù)庫端口號和賬號(例如:root)密碼(例如:123456),如下圖:

b、安裝成功驗證。命令行窗口輸入密碼,然后輸入顯示所有數(shù)據(jù)庫命令:show databases; 一定要有分號,并按回車。

c、NavicatforMySQL下載及使用。注冊,然后連接數(shù)據(jù)庫,輸入密碼后,能夠看到已存在的數(shù)據(jù)庫,可以在其中進(jìn)行相關(guān)數(shù)據(jù)庫和數(shù)據(jù)表的創(chuàng)建操作。

(具體以參考資料中的內(nèi)容為主)

2、web應(yīng)用部分開發(fā)

a、新建servlet,并且配置好web.xml中的相應(yīng)信息(在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, HttpServletResponse response) throws ServletException, 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數(shù)據(jù)庫中的數(shù)據(jù)表,表名叫:demotable ,需要自己預(yù)先在數(shù)據(jù)庫中進(jìn)行創(chuàng)建,包含相應(yīng)的字段和記錄。
       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內(nèi)容如下:

 

<?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、前端界面設(shè)計(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包,用作數(shù)據(jù)池來連接數(shù)據(jù)庫,在包中建立DBUtil類實現(xiàn)功能,代碼如下:

package com.DBTool;
 
 
 import java.sql.*;
 
 public class DBUtil {
   //其中mysql是數(shù)據(jù)庫名稱,在mysql57版本的數(shù)據(jù)庫中已經(jīng)預(yù)先新建完成;3306是mysql數(shù)據(jù)庫的端口號。
   private static String url="jdbc:mysql://localhost:3306/mysql";
   //com.mysql.jdbc.Driver是mysql-connector-java-5.1.40中的驅(qū)動包路徑
   private static String driverClass="com.mysql.jdbc.Driver";
   //mysql的賬號和密碼是在安裝mysql中進(jìn)行設(shè)置的,這里拿來用即可。
   private static String username="root";
   private static String password="123456";
   private static Connection conn;
   //裝載驅(qū)動
   static{
     try{
       Class.forName(driverClass);
     }
     catch(ClassNotFoundException e){
       e.printStackTrace();
     }
   }
   //獲取數(shù)據(jù)庫連接
   public static Connection getConnection(){
     try{
       conn=DriverManager.getConnection(url,username,password);
     }
     catch(SQLException e){
       e.printStackTrace();
     }
     return conn;
   }
   //建立數(shù)據(jù)庫連接
   public static void main(String[] args){
     Connection conn=DBUtil.getConnection();
     if(conn!=null){
       System.out.println("數(shù)據(jù)庫連接成功");
     }
     else{
       System.out.println("數(shù)據(jù)庫連接失敗");
     }
   }
   //關(guān)閉數(shù)據(jù)庫連接
   public static void Close(){
     if(conn!=null){
       try{
         conn.close();
       }
       catch(SQLException e){
         e.printStackTrace();
       }
     }
   }
 }

d、運(yùn)行服務(wù)器,測試是否成功搭建。

3、Android部分開發(fā)

僅附上核心部分代碼,如下:

 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應(yīng)用部署服務(wù)器上的IP地址:/HttpClientDemo/Login");//服務(wù)器地址,指向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格式發(fā)送 
           httpPost.setEntity(entity); 
           HttpResponse httpResponse= httpclient.execute(httpPost); 
           if(httpResponse.getStatusLine().getStatusCode()==200)//在200毫秒之內(nèi)接收到返回值 
           { 
             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(); 
   } 

最終,測試結(jié)果圖,如下:

參考資料:

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數(shù)據(jù)庫安裝與配置詳解

http://blog.csdn.NET/qq_14923661/article/details/50461696  Android平臺實現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論