要实现这套框架需要一个web容器,数据库和客户端
服务器建议选用linux系统,小型项目的话,安装tomcat和mysql,可以先使用tomcat作为web容器。
需要编写服务器端代码,如servlet,因为android客户端要和服务器进行通信,所以servlet要给客户端提供接口。android调用服务器接口后,服务器再请求数据库,就可以把记录保存到数据库了
public class DB {public static Connection getConnection() {String driver = "com.mysql.jdbc.Driver"String url = "jdbc:mysql://localhost:3306/db?user=root&password=root"Connection conn = nulltry {Class.forName(driver)conn = DriverManager.getConnection(url)} catch (ClassNotFoundException e) {e.printStackTrace()} catch (SQLException e) {e.printStackTrace()}return conn}public static Statement createStatement(Connection conn) {Statement stmt = nulltry {stmt = conn.createStatement()} catch (SQLException e) {e.printStackTrace()}return stmt}public static ResultSet executeQuery(Statement stmt, String sql) {ResultSet rs = nulltry {rs = stmt.executeQuery(sql)} catch (SQLException e) {e.printStackTrace()}return rs}public static int executeUpdate(Connection conn, String sql) {int row = 0Statement stmt = nulltry {stmt = conn.createStatement()row = stmt.executeUpdate(sql)} catch (SQLException e) {e.printStackTrace()} finally {// close()close(stmt)}return row}public static PreparedStatement prepareStatement(Connection conn, String sql) {PreparedStatement pstmt = nulltry {pstmt = conn.prepareStatement(sql)} catch (SQLException e) {e.printStackTrace()}return pstmt}public static PreparedStatement prepareStatement(Connection conn,String sql, int autoGeneratedKeys) {PreparedStatement pstmt = nulltry {pstmt = conn.prepareStatement(sql, autoGeneratedKeys)} catch (SQLException e) {e.printStackTrace()}return pstmt}public static void close(PreparedStatement pstmt) {//可以使用close(Statement stmt)方法,父类引用指向子类对象if (null != pstmt) {try {pstmt.close()} catch (SQLException e) {e.printStackTrace()}}pstmt = null}public static void close(Statement stmt) {if (null != stmt) {try {stmt.close()} catch (SQLException e) {e.printStackTrace()}}stmt = null}public static void close(ResultSet rs) {if (null != rs) {try {rs.close()} catch (SQLException e) {e.printStackTrace()}}rs = null}public static void close(Connection conn) {if (null != conn) {try {conn.close()} catch (SQLException e) {e.printStackTrace()}}conn = null}}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)