Android連接MySQL數(shù)據(jù)庫并進(jìn)行增刪改查操作示例講解
1.Android 連接MySQL數(shù)據(jù)庫
public class DBOpenHelper { private static String driver = "com.mysql.jdbc.Driver";//MySQL 驅(qū)動(dòng) 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驅(qū)動(dòng) conn = (Connection) DriverManager.getConnection(url, user, password);//獲取連接 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * 關(guān)閉數(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(); } } } /** * 關(guān)閉數(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ù)庫對(duì)象 private PreparedStatement ps=null;//操作整合sql語句的對(duì)象 private ResultSet rs=null;//查詢結(jié)果的集合 //DBService 對(duì)象 public static DBService dbService=null; /** * 構(gòu)造方法 私有化 * */ private DBService(){ } /** * 獲取MySQL數(shù)據(jù)庫單例類對(duì)象 * */ public static DBService getDbService(){ if(dbService==null){ dbService=new DBService(); } return dbService; } /** * 獲取要發(fā)送短信的患者信息 查 * */ public List<User> getUserData(){ //結(jié)果存放集合 List<User> list=new ArrayList<User>(); //MySQL 語句 String sql="select * from user"; //獲取鏈接數(shù)據(jù)庫對(duì)象 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);//關(guān)閉相關(guān)操作 return list; } /** * 修改數(shù)據(jù)庫中某個(gè)對(duì)象的狀態(tài) 改 * */ public int updateUserData(String phone){ int result=-1; if(!StringUtils.isEmpty(phone)){ //獲取鏈接數(shù)據(jù)庫對(duì)象 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");//第一個(gè)參數(shù)state 一定要和上面SQL語句字段順序一致 ps.setString(2,phone);//第二個(gè)參數(shù) phone 一定要和上面SQL語句字段順序一致 result=ps.executeUpdate();//返回1 執(zhí)行成功 } } catch (SQLException e) { e.printStackTrace(); } } DBOpenHelper.closeAll(conn,ps);//關(guān)閉相關(guān)操作 return result; } /** * 批量向數(shù)據(jù)庫插入數(shù)據(jù) 增 * */ public int insertUserData(List<User> list){ int result=-1; if((list!=null)&&(list.size()>0)){ //獲取鏈接數(shù)據(jù)庫對(duì)象 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);//第一個(gè)參數(shù) name 規(guī)則同上 ps.setString(2,phone);//第二個(gè)參數(shù) phone 規(guī)則同上 ps.setString(3,content);//第三個(gè)參數(shù) content 規(guī)則同上 ps.setString(4,state);//第四個(gè)參數(shù) state 規(guī)則同上 result=ps.executeUpdate();//返回1 執(zhí)行成功 } } } catch (SQLException e) { e.printStackTrace(); } } DBOpenHelper.closeAll(conn,ps);//關(guān)閉相關(guān)操作 return result; } /** * 刪除數(shù)據(jù) 刪 * */ public int delUserData(String phone){ int result=-1; if((!StringUtils.isEmpty(phone))&&(PhoneNumberUtils.isMobileNumber(phone))){ //獲取鏈接數(shù)據(jù)庫對(duì)象 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);//關(guān)閉相關(guān)操作 return result; } }
到此這篇關(guān)于Android 連接MySQL數(shù)據(jù)庫并進(jìn)行增刪改查操作示例講解的文章就介紹到這了,更多相關(guān)Android 連接MySQL數(shù)據(jù)庫并進(jìn)行增刪改查操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)EditText控件禁止輸入內(nèi)容的方法(附測試demo)
這篇文章主要介紹了Android實(shí)現(xiàn)EditText控件禁止輸入內(nèi)容的方法,涉及Android針對(duì)EditText控件屬性設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-12-12Android 中ContentProvider的實(shí)例詳解
這篇文章主要介紹了Android 中ContentProvider的實(shí)例詳解的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09Android避免內(nèi)存溢出(Out of Memory)方法匯總
這篇文章主要為大家詳細(xì)介紹了Android避免內(nèi)存溢出Out of Memory方法匯總,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01android特賣列表倒計(jì)時(shí)卡頓問題的解決方法
這篇文章主要為大家詳細(xì)介紹了android特賣列表倒計(jì)時(shí)卡頓問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android獲取手機(jī)的版本號(hào)等信息的代碼
這篇文章主要為大家詳細(xì)介紹了Android獲取手機(jī)的版本號(hào)等信息的代碼,代碼很精彩,感興趣的小伙伴們可以參考一下2016-07-07Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06