在andriod studio中,使用mysql时有点玩不转,包括依赖啊版本配置啊等等
2条回答 默认 最新
关注
在android中我们是可以连接mysql数据库的。连接方式如下:
1.首先我们需要导入mysql驱动jar包下载地址:
链接:https://pan.baidu.com/s/1PV9jV9m3LLjXeLfSE5ChOg
提取码:3v2q2.连接数据库的代码:
package com.demo.take.dao; import android.util.Log; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { private static String diver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://192.168.0.199:3306/school_take?characterEncoding=utf-8"; private static String user = "root";//用户名 private static String password = "root";//密码 /* * 连接数据库 * */ public static Connection getConn() { Connection conn = null; try { Class.forName(diver); conn = (Connection) DriverManager.getConnection(url, user, password);//获取连接 Log.e("getConn", "连接成功"); } catch (ClassNotFoundException e) { Log.e("getConn", e.getMessage(), e); e.printStackTrace(); } catch (SQLException e) { Log.e("getConn", e.getMessage(), e); e.printStackTrace(); } return conn; } public static void close(Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
3.下面给出一个增删改查的类
//用户数据库连接类 public class UserDao { //新增 public static boolean add(UserBean bean) { String sql = "insert into user(user_name,phone,create_date,password)values('" + bean.getUser_name() + "','" + bean.getPhone() + "','" + bean.getCreate_date() + "','" + bean.getPassword() + "')"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { Log.e("add->", e.getMessage(), e); e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } //删除 public static boolean delete(UserBean bean) { String sql = "delete from user where id=" + bean.getId(); Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { Log.e("delete->", e.getMessage(), e); e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } //修改 public static boolean update(UserBean bean) { 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() + "'"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { Log.e("update->", e.getMessage(), e); e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } //获取列表 public static List<UserBean> getListByPhone(String phone) { //结果存放集合 List<UserBean> list = new ArrayList<>(); //MySQL 语句 String sql = "select * from user where phone=" + phone; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; boolean f = false; int a = 0; try { state = conn.createStatement(); rs = state.executeQuery(sql); Log.e("getListByPhone->","getListByPhone"); while (rs.next()) { UserBean bean = new UserBean(); bean.setId(rs.getInt("id")); bean.setUser_name(rs.getString("user_name")); bean.setPhone(rs.getString("phone")); bean.setPassword(rs.getString("password")); bean.setCreate_date(rs.getString("create_date")); list.add(bean); Log.e("getListByPhone->",bean.toString()); } } catch (Exception e) { Log.e("getListByPhone->", e.getMessage(), e); e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } if (a > 0) { f = true; } return list; } //获取列表 public static List<UserBean> getList() { //结果存放集合 List<UserBean> list = new ArrayList<>(); //MySQL 语句 String sql = "select * from user"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; boolean f = false; int a = 0; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { UserBean bean = new UserBean(); bean.setId(rs.getInt("id")); bean.setUser_name(rs.getString("user_name")); bean.setPhone(rs.getString("phone")); bean.setPassword(rs.getString("password")); bean.setCreate_date(rs.getString("create_date")); list.add(bean); } } catch (Exception e) { Log.e("update->", e.getMessage(), e); e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } if (a > 0) { f = true; } return list; } }
4.下面是对应的用户实体类
package com.demo.take.bean; import java.io.Serializable; //用户实体类 public class UserBean implements Serializable { //主键 private int id; //用户姓名 private String user_name; //手机号 private String phone; //密码 private String password; //创建时间 private String create_date; public String getCreate_date() { return create_date; } public void setCreate_date(String create_date) { this.create_date = create_date; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserBean{" + "id=" + id + ", user_name='" + user_name + '\'' + ", phone='" + phone + '\'' + ", password='" + password + '\'' + '}'; } }
5.然后呢mysql数据库是需要自己去安装的,安装完数据库根据实体类创建对应的表,连接上就可以调用了。如下:
注意 :1.mysql安装需要安装5版本的,高版本连不上。
2.android连接mysql需要再子线程中去执行。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用