Problem in database connection class

2

I'm having problems with my MySQL database connection class in Android Studio.

Error:

    10-16 15:04:02.872      532-532/com.example.dev.mbstore W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
10-16 15:04:02.872      532-532/com.example.dev.mbstore I/dalvikvm﹕ Could not find method javax.naming.Reference.get, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom
10-16 15:04:02.872      532-532/com.example.dev.mbstore W/dalvikvm﹕ VFY: unable to resolve virtual method 8068: Ljavax/naming/Reference;.get (Ljava/lang/String;)Ljavax/naming/RefAddr;
10-16 15:04:02.872      532-532/com.example.dev.mbstore D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0004
10-16 15:04:02.882      532-532/com.example.dev.mbstore W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
10-16 15:04:02.882      532-532/com.example.dev.mbstore E/dalvikvm﹕ Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
10-16 15:04:02.882      532-532/com.example.dev.mbstore W/dalvikvm﹕ VFY: unable to resolve new-instance 518 (Ljavax/naming/StringRefAddr;) in Lcom/mysql/jdbc/ConnectionPropertiesImpl$ConnectionProperty;
10-16 15:04:02.882      532-532/com.example.dev.mbstore D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x0006
10-16 15:04:02.882      532-532/com.example.dev.mbstore D/dalvikvm﹕ DexOpt: unable to opt direct call 0x1f86 at 0x14 in Lcom/mysql/jdbc/ConnectionPropertiesImpl$ConnectionProperty;.storeTo
10-16 15:04:03.032      532-540/com.example.dev.mbstore I/dalvikvm﹕ Total arena pages for JIT: 11
10-16 15:04:03.232      532-535/com.example.dev.mbstore D/dalvikvm﹕ GC_CONCURRENT freed 167K, 3% free 10724K/11015K, paused 3ms+3ms
10-16 15:04:03.242      532-532/com.example.dev.mbstore I/dalvikvm﹕ Could not find method java.lang.management.ManagementFactory.getThreadMXBean, referenced from method com.mysql.jdbc.MysqlIO.appendDeadlockStatusInformation
10-16 15:04:03.242      532-532/com.example.dev.mbstore W/dalvikvm﹕ VFY: unable to resolve static method 7453: Ljava/lang/management/ManagementFactory;.getThreadMXBean ()Ljava/lang/management/ThreadMXBean;
10-16 15:04:03.242      532-532/com.example.dev.mbstore D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0079
10-16 15:04:03.351      532-532/com.example.dev.mbstore I/System.out﹕ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
    +++ LOG: entry corrupt or truncated
10-16 15:04:03.351      532-532/com.example.dev.mbstore I/System.out﹕ The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

It seems that it does not find the Driver class but I put the MySQL plugin in the dependencies and had it compiled.

Connection class:

public class DatabaseExport{
    private static Connection conn = null;

    public static void conectaDB(String url, String porta, String banco, String usuario, String senha)
    {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://"+url+":"+porta+"/"+banco,usuario,senha);
        }catch(Exception e)
        {
            Exceptions.getException(e);
        }
    }
}
    
asked by anonymous 16.10.2014 / 20:41

2 answers

2

The problem was solved with the creation of a webservice. Mobile applications do not support direct connection with database, an AsyncTask method was created to make the request in the webservice. Examples: link

    
05.08.2015 / 16:27
3

I do not know how you have the rest of the necessary settings so I'll leave the steps you need to be aware of in a generic way:

  • Download the MySQL JDBC Connector (English) and include the even in build path velopment of the Android project.

  • Make the inclusion of what is needed:

    import java.sql.Connection;
    
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.Statement;
    
  • Ensure that the code for JDBC is in AsyncTask , otherwise you can not interact with the database.

  • Ensure that INTERNET permission is required to open sockets , that is, in your manifest you should have:

    <uses-permission android:name="android.permission.INTERNET" />
    
  • Run a test to see if the connection went well or there are errors to solve:

    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection con = DriverManager.getConnection(endereco, utilizador, password);
    
      /* Debug: */
      /* System.out.println("Ligação à BD com sucesso."); */
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    
  • 17.10.2014 / 14:36