通過WEB伺服器訪問MYSQL,並且資料同步到android SQLite資料庫
阿新 • • 發佈:2019-01-26
2、連線資料庫。
3、訪問資料庫
過程2、3 具體步驟:
1、在Myeclipse下新建一個web專案,為了好統一管理在WEB-INF下建一個web.xml用來載入伺服器啟動時的配置資訊。這個檔案是由大量的<servlet></servlet>與<servlet-mapping></servlet-mapping>組成,這裡只做講解,
web.xml例項如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app 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"> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.amaker.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UpdateMenuServlet</servlet-name> <url-pattern>/servlet/UpdateMenuServlet</url-pattern> </servlet-mapping>
2、為了方便對資料庫驗證資訊的統一管理,在src目錄下建一個DBConfig.properties檔案。包含內容有:連線mysql資料庫的驅動、url、使用者名稱和密碼。
<SPAN style="FONT-SIZE: 18px">driver=c
<SPAN style="FONT-SIZE: 18px">driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/wirelessorder_db?useUnicode=true&characterEncoding=utf-8 username=root password=123</SPAN>
3、連線資料庫:
a、可以先在src目錄下建一個工具類DBUtil.java用於連線和關閉資料庫。具體程式碼如下。
package com.amaker.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
*
* @author BlackhorseMary
*/ public class DBUtil { /* * 關閉資料庫連線 */ public void closeConn(Connection conn){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } /* * 開啟資料庫連線 */ public Connection openConnection() { Properties prop = new Properties(); String driver = null; String url = null; String username = null; String password = null; try { prop.load(this.getClass().getClassLoader().getResourceAsStream( "DBConfig.properties")); driver = prop.getProperty("driver"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); Class.forName(driver); return DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return null; } }
b、再在src目錄下新建一個Menu.java,用於設定獲取其屬性。
package com.amaker.entity;
public class Menu {
private int id;
private int price;
private int typeId;
private String name;
private String pic;
private String remark;
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 getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
}
4、在src下建一個update.java類用於執行同步,即更新android SQLites資料庫中的資料(前提是SQLite資料庫裡面有對應的Menu表)。
/**
* @author BlackhorseMary
* 完成更新SQLite資料功能
*/
public class Update {
// 獲得選單列表
public List<Menu> getMenuList() {
// 查詢SQL語句
String sql =" select id,typeId,price,name,pic,remark from MenuTbl ";
// 資料庫連線工具類
DBUtil util = new DBUtil();
// 獲得連線
Connection conn = util.openConnection();
try {
// 獲得預定義語句
Statement pstmt = conn.createStatement();
// 執行查詢
ResultSet rs = pstmt.executeQuery(sql);
// 判斷訂單詳細
List<Menu> list = new ArrayList<Menu>();
while (rs.next()) {
// 獲得選單資訊
int id = rs.getInt(1);
int typeId = rs.getInt(2);
int price = rs.getInt(3);
String name = rs.getString(4);
String pic = rs.getString(5);
String remark = rs.getString(6);
Menu m = new Menu();
m.setId(id);
m.setName(name);
m.setPic(pic);
m.setPrice(price);
m.setRemark(remark);
m.setTypeId(typeId);
list.add(m);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
} finally {
util.closeConn(conn);
}
return null;
}
}
5、啟動伺服器,在瀏覽器中輸入相應URL地址,可以把相應的SQLITE資料庫匯出來,用SQLITE資料庫工具,檢視資料庫中Menu表的結果。
至此,大功告成。