0

I am trying to connect my app with a MySQL database. I downloaded the MySQL/Java Connector and added the .jar file to my library, but it doesn't work and I'm always getting this message:

I/System.out: java.lang.ClassNotFoundException: com.mysql.jdbc.driver java.lang.NullPointerException

build.gradle


android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    //compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.18'
    implementation files('libs/mysql-connector-java-8.0.18.jar')

}

My class where I try to connect with the db:

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import android.os.AsyncTask;
import android.widget.EditText;


import java.sql.*;
import java.util.ArrayList;
import com.mysql.jdbc.Driver;


public class DBConnection {
    public Connection getConnection() throws Exception
    {
        try {
            String driver = "com.mysql.jdbc.driver";
            String url = "jdbc:mysql://127.0.0.1:3306/test";
            String username = "root";
            String password = "";
            Class.forName(driver).newInstance();
            Connection c = DriverManager.getConnection(url, username, password);
            System.out.println("Connected");
        }catch (Exception e)
        {
            System.out.println(e);
        }
        return null;
    }
    ArrayList<String> array = new ArrayList<>();
    public String read(EditText e1)
    {
        try{
            Connection c = getConnection();
            PreparedStatement statement = c.prepareStatement("SELECT c_name FROM c_customer;");
            ResultSet resultSet = statement.executeQuery();

            while (resultSet.next())
            {
                array.add(resultSet.getString("c_name"));
            }
            e1.setText(array.get(0));
            System.out.printf("Successfull");
        }catch (Exception e)
        {
            System.out.println(e);
        }
        return null;
    }

}

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    DBConnection db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText user = findViewById(R.id.nameInput);
        final EditText pass = findViewById(R.id.passwortInput);

        Button login = findViewById(R.id.loginButton);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    db = new DBConnection();
                    db.read(user);
                }
            }
        });
    }
}

I hope you can help me.

  • Please read: [JDBC vs Web Service for Android](https://stackoverflow.com/q/15853367/295004) – Morrison Chang Jan 05 '20 at 01:20
  • For using web service you have to use php, don't you? I don't have any knowledge in using php. So first I will try it with jdbc and maybe later with web services. But thank you for the hint :) – ForStudy007 Jan 05 '20 at 01:42
  • Web service can be anything include Java/Kotlin (check Spring Boot) or any other language with appropriate framework like Python & Flask or Javascript & Node. – Morrison Chang Jan 05 '20 at 01:47
  • It's a school project and we have to use Android Studio, Java and MySql. So I cant use another environment or language. – ForStudy007 Jan 05 '20 at 02:00
  • As far as I know, MySQL Connector/J 8 does not work on Android as it uses Java features not available on Android. – Mark Rotteveel Jan 05 '20 at 09:18
  • Do you know which connector can I use instead? – ForStudy007 Jan 05 '20 at 12:00
  • Well I tried it with the version 5.1.48 because I saw someone on a youtube video use it, but it doesn't help either. I got the same error. – ForStudy007 Jan 05 '20 at 16:57

1 Answers1

1

Whilst I cannot help you with the ClassNotFoundException (because I haven't worked with Gradle before), I can tell you that the NullPointerException is because you don't return Connection in the getConnection() method. You just return null at the end of the try/catch block. Return the variable c in the try block and return null in the catch block.

Furthermore, your read method also doesn't return a String, again just null. Return your String once you set EditText, or just make the method void if all you are doing is updating the parameters value.

Finally, I don't think you need to throw Exception in the getConnection() method because you handle that already in the try/catch block. Good luck finding out where the other problem is though.

Dylan
  • 11
  • 2