Android連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解
更新時間:2020年08月04日 17:24:21 作者:jianning-wu
這篇文章主要介紹了Android 連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
1.Android 連接MySQL數(shù)據(jù)庫
public class DBOpenHelper { private static String driver = "com.mysql.jdbc.Driver";//MySQL 驅動 private static String url = "jdbc:mysql://IP:3306/數(shù)據(jù)庫";//MYSQL數(shù)據(jù)庫連接Url private static String user = "root";//用戶名 private static String password = "root";//密碼 /** * 連接數(shù)據(jù)庫 * */ 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; } /** * 關閉數(shù)據(jù)庫 * */ 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(); } } } /** * 關閉數(shù)據(jù)庫 * */ 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; //打開數(shù)據(jù)庫對象 private PreparedStatement ps=null;//操作整合sql語句的對象 private ResultSet rs=null;//查詢結果的集合 //DBService 對象 public static DBService dbService=null; /** * 構造方法 私有化 * */ private DBService(){ } /** * 獲取MySQL數(shù)據(jù)庫單例類對象 * */ public static DBService getDbService(){ if(dbService==null){ dbService=new DBService(); } return dbService; } /** * 獲取要發(fā)送短信的患者信息 查 * */ public List<User> getUserData(){ //結果存放集合 List<User> list=new ArrayList<User>(); //MySQL 語句 String sql="select * from user"; //獲取鏈接數(shù)據(jù)庫對象 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; } /** * 修改數(shù)據(jù)庫中某個對象的狀態(tài) 改 * */ public int updateUserData(String phone){ int result=-1; if(!StringUtils.isEmpty(phone)){ //獲取鏈接數(shù)據(jù)庫對象 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");//第一個參數(shù)state 一定要和上面SQL語句字段順序一致 ps.setString(2,phone);//第二個參數(shù) phone 一定要和上面SQL語句字段順序一致 result=ps.executeUpdate();//返回1 執(zhí)行成功 } } catch (SQLException e) { e.printStackTrace(); } } DBOpenHelper.closeAll(conn,ps);//關閉相關操作 return result; } /** * 批量向數(shù)據(jù)庫插入數(shù)據(jù) 增 * */ public int insertUserData(List<User> list){ int result=-1; if((list!=null)&&(list.size()>0)){ //獲取鏈接數(shù)據(jù)庫對象 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);//第一個參數(shù) name 規(guī)則同上 ps.setString(2,phone);//第二個參數(shù) phone 規(guī)則同上 ps.setString(3,content);//第三個參數(shù) content 規(guī)則同上 ps.setString(4,state);//第四個參數(shù) state 規(guī)則同上 result=ps.executeUpdate();//返回1 執(zhí)行成功 } } } catch (SQLException e) { e.printStackTrace(); } } DBOpenHelper.closeAll(conn,ps);//關閉相關操作 return result; } /** * 刪除數(shù)據(jù) 刪 * */ public int delUserData(String phone){ int result=-1; if((!StringUtils.isEmpty(phone))&&(PhoneNumberUtils.isMobileNumber(phone))){ //獲取鏈接數(shù)據(jù)庫對象 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 執(zhí)行成功 } } catch (SQLException e) { e.printStackTrace(); } } DBOpenHelper.closeAll(conn,ps);//關閉相關操作 return result; } }
到此這篇關于Android 連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解的文章就介紹到這了,更多相關Android 連接MySQL數(shù)據(jù)庫并進行增刪改查操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android實現(xiàn)EditText控件禁止輸入內(nèi)容的方法(附測試demo)
這篇文章主要介紹了Android實現(xiàn)EditText控件禁止輸入內(nèi)容的方法,涉及Android針對EditText控件屬性設置的相關技巧,需要的朋友可以參考下2015-12-12Android避免內(nèi)存溢出(Out of Memory)方法匯總
這篇文章主要為大家詳細介紹了Android避免內(nèi)存溢出Out of Memory方法匯總,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Android圖片添加水印圖片并把圖片保存到文件存儲的實現(xiàn)代碼
這篇文章主要介紹了Android圖片添加水印圖片并把圖片保存到文件存儲的實現(xiàn)代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06