Android + MySQL 使用 com.mysql.jdbc.Driver

2024-01-06

我正在编写一个连接到 MySQL 服务器的 Android 应用程序。目前,我正在通过 XAMPP 测试我的计算机上的 MySQL 服务器,使用http://本地主机:3306/ http://localhost:3306/。下面的代码在严格作为 JAVA 应用程序进行测试时工作正常。

import java.sql.*;

public class MySQL{
  public static void main(String[] args) {
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "database";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "";
    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      conn.close();
      System.out.println("Disconnected from database");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

但是,当我将其添加到 Android 应用程序时,我收到 Exception e 错误。

// interact with MySQL!!!
 private View.OnClickListener onSendRequest = new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   EditText username = (EditText) findViewById(R.id.Huusername);
      //    String un = " " + username.getText().toString() + " ";

      System.out.println("MySQL Connect Example.");
      Connection conn = null;
      String url = "jdbc:mysql://localhost:3306/";
      String dbName = "database";
      String driver = "com.mysql.jdbc.Driver";
      String userName = "root"; 
      String password = "";
      try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url+dbName,userName,password);
        Toast.makeText(getBaseContext(),
      "Connected to the database.", Toast.LENGTH_LONG)
      .show();
        conn.close();
        Toast.makeText(getBaseContext(),
      "Disconnected form the database.", Toast.LENGTH_LONG)
      .show();
      } catch (Exception e) {
       Toast.makeText(getBaseContext(),
      "Exception e.", Toast.LENGTH_LONG)
      .show();
        e.printStackTrace();
      }


  }
 };

当我编译 Android 应用程序时,我注意到控制台中出现以下语句:

[2011-01-26 16:05:54 - hookup]: Dxwarning: Ignoring InnerClasses attribute for an anonymous inner class
(com.mysql.jdbc.interceptors.ResultSetScannerInterceptor$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.

我认为这个声明与我收到的异常有关。有没有人使用 MySQL 和 Android 可以引导我走向正确的方向? 谢谢


使用 JDBC 连接器 mysql-connector-java-3.0.17-ga-bin.jar,目前这是唯一有效的。

还要检查您的代码,您需要为应用程序的每个活动实现一个连接池,只需添加一个私有“Connection”变量并在 OnCreate 方法中初始化它,例如 con = DriveManager.getConnection...... 然后使用它每次需要访问 MYSQL 时都使用 private var。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android + MySQL 使用 com.mysql.jdbc.Driver 的相关文章

随机推荐