android连接MySQL数据库
首先先开启MySQL的远程访问权限,详情请见上一个的博客。
说一说我遇到的坑吧,就是网上找了好多代码尝试着数据库,最后才连接成功,以下是我连接成功的代码
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.example.mysqlconnectiontest; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView textView; private String a= "" ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.bt_send).setOnClickListener( this ); textView=findViewById(R.id.tv_response); } @Override public void onClick(View view) { // Android 4.0 之后不能在主线程中请求HTTP请求 new Thread( new Runnable(){ @Override public void run() { UserDao userDao = new UserDao(); try { userDao.select( "test_one" ); } catch (Exception e) { e.printStackTrace(); } } }).start(); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com/apk/res-auto" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= ".MainActivity" > <Button android:id= "@+id/bt_send" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:onClick= "onClick" android:text= "查询数据表" /> <TextView android:id= "@+id/tv_response" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textStyle= "bold" /> </LinearLayout> |
DBOpenHelper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package com.example.mysqlconnectiontest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DBOpenHelper { private final static String driver = "com.mysql.jdbc.Driver" ; private final static String url = "jdbc:mysql://172.22.64.1/test?useUnicode=true&characterEncoding=UTF-8" ; private final static String username = "root" ; private final static String password = "123456" ; Connection conn= null ; Statement st= null ; ResultSet rs= null ; static { try { Class.forName(driver); } catch (ClassNotFoundException e) { System.out.println( "加载驱动错误" ); } } //2. 获取连接 public static Connection getConnect() throws Exception { return DriverManager.getConnection(url, username, password); } //3. 释放连接资源 public static void release(Connection conn, Statement st, ResultSet rs) throws Exception { if (rs != null ) { rs.close(); } if (st != null ) { st.close(); } if (conn != null ) { conn.close(); } } /*private static String diver = "com.mysql.jdbc.Driver"; //加入utf-8是为了后面往表中输入中文,表中不会出现乱码的情况 private static String url = "jdbc:mysql://10.99.117.52/xinwe?characterEncoding=utf-8"; private static String user = "root";//用户名 private static String password = "hzl02096";//密码 */ /* * 连接数据库 * */ /* public static Connection getConn(){ Connection conn = null; try { Class.forName(diver); conn = (Connection) DriverManager.getConnection(url,user,password);//获取连接 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }*/ } |
UserDao.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.example.mysqlconnectiontest; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; public class UserDao { //查询用户的方法 public boolean select(String table) throws Exception { Connection conn = null ; Statement state = null ; ResultSet rs = null ; try { conn = DBOpenHelper.getConnect(); state = conn.createStatement(); String sql = "select * from " + table; rs = state.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString( 1 ) + " " + rs.getString( 2 )); //就是输出第一列和第二列的值 } } catch (Exception e) { e.printStackTrace(); } finally { DBOpenHelper.release(conn, state, rs); } return true ; } } |
记住下面两行代码是放在manifest之间的,可不能放在application之前,一定要避坑!!!
1 2 | <uses-permission android:name= "android.permission.INTERNET" /> <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" /> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!