android连接mysql数据库在android中我们是可以连接mysql数据库的。连接⽅式如下:
1.⾸先我们需要导⼊mysql驱动jar包下载地址:
2.连接数据库的代码:
1package com.demo.take.dao;
2
3import android.util.Log;
4
5
6import java.sql.Connection;
7import java.sql.DriverManager;
8import java.sql.ResultSet;
9import java.sql.SQLException;
10import java.sql.Statement;
11
12
13public class DBUtil {
14
15 private static String diver = "sql.jdbc.Driver";
16 private static String url = "jdbc:mysql://192.168.0.199:3306/school_take?characterEncoding=utf-8";
17 private static String user = "root";//⽤户名
18 private static String password = "root";//密码
19
20 /*
21 * 连接数据库
22 * */
23 public static Connection getConn() {
24 Connection conn = null;
25 try {
26 Class.forName(diver);
27 conn = (Connection) Connection(url, user, password);//获取连接
28 Log.e("getConn", "连接成功");
29 } catch (ClassNotFoundException e) {
30 Log.e("getConn", e.getMessage(), e);
31 e.printStackTrace();
32 } catch (SQLException e) {
33 Log.e("getConn", e.getMessage(), e);
34 e.printStackTrace();
35 }
36 return conn;
37 }
38
38
39 public static void close(Statement state, Connection conn) {
40 if (state != null) {
41 try {
42 state.close();
43 } catch (SQLException e) {
44 e.printStackTrace();
45 }
46 }
47
48 if (conn != null) {
49 try {
50 conn.close();
51 } catch (SQLException e) {
52 e.printStackTrace();
53 }
54 }
55 }
56
57 public static void close(ResultSet rs, Statement state, Connection conn) {
58 if (rs != null) {
59 try {
60 rs.close();
61 } catch (SQLException e) {
62 e.printStackTrace();
63 }
64 }
65
66 if (state != null) {
67 try {
68 state.close();
69 } catch (SQLException e) {
70 e.printStackTrace();
71 }
72 }
73
74 if (conn != null) {
75 try {
76 conn.close();
77 } catch (SQLException e) {
78 e.printStackTrace();
79 }
80 }
81 }
82
83}
3.下⾯给出⼀个增删改查的类
1//⽤户数据库连接类
2public class UserDao {
3
4 //新增
5 public static boolean add(UserBean bean) {
6 String sql = "insert into user(user_name,phone,create_date,password)values('" + User_name() + "','" + Phone() + "','" + Crea
7 Connection conn = Conn();
8 Statement state = null;
9 boolean f = false;
10 int a = 0;
11 try {
12 state = ateStatement();
13 a = uteUpdate(sql);
14 } catch (Exception e) {
15 Log.e("add->", e.getMessage(), e);
16 e.printStackTrace();
16 e.printStackTrace();
17 } finally {
18 DBUtil.close(state, conn);
19 }
20 if (a > 0) {
21 f = true;
22 }
23 return f;
24 }
25
26 //删除
27 public static boolean delete(UserBean bean) {
28 String sql = "delete from user where id=" + Id();
29 Connection conn = Conn();
30 Statement state = null;
31 boolean f = false;
32 int a = 0;
33 try {
34 state = ateStatement();
35 a = uteUpdate(sql);
36 } catch (Exception e) {
37 Log.e("delete->", e.getMessage(), e);
38 e.printStackTrace();
39 } finally {
40 DBUtil.close(state, conn);
41 }
42 if (a > 0) {
43 f = true;
44 }
45 return f;
46 }
47
48
49 //修改
50 public static boolean update(UserBean bean) {
51
52 String sql = "update user set " + "user_name='" + User_name() + "', phone='" + Phone() + "', create_date='" + Create_date()
53 Connection conn = Conn();
54 Statement state = null;
55 boolean f = false;
56 int a = 0;
57 try {
58 state = ateStatement();
59 a = uteUpdate(sql);
60 } catch (Exception e) {
61 Log.e("update->", e.getMessage(), e);
62 e.printStackTrace();
63 } finally {
64 DBUtil.close(state, conn);
65 }
66 if (a > 0) {
67 f = true;
68 }
69 return f;
70 }
71
72 //获取列表
73 public static List<UserBean> getListByPhone(String phone) {
74
75 //结果存放集合
76 List<UserBean> list = new ArrayList<>();
77 //MySQL 语句
78 String sql = "select * from user where phone=" + phone;
79 Connection conn = Conn();
80 Statement state = null;
81 ResultSet rs = null;
81 ResultSet rs = null;
82 boolean f = false;
83 int a = 0;
84 try {
85 state = ateStatement();
86 rs = uteQuery(sql);
87 Log.e("getListByPhone->","getListByPhone");
88 while (rs.next()) {
89 UserBean bean = new UserBean();
90 bean.Int("id"));
91 bean.setUser_String("user_name"));
92 bean.String("phone"));
93 bean.String("password"));
94 bean.setCreate_String("create_date"));
95 list.add(bean);
96 Log.e("getListByPhone->",String());
97
98 }
99 } catch (Exception e) {
100 Log.e("getListByPhone->", e.getMessage(), e);
101 e.printStackTrace();
102 } finally {
103 DBUtil.close(rs, state, conn);
104 }
105 if (a > 0) {
106 f = true;
107 }
108 return list;
109 }
110
111 //获取列表
112 public static List<UserBean> getList() {
113
114 //结果存放集合
115 List<UserBean> list = new ArrayList<>();
116 //MySQL 语句
117 String sql = "select * from user";
118 Connection conn = Conn();
119 Statement state = null;
120 ResultSet rs = null;
121 boolean f = false;
122 int a = 0;
123 try {
124 state = ateStatement();
125 rs = uteQuery(sql);
126 while (rs.next()) {
127 UserBean bean = new UserBean();
128 bean.Int("id"));
129 bean.setUser_String("user_name")); 130 bean.String("phone"));
131 bean.String("password")); 132 bean.setCreate_String("create_date")); 133 list.add(bean);
134
135 }
136 } catch (Exception e) {
137 Log.e("update->", e.getMessage(), e);
138 e.printStackTrace();
139 } finally {
140 DBUtil.close(rs, state, conn);
141 }
142 if (a > 0) {
143 f = true;
144 }
145 return list;
146 }
146 }
147
148
149}
mysql下载jar包4.下⾯是对应的⽤户实体类
1package com.demo.take.bean;
2
3import java.io.Serializable;
4//⽤户实体类
5public class UserBean implements Serializable {
6 //主键
7 private int id;
8 //⽤户姓名
9 private String user_name;
10 //⼿机号
11 private String phone;
12 //密码
13 private String password;
14 //创建时间
15 private String create_date;
16
17 public String getCreate_date() {
18 return create_date;
19 }
20
21 public void setCreate_date(String create_date) {
22 ate_date = create_date;
23 }
24
25 public int getId() {
26 return id;
27 }
28
29 public void setId(int id) {
30 this.id = id;
31 }
32
33 public String getUser_name() {
34 return user_name;
35 }
36
37 public void setUser_name(String user_name) {
38 this.user_name = user_name;
39 }
40
41 public String getPhone() {
42 return phone;
43 }
44
45 public void setPhone(String phone) {
46 this.phone = phone;
47 }
48
49 public String getPassword() {
50 return password;
51 }
52
53 public void setPassword(String password) {
54 this.password = password;
55 }
56
57 @Override
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论