ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Java-使用com.mysql.jdbc.Driver的Android MySQL

2019-10-23 16:17:26  阅读:275  来源: 互联网

标签:exception inner-classes java mysql android


我正在编写一个将连接到MySQL服务器的Android应用程序.现在,我正在使用http://localhost:3306/通过XAMPP在我的计算机上测试MySQL服务器.下面的代码在严格作为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.

标签:exception,inner-classes,java,mysql,android
来源: https://codeday.me/bug/20191023/1913912.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有