0

I can't connect to a remote mysql database in my Android app, because the app can't find a suitable driver.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        applicationId "com.example.test.testapp"
        minSdkVersion 20
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    //https://stackoverflow.com/questions/37048731/gson-library-in-android-studio/37049457
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.android.support:design:26.1.0'
    //    implementation 'org.mariadb.jdbc:mariadb-java-client:2.1.2'
    compile 'com.android.support:support-annotations:27.1.1'
//    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//    implementation files('libs/mysql-connector-java-5.1.46.jar')
    //    compile project(':mysql-connector-java-5.1.46')
    //    compile project(':libs/mysql-connector-java-5.1.46.jar')
    implementation files('libs/mysql-connector-java-5.1.46-bin.jar')
}

This is my java class where the exception occurs.

public ServerSave() throws SQLException, ClassNotFoundException {
        // load and register JDBC driver for MySQL
        Class.forName("com.mysql.jdbc.Driver");

        this.conn = getConnection("jdbc:mariadb://remote.host.example/" + SQL_DATABASE, SQL_USER_NAME, SQL_PASSWORD);
    }

I thing this has is a configuration problem but i don't know how to solve it. I copied the mysql jar file to libs directory in my android project folder. I am using android studio for this project.

Thanks for your help.

Ace of Spade
  • 388
  • 3
  • 22
  • 1
    Connecting your Android application remotely to MySQL is generally a **very bad idea** as exposing your MySQL instance to the general internet is extremely risky. Is it possible to build an API layer between your application and the database? That way you can create a security buffer and have more fine-grained access controls. – tadman Jul 12 '18 at 18:09
  • my idea is to handle this with the mysql user access permissions and stored procedures – Ace of Spade Jul 12 '18 at 18:11
  • 1
    This is a common misunderstanding of what database security models are for and what they can do. An API can implement a very sophisticated access control system. MySQL Itself really can't. It's not intended to be exposed as a public service. Stored procedures are not enough protection. The most valuable asset in your system is the database and it should be locked down and protected as much as possible. If you give direct access you've multiplied your risks by several orders of magnitude. – tadman Jul 12 '18 at 18:14
  • @AceofSpade to even get into your database you've already given away the password to your db in your APK. At that point you're screwed. This is the number 1 thing you should never do. – Gabe Sechan Jul 12 '18 at 18:24
  • Isn't it more likely that i mess something in my api up than there is a bug in the database ? But of course to using both is better. – Ace of Spade Jul 12 '18 at 18:24
  • It isn't about bugs in the db, its about malicious use with your now public (its in the apk) account. Stored procedures? Just run SET @TRIGGER_CHECKS = False; There, all turned off. Once they're in your db, you are screwed. Datbases are coded to provide protections against mistakes, not malicious input. Passwords should never leave hardware you control. – Gabe Sechan Jul 12 '18 at 18:28

1 Answers1

0

I did not solve it directly. I created a rest api who talks to the mysql database.

Ace of Spade
  • 388
  • 3
  • 22