Im trying to learn how to use the JDBC to connect a application to a mysql database.
Im using the Android Studio.
I downloaded \"mysql-connector-java-5.1.37\" f
Try to sync your project
Tools -> Android -> Sync Project with Gradle Files
And then rebuild your project:
build -> rebuild project
I had the same issue and the problem was in the mysql-connector-java-5.1.37.jar file. I downloaded an older version (mysql-connector-java-3.0.17-ga-bin.jar) from here and worked like a charm.
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);
Just in case, don't forget to compile it in your build.gradle:
dependencies {
//other stuff
compile files('libs/mysql-connector-java-3.0.17-ga-bin.jar')
}
PD: This only works if you use the connection from and AsyncTask
Because Distributed mysql-connector-java-5.1.37-bin.jar is compiled for Java1.8, current Android dex compiler cannot recognize it.
$ javap -v com/mysql/jdbc/JDBC42Helper.class| grep "major version"
major version: 52
On the oher hand, mysql-connector-java-5.1.36.jar is compiled for Java1.6.
$ javap -v com/mysql/jdbc/JDBC4Connection.class |grep "major version"
major version: 50
5.1.37 supports JDBC4.2, so requires Java1.8.