android通過web與後臺資料庫互動
1.背景
開發一個app與後臺資料庫互動,基於mysql+jdbc+tomcat,沒有使用DBUtils或jdbc框架,純粹底層jdbc實現. 以後逐步改用Spring框架,優化mysql,進一步部署tomcat等等,現在專案剛剛起步,還有很多不懂的東西,得慢慢來...... 這幾天踩了很多坑,說得誇張點真是踩到我沒有知覺,希望能幫助別人少踩坑...
2.開發環境
- 系統 : win10
- IDE : Android Studio 3.5.1,IntelliJ IDEA 2019.02
- DBMS : Mysql 8.0.17
- web伺服器: tomcat9
3.相關資源
-
1.相關jar包: mysqlV8.0.17驅動
-
2.github 這是原始碼地址,包括前後端與建表等所有程式碼. (歡迎star)
-
3.碼雲 這也是原始碼地址
4.配置開發環境
IDE就不說了,重點說一下mysql與tomcat9的安裝
一. 安裝Mysql8.0.17
這個是目前比較新的mysql版本.
伺服器系統是centos 其他系統安裝看這裡
centos使用yum命令安裝(參考連結)
(1) 下載mysql
sudo yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
複製程式碼
(2) 安裝mysql
sudo yum install mysql-community-server
複製程式碼
(3) 啟動服務
sudo service mysqld start
複製程式碼
(4) 檢視初始化密碼,用於下一步設定自己的root密碼
sudo grep 'temporary password' /var/log/mysqld.log
複製程式碼
(5) 本地使用root登入
mysql -u root -p
複製程式碼
輸入上一步看到的密碼
(6) 更改密碼
alter mysql.user 'root'@'localhost' identified by 'password' ;
複製程式碼
注意新版本的mysql不能使用太弱的密碼 如果出現如下提示
則說明密碼太弱了,請使用一個更高強度的密碼(7) 允許外部訪問
use mysql;
update user set host='%' where user='root';
複製程式碼
這個可以根據自己的需要去修改,host='%'表明允許所有的ip登入,也可以設定特定的ip,若使用host='%'的話建議新建一個使用者配置相應的許可權.
(8) 配置防火牆(可選)
由於作者使用的是阿里雲的伺服器,沒配置防火牆的話遠端連線不上,因此需要手動配置,如圖
其中授權物件可以根據自己的需要更改,0.0.0.0/0表示允許所有的ip.
二.安裝tomcat9
(1) 先去官網下載,下載後上傳檔案到伺服器
作者使用的是scp命令,不會的可以看這裡scp apache-tomcat-xxxx.tar.gz [email protected]:/
複製程式碼
改成自己的使用者名稱和ip
(2) 連線到伺服器,解壓壓縮包
mkdir /usr/local/tomcat
mv apache-tomcat-xxxx.tar.gz /usr/local/tomcat
tar -xzvf apache-tomcat-xxx.tar.gz
複製程式碼
(3) 修改tomcat預設埠(可選)
修改conf/server.xml檔案,一般只需修改
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
複製程式碼
中的8080埠,修改這個埠即可 這個懶的話(比如作者)可以不改
(4) 啟動tomcat
執行bin目錄下的startup.sh
cd bin
./startup.sh
複製程式碼
(5) 測試
瀏覽器輸入
伺服器IP:埠
複製程式碼
若出現
則表示成功.(6)開機啟動
建議配置開機啟動,修改/etc/rc.local檔案
vim /etc/rc.local
新增
sh /usr/local/tomcat/bin/startup.sh
複製程式碼
這個根據自己的tomcat安裝路徑修改,指定bin下的startup.sh即可
5.建庫建表
建立使用者表,這裡簡化操作(好吧我喜歡偷懶)就不建立新使用者不授權了 這是一個在本地用root登入的示例,請根據實際情況建立並授權使用者.
(1) 建立user.sql
CREATE DATABASE userinfo;
USE userinfo;
CREATE TABLE user
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,name CHAR(30) NULL,password CHAR(30) NULL
);
複製程式碼
(2) 匯入到資料庫
mysql -u root -p < user.sql
複製程式碼
6.後端部分
(1) 建立專案
選擇web application
選好路徑,改好名字後finish(2) 新增jar包
建立一個叫lib的目錄
新增兩個jar包: mysql-connector-java-8.0.17.jar javax.servlet-api-4.0.1.jar 開啟Project Structure Modules--> + --> JARs or directories 選擇剛才新建的lib下的兩個jar包 打勾,apply(3) 建立包與類
總共4個包
- com.servlet 用於處理來自前端的請求,包含SignUp.java,SignIn.java
- com.util 主要功能是資料庫連線,包含DBUtils.java
- com.entity 使用者類,包含User.java
- com.dao 操作使用者類的類,包含UserDao.java
(4) 先來處理DBUtils類
這個是連線資料庫的類,純粹的底層jdbc實現,注意驅動版本.
package com.util;
import java.sql.*;
public class DBUtils {
private static Connection connection = null;
public static Connection getConnection()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/資料庫名字";
String usename = "賬號";
String password = "密碼";
connection = DriverManager.getConnection(url,usename,password);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return connection;
}
public static void closeConnection()
{
if(connection != null)
{
try {
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
複製程式碼
主要就是獲取連線與關閉連線兩個函式.
String url = "jdbc:mysql://127.0.0.1:3306/資料庫名字";
String usename = "賬號";
String password = "密碼";
複製程式碼
這幾行根據自己的使用者名稱,密碼,伺服器ip和庫名修改
注意,mysql8.0以上使用的註冊驅動的語句是
Class.forName("com.mysql.cj.jdbc.Driver");
複製程式碼
舊版的是
Class.forName("com.mysql.jdbc.Driver");
複製程式碼
注意對應.
(5) 接下來處理User類
User類比較簡單,就是就三個欄位與getter,setter
package com.entity;
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
複製程式碼
(6) 接下來是UserDao
package com.dao;
import com.entity.User;
import com.util.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
public boolean query(User user)
{
Connection connection = DBUtils.getConnection();
String sql = "select * from user where name = ? and password = ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,user.getName());
preparedStatement.setString(2,user.getPassword());
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet.next();
}
catch (SQLException e)
{
e.printStackTrace();
return false;
}
finally {
DBUtils.closeConnection();
}
}
public boolean add(User user)
{
Connection connection = DBUtils.getConnection();
String sql = "insert into user(name,password) values(?,?)";
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,user.getPassword());
preparedStatement.executeUpdate();
return preparedStatement.getUpdateCount() != 0;
}
catch (SQLException e)
{
e.printStackTrace();
return false;
}
finally {
DBUtils.closeConnection();
}
}
}
複製程式碼
主要就是查詢與新增操作,查詢操作中存在該使用者就返回true,否則返回false 新增操作中使用executeUpdate()與getUpdateCount() != 0.注意不能直接使用
return preparedStatement.execute();
複製程式碼
去代替
preparedStatement.executeUpdate();
return preparedStatement.getUpdateCount() != 0;
複製程式碼
咋一看好像沒有什麼問題,那天晚上我測試的時候問題可大了,android那邊顯示註冊失敗,但是資料庫這邊的卻insert進去了.........我......
好吧說多了都是淚,還是函式用得不夠熟練.
- 一般來說select使用executeQuery() executeQuery()返回ResultSet,表示結果集,儲存了select語句的執行結果,配合next()使用
- delete,insert,update使用executeUpdate() executeUpdate()返回的是一個整數,表示受影響的行數,即delete,update修改的行數,對於drop,create操作返回0
- create,drop使用execute()
execute()的返回值是這樣的:
- 如果第一個結果是ResultSet物件,則返回true
- 如果第一個結果是更新計數或者沒有結果則返回false
所以在這個例子中
return preparedStatement.execute();
複製程式碼
肯定返回false,所以才會資料庫這邊insert進去,但前端顯示註冊失敗(這個bug作者找了很久......)
(7) servlet包的SignIn與SignUp類
SingIn類用於處理登入,呼叫jdbc檢視資料庫是否有對應的使用者 SignUp類用於處理註冊,把user新增到資料庫中 這兩個使用的是http連線,後期作者會採用https加密連線.
SignIn.java
package com.servlet;
import com.dao.UserDao;
import com.entity.User;
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.IOException;
@WebServlet("/SignIn")
public class SingIn extends HttpServlet {
@Override
protected void doGet(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException,ServletException
{
this.doPost(httpServletRequest,httpServletResponse);
}
@Override
protected void doPost(HttpServletRequest httpServletRequest,ServletException
{
httpServletRequest.setCharacterEncoding("utf-8");
httpServletResponse.setCharacterEncoding("utf-8");
httpServletResponse.setContentType("text/plain;charset=utf-8");//設定相應型別為html,編碼為utf-8
String name = httpServletRequest.getParameter("name");
String password = httpServletRequest.getParameter("password");
UserDao userDao = new UserDao();
User user = new User();
user.setName(name);
user.setPassword(password);
if(!userDao.query(user))//若查詢失敗
{
httpServletResponse.sendError(204,"query failed.");//設定204錯誤碼與出錯資訊
}
}
}
複製程式碼
@WebServlet("/SignIn")
複製程式碼
這行程式碼表示這是一個名字叫SignIn的servlet,可用於實現servlet與url的對映,如果不在這裡新增這個註解,則需要在WEB-INF目錄下的web.xml新增一個
<servlet-mapping>
複製程式碼
叫servlet的對映
httpServletResponse.setContentType("text/plain;charset=utf-8");//設定相應型別為html,編碼為utf-8
複製程式碼
這行程式碼設定響應型別與編碼
String name = httpServletRequest.getParameter("name");
String password = httpServletRequest.getParameter("password");
複製程式碼
HttpServletRequest.getParameter(String name)方法表示根據name獲取相應的引數
下面是SignUp.java
package com.servlet;
import com.dao.UserDao;
import com.entity.User;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.IOException;
@WebServlet("/SignUp")
public class SignUp extends HttpServlet {
@Override
protected void doGet(HttpServletRequest httpServletRequest,ServletException
{
httpServletRequest.setCharacterEncoding("utf-8");
httpServletResponse.setCharacterEncoding("utf-8");//設定編碼防止中文亂碼
httpServletResponse.setContentType("text/plain;charset=utf-8");//設定相應型別為html,編碼為utf-8
String name = httpServletRequest.getParameter("name");//根據name獲取引數
String password = httpServletRequest.getParameter("password");//根據password獲取引數
UserDao userDao = new UserDao();
User user = new User();
user.setName(name);
user.setPassword(password);
if(!userDao.add(user)) //若新增失敗
{
httpServletResponse.sendError(204,"add failed.");//設定204錯誤碼與出錯資訊
}
}
}
複製程式碼
(8) 新增servlet到web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SignIn</servlet-name>
<servlet-class>com.servlet.SingIn</servlet-class>
</servlet>
<servlet>
<servlet-name>SignUp</servlet-name>
<servlet-class>com.servlet.SignUp</servlet-class>
</servlet>
</web-app>
複製程式碼
要把剛才建立的Servlet新增進web.xml,在<servlet>中新增子元素<servlet-name>與<servlet-class> <servlet-name>是Servlet的名字,最好與類名一致. <servlet-class>是Servlet類的位置. 如果在Servlet類中沒有新增
@WebServlet("/xxxx")
複製程式碼
這個註解,則需要在web.xml中新增
<servlet-mapping>
<servlet-name>SignIn</servlet-name>
<url-pattern>/SignIn</url-pattern>
</servlet-mapping>
複製程式碼
其中<servlet-name>與<servlet>中的子元素<servlet-name>中的值一致 <url-pattern>是訪問的路徑
(9) 最後新增一個叫Hello.html的html檔案用於測試.
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
Hello web.
</body>
</html>
複製程式碼
7.打包釋出
作者用的是IDEA,Eclipse的打包請看這裡
(1) 開啟project structure
(2) 選擇Artifacts,Web Application:Archive
(3) 改名字,建立WEB-INF目錄與子目錄classes
(4) 選中classes,新增Module Output,選擇自己的web專案
(5) 新增jar包,選中lib目錄後新增jar包檔案
(那個lib資料夾被擋住了.....)
(6) 新增Hello.html與web.xml
web.xml這個需要在WEB-INF目錄裡,Hello.html在WEB-INF外面
(7) 打包,Build->Build Artifacts
(8) 上傳到伺服器
把打包好的.war檔案上傳到伺服器的tomcat的/webapps目錄下的
scp ***.war [email protected]:/usr/local/tomcat/webapps
複製程式碼
注意改成自己的webapps目錄.
(9) 測試
在瀏覽器輸入
伺服器IP:埠/專案/Hello.html
複製程式碼
作者是在本地上開了tomcat後測試的
8.前端頁面部分
(1) 新建工程
(2) MainActivity.java
package com.cx;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button signin = (Button) findViewById(R.id.signin);
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = ((EditText) findViewById(R.id.etname)).getText().toString();
String password = ((EditText) findViewById(R.id.etpassword)).getText().toString();
if (UserService.signIn(name,password))
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"登入成功",Toast.LENGTH_SHORT).show();
}
});
else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"登入失敗",Toast.LENGTH_SHORT).show();
}
});
}
}
});
Button signup = (Button) findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = ((EditText) findViewById(R.id.etname)).getText().toString();
String password = ((EditText) findViewById(R.id.etpassword)).getText().toString();
if (UserService.signUp(name,"註冊成功","註冊失敗",Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
}
複製程式碼
沒什麼好說的,就為兩個Button繫結事件,然後設定兩個Toast提示資訊.
(3) UserService.java
package com.cx;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class UserService {
public static boolean signIn(String name,String password) {
MyThread myThread = new MyThread("http://本機IP:8080/cx/SignIn",name,password);
try
{
myThread.start();
myThread.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return myThread.getResult();
}
public static boolean signUp(String name,String password) {
MyThread myThread = new MyThread("http://本機IP:8080/cx/SignUp",password);
try
{
myThread.start();
myThread.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return myThread.getResult();
}
}
class MyThread extends Thread
{
private String path;
private String name;
private String password;
private boolean result = false;
public MyThread(String path,String name,String password)
{
this.path = path;
this.name = name;
this.password = password;
}
@Override
public void run()
{
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(8000);//設定連線超時時間
httpURLConnection.setReadTimeout(8000);//設定讀取超時時間
httpURLConnection.setRequestMethod("POST");//設定請求方法,post
String data = "name=" + URLEncoder.encode(name,"utf-8") + "&password=" + URLEncoder.encode(password,"utf-8");//設定資料
httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//設定響應型別
httpURLConnection.setRequestProperty("Content-Length",data.length() + "");//設定內容長度
httpURLConnection.setDoOutput(true);//允許輸出
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(data.getBytes("utf-8"));//寫入資料
result = (httpURLConnection.getResponseCode() == 200);
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean getResult()
{
return result;
}
}
複製程式碼
MyThread myThread = new MyThread("http://本機IP:8080/cx/SignUp",password);
MyThread myThread = new MyThread("http://本機IP:8080/cx/SignIn",password);
複製程式碼
這兩行換成自己的ip,本地ip的話可以用ipconfig或ifconfig檢視,修改了預設埠的話也把埠一起改了. 路徑的話就是
埠/web專案名/Servlet名
複製程式碼
web專案名是再打成war包時設定的,Servlet名在web.xml中的<servlet>的子元素<servlet-name>設定,與java原始碼中的@WebServlet()註解中的一致
另外一個要注意的就是執行緒問題,需要新開一個執行緒進行http的連線
(4) activity_main.xml
前端頁面部分很簡單,就兩個button,用於驗證功能.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用者名稱"
/>
<EditText
android:layout_width="300dp"
android:layout_height="60dp"
android:id="@+id/etname"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密碼"
/>
<EditText
android:layout_width="300dp"
android:layout_height="60dp"
android:id="@+id/etpassword"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="120dp"
android:layout_height="60dp"
android:text="註冊"
android:id="@+id/signup"
/>
<Button
android:layout_width="120dp"
android:layout_height="60dp"
android:text="登入"
android:id="@+id/signin"
/>
</LinearLayout>
</LinearLayout>
複製程式碼
9.測試
(1) 註冊測試
隨便輸入使用者名稱與密碼
檢視資料庫
這裡沒有加密儲存,後期會新增加密儲存(2) 登入測試
perfect!10.注意事項
(1) 資料庫的使用者名稱和密碼一定要設定正確,要不然會這樣提示
這個錯誤在載入驅動錯誤時也可能會出現這個錯誤,因此要確保打成war包時lib目錄正確且jar包版本正確. 還有就是由於這個是jdbc的底層實現,注意手寫的sql語句不能錯 千萬千萬別像我這樣:(2) 網路許可權問題
這個需要在AndroidManifest.xml新增網路許可權
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
複製程式碼
(3) 防火牆問題
伺服器的話一般會有相應的相應的網頁介面配置,比如作者的是阿里雲伺服器,當然也可以手動配置iptables 修改/etc/sysconfig/iptables
vim /etc/sysconfig/iptables
複製程式碼
新增
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
複製程式碼
重啟iptables
service iptables restart
複製程式碼
(4) 使用HTTP注意事項
由於從Android P開始,google預設要求使用加密連線,即要使用HTTPS,所以會禁止使用HTTP連線 使用HTTP連線時會出現以下異常
W/System.err: java.io.IOException: Cleartext HTTP traffic to **** not permitted
java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy
複製程式碼
兩種建議:
1 使用HTTPS
2 修改預設的AndroidManifest.xml使其允許HTTP連線
在res下新建一個資料夾xml,建立一個叫network_security_config.xml的檔案,檔案內容如下
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
複製程式碼
然後在AndroidMainfest.xml中加入
<application
android:networkSecurityConfig="@xml/network_security_config"
/>
複製程式碼
即可
另一種辦法是直接加入一句
android:usesCleartextTraffic="true"
複製程式碼
<application
android:usesCleartextTraffic="true"
/>
複製程式碼
(5) 執行緒問題
從android4.0開始,聯網不能再主執行緒操作,萬一網路不好就會卡死,所以有關聯網的操作都需要新開一個執行緒,不能在主執行緒操作.
(6) AVD問題
這個bug作者找了很久,http連線沒問題,伺服器沒問題,資料庫沒問題,前端程式碼沒問題,然後去了stackoverflow,發現是AVD的問題,我....... 簡單來說就是解除安裝了再重啟AVD,居然成功了.....11 最後
作者小白一枚,有什麼不對的地方請大家指正,評論作者會好好回覆的. 下面是我的CSDN部落格 CSDN
參考網站 1.Android 通過Web伺服器與Mysql資料庫互動
4.PreparedStatement的executeQuery、executeUpdate和execute