当前位置:   article > 正文

android连接mysql数据库_安卓联接数据库

安卓联接数据库

android中我们是可以连接mysql数据库的。连接方式如下:

1.首先我们需要导入mysql驱动jar包下载地址:

链接:https://pan.baidu.com/s/1PV9jV9m3LLjXeLfSE5ChOg 
提取码:3v2q

 2.连接数据库的代码:

  1. package com.demo.take.dao;
  2. import android.util.Log;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. public class DBUtil {
  9. private static String diver = "com.mysql.jdbc.Driver";
  10. private static String url = "jdbc:mysql://192.168.0.199:3306/school_take?characterEncoding=utf-8";
  11. private static String user = "root";//用户名
  12. private static String password = "root";//密码
  13. /*
  14. * 连接数据库
  15. * */
  16. public static Connection getConn() {
  17. Connection conn = null;
  18. try {
  19. Class.forName(diver);
  20. conn = (Connection) DriverManager.getConnection(url, user, password);//获取连接
  21. Log.e("getConn", "连接成功");
  22. } catch (ClassNotFoundException e) {
  23. Log.e("getConn", e.getMessage(), e);
  24. e.printStackTrace();
  25. } catch (SQLException e) {
  26. Log.e("getConn", e.getMessage(), e);
  27. e.printStackTrace();
  28. }
  29. return conn;
  30. }
  31. public static void close(Statement state, Connection conn) {
  32. if (state != null) {
  33. try {
  34. state.close();
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. if (conn != null) {
  40. try {
  41. conn.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. public static void close(ResultSet rs, Statement state, Connection conn) {
  48. if (rs != null) {
  49. try {
  50. rs.close();
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. if (state != null) {
  56. try {
  57. state.close();
  58. } catch (SQLException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. if (conn != null) {
  63. try {
  64. conn.close();
  65. } catch (SQLException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. }
  70. }

 3.下面给出一个增删改查的类

  1. //用户数据库连接类
  2. public class UserDao {
  3. //新增
  4. public static boolean add(UserBean bean) {
  5. String sql = "insert into user(user_name,phone,create_date,password)values('" + bean.getUser_name() + "','" + bean.getPhone() + "','" + bean.getCreate_date() + "','" + bean.getPassword() + "')";
  6. Connection conn = DBUtil.getConn();
  7. Statement state = null;
  8. boolean f = false;
  9. int a = 0;
  10. try {
  11. state = conn.createStatement();
  12. a = state.executeUpdate(sql);
  13. } catch (Exception e) {
  14. Log.e("add->", e.getMessage(), e);
  15. e.printStackTrace();
  16. } finally {
  17. DBUtil.close(state, conn);
  18. }
  19. if (a > 0) {
  20. f = true;
  21. }
  22. return f;
  23. }
  24. //删除
  25. public static boolean delete(UserBean bean) {
  26. String sql = "delete from user where id=" + bean.getId();
  27. Connection conn = DBUtil.getConn();
  28. Statement state = null;
  29. boolean f = false;
  30. int a = 0;
  31. try {
  32. state = conn.createStatement();
  33. a = state.executeUpdate(sql);
  34. } catch (Exception e) {
  35. Log.e("delete->", e.getMessage(), e);
  36. e.printStackTrace();
  37. } finally {
  38. DBUtil.close(state, conn);
  39. }
  40. if (a > 0) {
  41. f = true;
  42. }
  43. return f;
  44. }
  45. //修改
  46. public static boolean update(UserBean bean) {
  47. String sql = "update user set " + "user_name='" + bean.getUser_name() + "', phone='" + bean.getPhone() + "', create_date='" + bean.getCreate_date() + "', password='" + bean.getPassword() + "' where id='" + bean.getId() + "'";
  48. Connection conn = DBUtil.getConn();
  49. Statement state = null;
  50. boolean f = false;
  51. int a = 0;
  52. try {
  53. state = conn.createStatement();
  54. a = state.executeUpdate(sql);
  55. } catch (Exception e) {
  56. Log.e("update->", e.getMessage(), e);
  57. e.printStackTrace();
  58. } finally {
  59. DBUtil.close(state, conn);
  60. }
  61. if (a > 0) {
  62. f = true;
  63. }
  64. return f;
  65. }
  66. //获取列表
  67. public static List<UserBean> getListByPhone(String phone) {
  68. //结果存放集合
  69. List<UserBean> list = new ArrayList<>();
  70. //MySQL 语句
  71. String sql = "select * from user where phone=" + phone;
  72. Connection conn = DBUtil.getConn();
  73. Statement state = null;
  74. ResultSet rs = null;
  75. boolean f = false;
  76. int a = 0;
  77. try {
  78. state = conn.createStatement();
  79. rs = state.executeQuery(sql);
  80. Log.e("getListByPhone->","getListByPhone");
  81. while (rs.next()) {
  82. UserBean bean = new UserBean();
  83. bean.setId(rs.getInt("id"));
  84. bean.setUser_name(rs.getString("user_name"));
  85. bean.setPhone(rs.getString("phone"));
  86. bean.setPassword(rs.getString("password"));
  87. bean.setCreate_date(rs.getString("create_date"));
  88. list.add(bean);
  89. Log.e("getListByPhone->",bean.toString());
  90. }
  91. } catch (Exception e) {
  92. Log.e("getListByPhone->", e.getMessage(), e);
  93. e.printStackTrace();
  94. } finally {
  95. DBUtil.close(rs, state, conn);
  96. }
  97. if (a > 0) {
  98. f = true;
  99. }
  100. return list;
  101. }
  102. //获取列表
  103. public static List<UserBean> getList() {
  104. //结果存放集合
  105. List<UserBean> list = new ArrayList<>();
  106. //MySQL 语句
  107. String sql = "select * from user";
  108. Connection conn = DBUtil.getConn();
  109. Statement state = null;
  110. ResultSet rs = null;
  111. boolean f = false;
  112. int a = 0;
  113. try {
  114. state = conn.createStatement();
  115. rs = state.executeQuery(sql);
  116. while (rs.next()) {
  117. UserBean bean = new UserBean();
  118. bean.setId(rs.getInt("id"));
  119. bean.setUser_name(rs.getString("user_name"));
  120. bean.setPhone(rs.getString("phone"));
  121. bean.setPassword(rs.getString("password"));
  122. bean.setCreate_date(rs.getString("create_date"));
  123. list.add(bean);
  124. }
  125. } catch (Exception e) {
  126. Log.e("update->", e.getMessage(), e);
  127. e.printStackTrace();
  128. } finally {
  129. DBUtil.close(rs, state, conn);
  130. }
  131. if (a > 0) {
  132. f = true;
  133. }
  134. return list;
  135. }
  136. }

4.下面是对应的用户实体类

  1. package com.demo.take.bean;
  2. import java.io.Serializable;
  3. //用户实体类
  4. public class UserBean implements Serializable {
  5. //主键
  6. private int id;
  7. //用户姓名
  8. private String user_name;
  9. //手机号
  10. private String phone;
  11. //密码
  12. private String password;
  13. //创建时间
  14. private String create_date;
  15. public String getCreate_date() {
  16. return create_date;
  17. }
  18. public void setCreate_date(String create_date) {
  19. this.create_date = create_date;
  20. }
  21. public int getId() {
  22. return id;
  23. }
  24. public void setId(int id) {
  25. this.id = id;
  26. }
  27. public String getUser_name() {
  28. return user_name;
  29. }
  30. public void setUser_name(String user_name) {
  31. this.user_name = user_name;
  32. }
  33. public String getPhone() {
  34. return phone;
  35. }
  36. public void setPhone(String phone) {
  37. this.phone = phone;
  38. }
  39. public String getPassword() {
  40. return password;
  41. }
  42. public void setPassword(String password) {
  43. this.password = password;
  44. }
  45. @Override
  46. public String toString() {
  47. return "UserBean{" +
  48. "id=" + id +
  49. ", user_name='" + user_name + '\'' +
  50. ", phone='" + phone + '\'' +
  51. ", password='" + password + '\'' +
  52. '}';
  53. }
  54. }

5.然后呢mysql数据库是需要自己去安装的,安装完数据库根据实体类创建对应的表,连接上就可以调用了。如下:

注意 :1.mysql安装需要安装5版本的,高版本连不上。

            2.android连接mysql需要再子线程中去执行。

项目下载地址:android发布任务小项目-Android文档类资源-CSDN下载

如果有啥不明白可以加qq:332872622

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/480480?site
推荐阅读
相关标签
  

闽ICP备14008679号