使用com.mysql.jdbc.Driver的Andr​​oid + MySQL的 [英] Android + MySQL using com.mysql.jdbc.Driver

查看:114
本文介绍了使用com.mysql.jdbc.Driver的Andr​​oid + MySQL的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序,将连接到MySQL服务器。 //本地主机:3306 / 现在,我使用 HTTP通过XAMPP测试机上的MySQL服务器。在code严格的测试,当它作为一个Java应用以下的罚款。

I am writing an Android application that will connect to a MySQL server. For now, I am testing the MySQL server on my computer through XAMPP using http://localhost:3306/. The code below works fine when testing it strictly as a JAVA application.

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();
    }
  }
}

然而,当我将它添加到我的Andr​​oid应用程序我得到的例外五错误。

However when I add it to my Android application I get the Exception e error.

// 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();
      }


  }
 };

由于我编译我的Andr​​oid应用程序,我注意到在控制台下面的语句:

As I am compiling my Android application, I notice the following statement in the Console:

[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有可能导致我在正确的方向吗? 谢谢

I assume this statement has something to do with the Exception I am getting. Has anyone worked with MySQL and Android that could lead me in the right direction? Thanks

推荐答案

使用JDBC连接器使用mysql-connector-java的-3.0.17-GA-bin.jar的,现在是唯一可行的。

Use JDBC connector mysql-connector-java-3.0.17-ga-bin.jar, for now is the only one that works.

检查code也,你需要实现你的应用程序的每个活动连接池,只需添加一个私人的连接var和初始化,在你喜欢CON = DriveManager.getConnection onCreate方法... ......然后使用专用VAR每次需要访问MySQL的时间。

Check your code also, you need to implement a pool of connections for each activity of your application, just add a private "Connection" var and initialize that in your OnCreate method like con = DriveManager.getConnection...... then use that private var every time you need to access MYSQL.

这篇关于使用com.mysql.jdbc.Driver的Andr​​oid + MySQL的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆