Android Studio 2.3 error jdbc mysql connection

2

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.support.v7.app.AppCompatActivity;
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 {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String url = "jdbc:mysql://database";
        String user = "user";
        String password = "pass";
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection(url, user, password);
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from table");
            con.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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 {
        jcenter()
    }
}

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
source

1 answer

1

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