Android + MySQL using com.mysql.jdbc.Driver

I am writing an Android application that will connect to a MySQL server. So far I am testing a MySQL server on my computer using XAMPP using http: // localhost: 3306 / . The code below works fine when checking 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 app, I get an 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();
      }


  }
 };

When I compile my Android application, I see the following instruction 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 that this statement has something to do with the Exception I receive. Has anyone worked with MySQL and Android that could lead me in the right direction? Thanks

+3
source share
4

JDBC- mysql-connector-java-3.0.17-ga-bin.jar, .

, , "" var OnCreate, con = DriveManager.getConnection...... var , MYSQL.

+6

JDBC, : JDBC- HTTP , JDBC ( ), JDBC MySql, HTTP. , . Google "Android JDBC HTTP".

+2

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

Most likely, you will be able to access data using an implementation (for example, a SQLite database) than directly access MySQL.

0
source

In order to provide a connection to a remote database in android, you need to use WebService, because android does not have an API to connect to a remote database.

-3
source

Source: https://habr.com/ru/post/1788037/


All Articles