Android Studio 2.3 error jdbc mysql connection


Hi, I have downloaded and installed Android Studio 2.3 and I want to make a connection to a remote mysql database.

My version of java is the java version "1.7.0_121".

I have pasted the file mysql-connector-java-6.0.5-bin.jar in the folder /bin of the project this is the java code:

import android.os.Bundle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        String url = "jdbc:mysql://database";
        String user = "user";
        String password = "pass";
        try {
            Connection con = DriverManager.getConnection(url, user, password);
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from table");
        }catch (Exception e) {

and it gives me the following error:


Error: Error converting bytecode to dex: Cause: Dex can not parse version 52 byte code. This is caused by library dependencies that have   been compiled using Java 8 or above. If you are using the 'java'   gradle plugin in a library submodule add targetCompatibility = '1.7'   sourceCompatibility = '1.7' to that submodule's build.gradle file.

I have modified the build.gradle (project: DataBase) files as the error says

allprojects {
    tasks.withType(JavaCompile) {
        sourceCompatibility = '1.7'
        targetCompatibility = '1.7'

    repositories {

build.gradle (Module: app)

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7

But I still get the same error, I get this error just by adding the file mysql-connector-java-6.0.5-bin.jar in the folder /bin of the project.

asked by Pichu 18.04.2017 в 12:02

1 answer


Simply use Java 8 . Put this in your build.gradle :

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
answered by 18.04.2017 в 12:10