1. 程式人生 > 程式設計 >Android連線MySQL資料庫並進行增刪改查操作示例講解

Android連線MySQL資料庫並進行增刪改查操作示例講解

1.Android 連線MySQL資料庫

public class DBOpenHelper {
 
  private static String driver = "com.mysql.jdbc.Driver";//MySQL 驅動
  private static String url = "jdbc:mysql://IP:3306/資料庫";//MYSQL資料庫連線Url
  private static String user = "root";//使用者名稱
  private static String password = "root";//密碼
 
  /**
   * 連線資料庫
   * */
 
  public static Connection getConn(){
    Connection conn = null;
    try {
      Class.forName(driver);//獲取MYSQL驅動
      conn = (Connection) DriverManager.getConnection(url,user,password);//獲取連線
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return conn;
  }
 
  /**
   * 關閉資料庫
   * */
 
  public static void closeAll(Connection conn,PreparedStatement ps){
    if (conn != null) {
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    if (ps != null) {
      try {
        ps.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
 
  }
 
  /**
   * 關閉資料庫
   * */
 
  public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){
    if (conn != null) {
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    if (ps != null) {
      try {
        ps.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    if (rs != null) {
      try {
        rs.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
 
}

2.增刪改查

public class DBService {
 
  private Connection conn=null; //開啟資料庫物件
  private PreparedStatement ps=null;//操作整合sql語句的物件
  private ResultSet rs=null;//查詢結果的集合
 
  //DBService 物件
  public static DBService dbService=null;
 
  /**
   * 構造方法 私有化
   * */
 
  private DBService(){
 
  }
 
  /**
   * 獲取MySQL資料庫單例類物件
   * */
 
  public static DBService getDbService(){
    if(dbService==null){
      dbService=new DBService();
    }
    return dbService;
  }
 
 
  /**
   * 獲取要傳送簡訊的患者資訊  查
   * */
 
  public List<User> getUserData(){
    //結果存放集合
    List<User> list=new ArrayList<User>();
    //MySQL 語句
    String sql="select * from user";
    //獲取連結資料庫物件
    conn= DBOpenHelper.getConn();
    try {
      if(conn!=null&&(!conn.isClosed())){
        ps= (PreparedStatement) conn.prepareStatement(sql);
        if(ps!=null){
          rs= ps.executeQuery();
          if(rs!=null){
            while(rs.next()){
              User u=new User();
              u.setId(rs.getString("id"));
              u.setName(rs.getString("name"));
              u.setPhone(rs.getString("phone"));
              u.setContent(rs.getString("content"));
              u.setState(rs.getString("state"));
              list.add(u);
            }
          }
        }
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    DBOpenHelper.closeAll(conn,ps,rs);//關閉相關操作
    return list;
  }
 
  /**
   * 修改資料庫中某個物件的狀態  改
   * */
 
  public int updateUserData(String phone){
    int result=-1;
    if(!StringUtils.isEmpty(phone)){
      //獲取連結資料庫物件
      conn= DBOpenHelper.getConn();
      //MySQL 語句
      String sql="update user set state=? where phone=?";
      try {
        boolean closed=conn.isClosed();
        if(conn!=null&&(!closed)){
          ps= (PreparedStatement) conn.prepareStatement(sql);
          ps.setString(1,"1");//第一個引數state 一定要和上面SQL語句欄位順序一致
          ps.setString(2,phone);//第二個引數 phone 一定要和上面SQL語句欄位順序一致
          result=ps.executeUpdate();//返回1 執行成功
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    DBOpenHelper.closeAll(conn,ps);//關閉相關操作
    return result;
  }
 
  /**
   * 批量向資料庫插入資料  增
   * */
 
  public int insertUserData(List<User> list){
    int result=-1;
    if((list!=null)&&(list.size()>0)){
      //獲取連結資料庫物件
      conn= DBOpenHelper.getConn();
      //MySQL 語句
      String sql="INSERT INTO user (name,phone,content,state) VALUES (?,?,?)";
      try {
        boolean closed=conn.isClosed();
        if((conn!=null)&&(!closed)){
          for(User user:list){
            ps= (PreparedStatement) conn.prepareStatement(sql);
            String name=user.getName();
            String phone=user.getPhone();
            String content=user.getContent();
            String state=user.getState();
            ps.setString(1,name);//第一個引數 name 規則同上
            ps.setString(2,phone);//第二個引數 phone 規則同上
            ps.setString(3,content);//第三個引數 content 規則同上
            ps.setString(4,state);//第四個引數 state 規則同上
            result=ps.executeUpdate();//返回1 執行成功
          }
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    DBOpenHelper.closeAll(conn,ps);//關閉相關操作
    return result;
  }
 
  
  /**
   * 刪除資料 刪
   * */
 
  public int delUserData(String phone){
    int result=-1;
    if((!StringUtils.isEmpty(phone))&&(PhoneNumberUtils.isMobileNumber(phone))){
      //獲取連結資料庫物件
      conn= DBOpenHelper.getConn();
      //MySQL 語句
      String sql="delete from user where phone=?";
      try {
        boolean closed=conn.isClosed();
        if((conn!=null)&&(!closed)){
          ps= (PreparedStatement) conn.prepareStatement(sql);
          ps.setString(1,phone);
          result=ps.executeUpdate();//返回1 執行成功
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    DBOpenHelper.closeAll(conn,ps);//關閉相關操作
    return result;
  }
 
}

到此這篇關於Android 連線MySQL資料庫並進行增刪改查操作示例講解的文章就介紹到這了,更多相關Android 連線MySQL資料庫並進行增刪改查操作內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!