当前位置:   article > 正文

Android Studio连接MYSQL数据库_mysql数据库使用php脚本语言,再连接android syudio

mysql数据库使用php脚本语言,再连接android syudio

首先导入mysql的jar包,这里连接的是8版本的。

这里之前到如果mysql的jar包了

首先跳到Project模式:

直接复制粘贴到这里:

这里之前到如果了。想删掉重新导入一次,但是报错,什么ioexception。这里将Project Structure中的Moudle中的app中的引入的该jar包的依赖删除掉,然后重启as,再删除就可以了。好吧,这也不是重点。

好,这里重新导入一边,复制jar包,粘贴到libs

点击ok后:

这里还没有引入依赖,接下来引入依赖。

有两种方法:

(1)右键jar包

点击这个。之后出现:

点击ok,就可以直接在app的build.gradle中创建dependency。

(2)这个比较麻烦

首先进入project structure

快捷键ctrl+alt+shift+s.进入这个界面:

依次点击:

点击第二个jar dependency

出现这个界面:

输入如下路径,这里是相对app文件夹的相对路径:

添加进来了:点击ok

前面 出现‘>' ,引入成功。

app的build.gradle中也会自动引入依赖。

到这里mysql的jar包就导入完成了。、

之后再程序中可以通过:

Class.forName("com.mysql.cj.jdbc.Driver");

来引入。

接下来就可以建立数据库连接了。

这里写了一个工具类,将连接mysql的过程封装起来,直接再main函数中调用即可。这里main函数好像可以不new对象就调用对象中的static方法。

用到的就这两个,其中connection是工具类,main调用工具类。

connection的代码如下:

  1. package com.example.myapplication;
  2. import android.util.Log;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. public class Connection {
  6. public static void mymysql(){
  7. final Thread thread =new Thread(new Runnable() {
  8. @Override
  9. public void run() {
  10. while (!Thread.interrupted()) {
  11. try {
  12. Thread.sleep(100); // 每隔0.1秒尝试连接
  13. } catch (InterruptedException e) {
  14. // Log.e(TAG, e.toString());
  15. System.out.println(e.toString());
  16. }
  17. // 1.加载JDBC驱动
  18. try {
  19. Class.forName("com.mysql.cj.jdbc.Driver");
  20. // Log.v(TAG, "加载JDBC驱动成功");
  21. System.out.println("加载JDBC驱动成功");
  22. } catch (ClassNotFoundException e) {
  23. // Log.e(TAG, "加载JDBC驱动失败");
  24. System.out.println("加载JDBC驱动失败");
  25. return;
  26. }
  27. // 2.设置好IP/端口/数据库名/用户名/密码等必要的连接信息
  28. String ip = "";
  29. int port = 3306;
  30. String dbName = "";
  31. String url = "jdbc:mysql://" + ip + ":" + port
  32. + "/" + dbName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";
  33. // useUnicode=true&characterEncodeing=UTF-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true"
  34. // 构建连接mysql的字符串
  35. String user = "root";
  36. String password = "WQT200126";
  37. // 3.连接JDBC
  38. try {
  39. System.out.println("111");
  40. java.sql.Connection conn = DriverManager.getConnection(url, user, password);
  41. // Log.d(TAG, "数据库连接成功");
  42. System.out.println("数据库连接成功");
  43. conn.close();
  44. return;
  45. }
  46. catch (SQLException e) {
  47. // Log.e(TAG, e.getMessage());
  48. System.out.println('H'+e.getMessage());
  49. }
  50. }
  51. }
  52. });
  53. thread.start();
  54. }
  55. }

其中数据库配置的代码是这些:

  1. String ip = "";
  2. int port = 3306;
  3. String dbName = "";
  4. String url = "jdbc:mysql://" + ip + ":" + port
  5. + "/" + dbName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";
  6. // useUnicode=true&characterEncodeing=UTF-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true"

其中,dbname就是连接的数据库名称,如果是连接本机,ip地址就是127.0.0.1。

还需要加上用户名和密码:

  1. String user = "root";
  2. String password = "";

设置好后就通过:

java.sql.Connection conn = DriverManager.getConnection(url, user, password);

来连接。

这里as中好像不允许耗时程序再主线程中,意思是连接MYSQL是个耗时程序,所以需要另外开辟一个线程来连接MYSQL。所以这里new了一个Thread。

然后工具类写好了,接下来再main函数中引入。

main函数代码如下:

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. //import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import com.example.myapplication.Connection;
  9. public class Main {
  10. public static void main(String[] args) {
  11. System.out.println("eh");
  12. Connection.mysql1();
  13. }
  14. }

接下来运行main函数。再运行前,要调整这里:

然后运行:

运行结果:

  1. HThe server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
  2. 加载JDBC驱动成功
  3. 111
  4. HThe server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
  5. 加载JDBC驱动成功
  6. 111
  7. HThe server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
  8. 加载JDBC驱动成功
  9. 111

就一直打印这几句话。

这里的“数据库连接成功"没有打印。

仔细看有这句话:

HThe server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

好像要加什么时间域。

改了url

原来的url是:

  1. String url = "jdbc:mysql://" + ip + ":" + port
  2. + "/" + dbName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";

改为:

  1. String url = "jdbc:mysql://" + ip + ":" + port
  2. + "/" + dbName+"? useUnicode=true&characterEncodeing=UTF-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true";

再连接:

成功了。

这里尝试一下不用开辟线程的方法,因为这里没有在app上运行:

代码:

工具类中加入:

  1. public static void mysql1(){
  2. String success = "111";
  3. System.out.println(success);
  4. // while (this.success_con!="111") {
  5. while (success=="111"){
  6. // 1.加载JDBC驱动
  7. try {
  8. Class.forName("com.mysql.cj.jdbc.Driver");
  9. // Log.v(TAG, "加载JDBC驱动成功");
  10. System.out.println("加载JDBC驱动成功");
  11. } catch (ClassNotFoundException e) {
  12. // Log.e(TAG, "加载JDBC驱动失败");
  13. System.out.println("加载JDBC驱动失败");
  14. return;
  15. }
  16. // 2.设置好IP/端口/数据库名/用户名/密码等必要的连接信息
  17. String ip = "127.0.0.1";
  18. int port = 3306;
  19. String dbName = "mm";
  20. String url = "jdbc:mysql://" + ip + ":" + port
  21. + "/" + dbName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";
  22. // useUnicode=true&characterEncodeing=UTF-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true"
  23. // 构建连接mysql的字符串
  24. String user = "root";
  25. String password = "WQT200126";
  26. // 3.连接JDBC
  27. try {
  28. System.out.println("111");
  29. java.sql.Connection conn = DriverManager.getConnection(url, user, password);
  30. // Log.d(TAG, "数据库连接成功");
  31. System.out.println("数据库连接成功");
  32. success = "sss";
  33. conn.close();
  34. return;
  35. }
  36. catch (SQLException e) {
  37. // Log.e(TAG, e.getMessage());
  38. System.out.println('H'+e.getMessage());
  39. }
  40. }
  41. }

main函数中:

  1. public class Main {
  2. public static void main(String[] args) {
  3. System.out.println("eh");
  4. Connection.mysql1();
  5. // this.test();
  6. // Main main = new Main();
  7. // main.test();
  8. }
  9. }

结果:

也可以连接。

接下来试一下在app中运行。

这里需要注意的是,在app中运行要开辟线程。不在app中运行,也就是不在oncreate,onclick中打印信息,不能用Log()。

之前的代码中,我都是将log注释掉了,用的sout。

首先在main函数中添加oncreate方法。

  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.activity_main);
  4. Connection.mymysql1();
  5. }

需要注意的是:

运行前要sync:同步一下。

同步完成:

点击这个运行:

运行会报错:

Cannot fit requested classes in a single dex file (# methods: 81010 > 65536)

好像是容量超过限制。

参考这篇:解决“Cannot fit requested classes in a single dex file”的问题-CSDN博客

然后SYNC,BUILD,RUN

成功。

然后尝试在手机上运行。

在main中加入代码:

  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.activity_main);
  4. Connection.mymysql1();
  5. }

运行成功。

需要注意,这里mysql的jar包要换位5.*,不然在手机上运行会报错。

参考这篇:安卓连接云mysql时报错:java.lang.NoClassDefFoundError: Failed resolution of: Ljava/sql/SQLType-CSDN博客

同时更改

Class.forName("com.mysql.jdbc.Driver");

libs中之前的8.*的jar包要删掉,不然会报错。

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

闽ICP备14008679号