How to make MySQL Connector/J working on android?

前端 未结 3 1696
心在旅途
心在旅途 2021-01-03 10:54

I\'ve got a MySQL server running on a local Linux machine. I haven\'t been faced to any problems communicating with it and executing SQL statements through a simple Java pro

相关标签:
3条回答
  • 2021-01-03 11:34

    The general misconception among Java developers who is moving to Android world is that everything that has been created for old good Java can be used on Android as well. Since Android usage of Java has violated the major Java principle, "write once, run everywhere", it's simply not true. I've seen people trying to use common Apache jars with Android, which resulted to weird errors (see Can't import Apache HTTP in Eclipse).

    The same problems seem to be true for Java JDBC drivers and API's, e.g. see answers here: Connecting to MySQL from Android with JDBC

    The common advice - each time when you try to use a 3rd party JAR with Android, check if it's compatible with the latter or if there is a port, which is specific for Android.

    0 讨论(0)
  • 2021-01-03 11:47

    It is possible, if you use the correct connector. I use the connector 3.0.17, in some others examples the connector is mysql-connector-java-5.1.24-bin.jar

    1. Just download the right connector.
    2. Past the connector-java.x.x.x-bin.jar on libs
    3. Import the java.sql

      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
      

      Use the next method to connect

      public void conectarBDMySQL (String user, String password, 
                  String ip, String port, String cat)
          {
              if (conexionMySQL == null)      
              {
                  String urlConexionMySQL = "";
                  if (cat!= "")
                      urlConexionMySQL = "jdbc:mysql://" + ip + ":" + port+ "/" + cat;
                  else
                      urlConexionMySQL = "jdbc:mysql://" + ip + ":" + port;
                  if (user!= "" & password!= "" & ip != "" & port!= "")
                  {
                      try 
                      {
                          Class.forName("com.mysql.jdbc.Driver");
                          conexionMySQL = DriverManager.getConnection(urlConexionMySQL, 
                                  user, password);                    
                      } 
                      catch (ClassNotFoundException e) 
                      {
                            Toast.makeText(getApplicationContext(),
                                      "Error: " + e.getMessage(),
                                      Toast.LENGTH_SHORT).show();
                      } 
                      catch (SQLException e) 
                      {
                            Toast.makeText(getApplicationContext(),
                                      "Error: " + e.getMessage(),
                                      Toast.LENGTH_SHORT).show();
                      }
                  }
              }
          }
      

    It works perfect for me, and i have had no problems!!!

    0 讨论(0)
  • 2021-01-03 11:59

    Generally, the recommended approach is to post to a web service, and allow the web service to perform the required actions on the database. However, there is an open source library on GitHub which allows you to connect directly to the database server without the need of a web service.

    The library is fairly new and has been tested against MySQL 5.1 -> 5.7 which should also be compatible with the equivalent MySQL version to MariaDB.

    Disclaimer: I wrote it.

    0 讨论(0)
提交回复
热议问题