Raddfood
Raddfood

Reputation: 169

Android + MySQL using com.mysql.jdbc.Driver

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

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


  }
 };

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.

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

Upvotes: 3

Views: 29303

Answers (4)

kaw
kaw

Reputation: 75

An other approach is to use a Virtual JDBC Driver that uses a three-tier architecture: your JDBC code is sent through HTTP to a remote Servlet that filters the JDBC code (configuration & security) before passing it to the MySql JDBC Driver. The result is sent you back through HTTP. There are some free software that use this technique. Just Google "Android JDBC Driver over HTTP".

Upvotes: 2

ruhalde
ruhalde

Reputation: 3551

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

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.

Upvotes: 6

EuEu
EuEu

Reputation: 11

To provide a connection to a remote database in android, will be necessary to use WebService because the android does not have an API to connect to remote database.

Upvotes: -2

djg
djg

Reputation: 1283

Have you tried looking at ContentProviders? http://developer.android.com/guide/topics/providers/content-providers.html

You will probably have more luck accessing data through an implementation of that (SQLite database for example) than accessing MySQL directly.

Upvotes: 0

Related Questions