1

I'm using Android Studio for 2 Weeks now and I want to write a code, in order to connect to a MySQL Database which is on my own Domain Webspace.

I've tried the Java DB Connector from MySQL itself (mysql-connector-java-gpl-5.1.34.msi).

So first of all i tried the JDBC Connector but i figured out this one is only for SQLLite Databases.

I have bound in the Mysql Java Connector from MYSQL.com and edited the build.gradle and the settings.gradle

SETTINGS GRADLE

include ':app'
include ':Libraries/Mysql-Connector-Java-3.0.17-ga'
include ':Mysql-Connector-Java-3.0.17-ga'

and the latest version of the connector (mysql-connector-java-gpl-5.1.34.msi) into ROOT-libs(selfmade directory) as my DirectoryName was MySQLJ.

I am using API 19

android {
    compileSdkVersion 19
    buildToolsVersion "19.1"

    defaultConfig {
        applicationId "##################" (my company name censored)
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {

            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(':libraries:mysql-connector-java-3.0.17-ga')
}

I TRIED IT WITH minifyEnable true and false

so everytime I want to rebuild the Project as taught in this Tutorial

https://www.youtube.com/watch?v=1MyBO9z7ojk

i get this error exception

  1. Error:(26, 0) Project with path ':libraries:mysql-connector-java-3.0.17-ga' could not be found in project ':app'.
  2. Error:Configuration with name 'default' not found.

so I searched on stackoverflow for answers and i came to this result:

(problem with ~ default) this library is not for AndroidStudio? Because this library hasn't got a own build.gradle so the source of the error exception with this default thingy is the uncompatible library?

Is it possible to write a code to connect to a MYSQL Database with Android Studio? and Where can i find a library for the MYSQL Connector????

viban
  • 11
  • 1
  • 3

1 Answers1

0

This is what works for me:

Download and paste the mysql-connector into your libs folder. WARNING: the latest version (mysql-connector-java-5.1.37.jar) throws some problems while compiling (like this). I recommend an older version (mysql-connector-java-3.0.17-ga-bin.jar) from here.

In your build.gradle:

dependencies {
    //other stuff
    compile files('libs/mysql-connector-java-3.0.17-ga-bin.jar')
}

To get the connection use:

Class.forName("com.mysql.jdbc.Driver").newInstance();
String connection = "jdbc:mysql://"+ your_ip + ":"+ your_port +"/"+   database_name;
conn = DriverManager.getConnection(connection, user, pass); 

PD: This only works if you use the connection from and AsyncTask

Community
  • 1
  • 1
Carlos Borau
  • 1,433
  • 1
  • 24
  • 34