tomcat apache mysql_Android实现与Apache Tomcat服务器数据交互(MySql数据库)
需求: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, 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數據庫中的數據表,表名叫: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內容如下:
web
Login
Login
com.Servlet.Login
Login
/Login
index.html
index.jsp
b、前端界面設計(TestPage.jsp)如下:
Insert title here| 用戶名 | |
| 密碼 | |
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 params=new ArrayList();//將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();
}
最終,測試結果圖,如下:
參考資料:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的tomcat apache mysql_Android实现与Apache Tomcat服务器数据交互(MySql数据库)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql avg 报错_MySQL报错
- 下一篇: lua把userdata写入mysql_