開發一個app與後臺數據庫交互,基於mysql+jdbc+tomcat,沒有使用DBUtils或jdbc框架,純粹底層jdbc實現.
之後逐步改用Spring框架,優化mysql,進一步部署tomcat等等,如今項目剛剛起步,還有不少不懂的東西,得慢慢來......
這幾天踩了不少坑,說得誇張點真是踩到我沒有知覺,但願能幫助別人少踩坑...php
2.github
這是源碼地址,包括先後端與建表等全部代碼.
(歡迎star)html
IDE就不說了,重點說一下mysql與tomcat9的安裝前端
這個是目前比較新的mysql版本.java
服務器系統是centos
其餘系統安裝看這裏mysql
centos使用yum命令安裝(參考連接)android
sudo yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
sudo yum install mysql-community-server
sudo service mysqld start
sudo grep 'temporary password' /var/log/mysqld.log
mysql -u root -p
輸入上一步看到的密碼git
alter mysql.user 'root'@'localhost' identified by 'password';
注意新版本的mysql不能使用太弱的密碼
若是出現以下提示
則說明密碼太弱了,請使用一個更高強度的密碼github
use mysql; update user set host='%' where user='root';
這個能夠根據本身的須要去修改,host='%'代表容許全部的ip登陸,也能夠設置特定的ip,若使用host='%'的話建議新建一個用戶配置相應的權限.web
因爲做者使用的是阿里雲的服務器,沒配置防火牆的話遠程鏈接不上,所以須要手動配置,如圖
sql
其中受權對象能夠根據本身的須要更改,0.0.0.0/0表示容許全部的ip.
<br><br>
做者使用的是scp命令,不會的能夠看這裏
scp apache-tomcat-xxxx.tar.gz username@xx.xx.xx.xx:/
改爲本身的用戶名和ip
mkdir /usr/local/tomcat mv apache-tomcat-xxxx.tar.gz /usr/local/tomcat tar -xzvf apache-tomcat-xxx.tar.gz
修改conf/server.xml文件,通常只需修改
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
中的8080端口,修改這個端口便可
這個懶的話(好比做者)能夠不改
運行bin目錄下的startup.sh
cd bin ./startup.sh
瀏覽器輸入
服務器IP:端口
若出現
則表示成功.
建議配置開機啓動,修改/etc/rc.local文件
vim /etc/rc.local 添加 sh /usr/local/tomcat/bin/startup.sh
這個根據本身的tomcat安裝路徑修改,指定bin下的startup.sh便可
建立用戶表,這裏簡化操做(好吧我喜歡偷懶)就不建立新用戶不受權了
這是一個在本地用root登陸的示例,請根據實際狀況建立並受權用戶.
CREATE DATABASE userinfo; USE userinfo; CREATE TABLE user ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name CHAR(30) NULL, password CHAR(30) NULL );
mysql -u root -p < user.sql
選擇web application
選好路徑,改好名字後finish
建立一個叫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
總共4個包
這個是鏈接數據庫的類,純粹的底層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");
注意對應.
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; } }
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.getName()); preparedStatement.setString(2,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進去了.........我......
好吧說多了都是淚,仍是函數用得不夠熟練.<br><br>
因此在這個例子中
return preparedStatement.execute();
確定返回false,因此纔會數據庫這邊insert進去,但前端顯示註冊失敗(這個bug做者找了好久......)
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,HttpServletResponse httpServletResponse) throws IOException, 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,HttpServletResponse httpServletResponse) throws IOException,ServletException { this.doPost(httpServletRequest,httpServletResponse); } @Override protected void doPost(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException,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錯誤碼與出錯信息 } } }
<?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>是訪問的路徑
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>Welcome</title> </head> <body> Hello web. </body> </html>
<br><br><br>
做者用的是IDEA,Eclipse的打包請看這裏
(那個lib文件夾被擋住了.....)
web.xml這個須要在WEB-INF目錄裏,Hello.html在WEB-INF外面
把打包好的.war文件上傳到服務器的tomcat的/webapps目錄下的
scp ***.war username@xxx.xxx.xxx.xxx:/usr/local/tomcat/webapps
注意改爲本身的webapps目錄.
在瀏覽器輸入
服務器IP:端口/項目/Hello.html
做者是在本地上開了tomcat後測試的
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, 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綁定事件,而後設置兩個Toast提示信息.
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",name,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",name,password); MyThread myThread = new MyThread("http://本機IP:8080/cx/SignIn",name,password);
這兩行換成本身的ip,本地ip的話能夠用ipconfig或ifconfig查看,修改了默認端口的話也把端口一塊兒改了.
路徑的話就是
端口/web項目名/Servlet名
web項目名是再打成war包時設置的,Servlet名在web.xml中的<servlet>的子元素<servlet-name>設置,與java源碼中的@WebServlet()註解中的一致
另一個要注意的就是線程問題,須要新開一個線程進行http的鏈接
前端頁面部分很簡單,就兩個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>
隨便輸入用戶名與密碼
查看數據庫
這裏沒有加密保存,後期會添加加密保存
perfect!
這個錯誤在加載驅動錯誤時也可能會出現這個錯誤,所以要確保打成war包時lib目錄正確且jar包版本正確.
還有就是因爲這個是jdbc的底層實現,注意手寫的sql語句不能錯
千萬千萬別像我這樣:
這個須要在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" />
服務器的話通常會有相應的相應的網頁界面配置,好比做者的是阿里雲服務器,固然也能夠手動配置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
因爲從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
兩種建議:
在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" />
從android4.0開始,聯網不能再主線程操做,萬一網絡很差就會卡死,因此有關聯網的操做都須要新開一個線程,不能在主線程操做.
這個bug做者找了好久,http鏈接沒問題,服務器沒問題,數據庫沒問題,前端代碼沒問題,而後去了stackoverflow,發現是AVD的問題,我.......
簡單來講就是卸載了再重啓AVD,竟然成功了.....
參考網站
1.Android 經過Web服務器與Mysql數據庫交互
2.Android高版本聯網失敗
3.IDEA 部署Web項目
4.PreparedStatement的executeQuery、executeUpdate和execute
5.preparedstatement execute()操做成功!可是返回false
6.HttpServletResponse(一)
7.HttpServletResponse(二)
8.HttpServletRequest
9.HttpUrlConnection
10.java.net.socketexception