開發一個app與後臺數據庫交互,基於MySQL+原生JDBC+Tomcat,沒有使用DBUtils或JDBC框架,純粹底層jdbc實現.
之後逐步改用Spring框架,優化MySQL,進一步部署Tomcat等等,如今項目剛剛起步,還有不少不懂的東西,得慢慢來......
這幾天踩了不少坑,說得誇張點真是踩到筆者沒有知覺,但願能幫助別人少踩坑...php
說一下MySQL與Tomcat的安裝.html
這個是目前比較新的MySQL版本.
服務器系統是CentOS.
其餘系統安裝看這裏:前端
CentOS使用yum命令安裝:java
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
首先使用root登陸:mysql
mysql -u root -p
輸入上一步看到的密碼,接着使用alter修改密碼:android
alter mysql.user 'root'@'localhost' identified by 'password';
注意新版本的MySQL不能使用太弱的密碼.
若是出現以下提示:
則說明密碼太弱了,請使用一個更高強度的密碼.git
use mysql; update user set host='%' where user='root';
這個能夠根據本身的須要去修改,host='%'
代表容許全部的ip登陸,也能夠設置特定的ip,若使用host='%'
的話建議新建一個用戶配置相應的權限.github
通常來講須要在對應的雲廠商的防火牆配置中開啓響應端口,如圖:
其中受權對象能夠根據本身的須要更改,0.0.0.0/0
表示容許全部的ip.web
先去官網下載,下載後上傳文件到服務器:
筆者使用的是scp命令,使用不熟練的能夠戳這裏看看sql
scp apache-tomcat-xxxx.tar.gz username@xx.xx.xx.xx:/
改爲本身的用戶名和ip.
ssh鏈接到服務器,接着移動到/usr/local
並解壓:
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
文件,添加:
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
:
建立一個叫lib的目錄:
添加兩個jar包(jar包在文末提供下載連接):
打開Project Structure:Modules --> + --> JARs or directories
:
選擇剛纔新建的lib下的兩個jar包:
打勾,apply:
總共4個包
com.servlet
:用於處理來自前端的請求,包含SignUp.java
,SignIn.java
com.util
:主要功能是數據庫鏈接,包含DBUtils.java
com.entity
:實體類,包含User.java
com.dao
:操做用戶類的類,包含UserDao.java
鏈接數據庫的類,純粹的底層jdbc實現,注意驅動版本.
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和庫名修改.
注意,MySQL 8.0以上使用的註冊驅動的語句是:
Class.forName("com.mysql.cj.jdbc.Driver");
舊版的是:
Class.forName("com.mysql.jdbc.Driver");
User類比較簡單,就是就三個字段與getter,setter:
public class User { private int id; private String name; private String password; //三個getter與三個setter //... }
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進去了.........這.....
好吧說多了都是淚,仍是函數用得不夠熟練.
select
使用executeQuery()
,executeQuery()
返回ResultSet
,表示結果集,保存了select
語句的執行結果,配合next()
使用delete
,insert
,update
使用executeUpdate()
,executeUpdate()
返回的是一個整數,表示受影響的行數,即delete
,insert
,update
修改的行數,對於drop
,create
操做返回0create
,drop
使用execute()
,execute()
的返回值是這樣的:
ResultSet
對象,則返回true因此在這個例子中
return preparedStatement.execute();
確定返回false,因此纔會數據庫這邊insert進去,但前端顯示註冊失敗(這個bug筆者找了是真的久......)
servlet
包的SingIn
類用於處理登陸,調用JDBC查看數據庫是否有對應的用戶.SignUp
類用於處理註冊,把User添加到數據庫中.
目前使用的是HTTP鏈接,後期會考慮添加HTTPS支持.
SignIn.java
以下:
@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")
首先是@WebServlet
註解,表示這是一個名字叫SignIn
的servlet
,可用於實現servlet與url的映射,若是不在這裏添加這個註解,則須要在WEB-INF
目錄下的web.xml
添加一個<servlet-mapping>
,也就是叫servlet的映射.
接着設置響應類型與編碼:
httpServletResponse.setContentType("text/plain;charset=utf-8");//設置相應類型爲html,編碼爲utf-8
HttpServletRequest.getParameter(String name)
方法表示根據name獲取相應的參數:
String name = httpServletRequest.getParameter("name"); String password = httpServletRequest.getParameter("password");
下面是SignUp.java
:
@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>
是訪問的路徑.
最後添加一個叫Hello.html
的HTML測試文件.
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>Welcome</title> </head> <body> Hello web. </body> </html>
筆者用的IDEA,Eclipse的打包請看這裏.
修更名字,並建立WEB-INF
目錄與子目錄classes
:
選中classes
,添加Module Output
,選擇本身的web項目:
添加JAR包,選中lib
目錄後添加JAR包文件:
(lib
文件夾被擋住了不要在乎細節哈...)
接着添加Hello.html
與web.xml
,web.xml
須要在WEB-INF
目錄裏,Hello.html
在WEB-INF
外面:
Build->Build Artifacts
:
打包好的.war文件上傳到服務器的Tomcat的webapps
目錄下:
scp ***.war username@xxx.xxx.xxx.xxx:/usr/local/tomcat/webapps
注意改爲本身的webapps
目錄.
Tomcat啓動後,在瀏覽器輸入
服務器IP:端口/項目/Hello.html
筆者爲了方便就在本地測試了:
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提示信息.
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>
設置,與源碼中的@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>
隨便輸入用戶名與密碼
查看數據庫:
數據庫的用戶名和密碼必定要設置正確,要否則會像下圖同樣拋出異常:
在加載驅動錯誤時也可能會出現這個錯誤,所以要確保打成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" />
便可
另外一種辦法是在AndroidManifest.xml
直接加入一句
<application android:usesCleartextTraffic="true" />
從android4.0開始,聯網不能再主線程操做,萬一網絡很差就會卡死,因此有關聯網的操做都須要新開一個線程,不能在主線程操做.
這個bug筆者找了好久,HTTP鏈接沒問題,服務器沒問題,數據庫沒問題,前端代碼沒問題,而後去了StackOverflow,發現是AVD的問題....
簡單來講就是卸載了APP再重啓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
若是以爲文章好看,歡迎點贊。
同時歡迎關注微信公衆號:氷泠之路。