Access MYSQL through a WEB server and synchronize data to the androidSQLite Database

Source: Internet
Author: User
Blog. csdn. netmary8812.pdf create a MYSQL database and create a table (such as the menu table ). 2. Connect to the database. 3, access the database process 2, 3 specific steps: 1, in Myeclipse to create a web project, in order to better unified management in the WEB-INF to create a web. xml is used to load the configuration information when the server is started. This file is

Http://blog.csdn.net/Mary881225 1. Create a MYSQL database and create a table, such as the menu table ). 2. Connect to the database. 3, access the database process 2, 3 specific steps: 1, in Myeclipse to create a web project, in order to better unified management in the WEB-INF to create a web. xml is used to load the configuration information when the server is started. This file is

Http://blog.csdn.net/Mary881225

1. Create a MYSQL database and a table (such as the menu table ).

2. Connect to the database.

3. Access the database

Steps 2 and 3:

1. Create a web project under Myeclipse, and create a web. xml under the WEB-INF to load the configuration information when the server is started for unified management. This file is composed of a large number And Composition, which is only explained here,

The web. xml instance is as follows:

 
 
  
   
    LoginServlet
   
   
    com.amaker.servlet.LoginServlet
   
  
  
   
    UpdateMenuServlet
   
   
    /servlet/UpdateMenuServlet
   
  
 

2. To facilitate unified management of database verification information, create a DBConfig. properties file under the src directory. Including the driver, url, user name, and password used to connect to the mysql database.

Driver = c

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/wirelessorder_db?useUnicode=true&characterEncoding=utf-8username=rootpassword=123

3. Connect to the database:

A. You can first create a tool class DBUtil. java in the src directory to connect to and close the database. The Code is as follows.

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 {/** close database Connection */public void closeConn (Connection conn) {try {conn. close ();} catch (SQLException e) {e. printStackTrace () ;}}/** Open Database Connection */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. Create a Menu. java in the src directory to set and obtain its attributes.

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. Create an update. java class under src for synchronization, that is, update the data in the android SQLites database (provided that the SQLite database contains the corresponding Menu table ).

/*** @ Author BlackhorseMary * to Update SQLite data */public class Update {// obtain the menu List public List
 
  
GetMenuList () {// query the SQL statement String SQL = "select id, typeId, price, name, pic, remark from MenuTbl "; // Database Connection Tool class DBUtil util = new DBUtil (); // obtain Connection conn = util. openConnection (); try {// obtain the predefined Statement pstmt = conn. createStatement (); // execute the query ResultSet rs = pstmt.exe cuteQuery (SQL); // judge the detailed List of orders
  
   
List = new ArrayList
   
    
(); While (rs. next () {// obtain the menu information 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. Start the server and enter the corresponding URL in the browser. You can export the SQLITE database and use the SQLITE database tool to view the results of the Menu table in the database.

So far, the success is achieved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.