Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting an error from using mysql-connector-java-5.1.39-bin.jar as a library in Android Studio

 
David Mesman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, again! I am making a android app to connect with a sql database (online). I added mysql-connector-java-5.1.39-bin.jar as libary in Android Studio and wrote the basic connect function:

The error I get is:

Thanks in advance!
 
Tim Moores
Bartender
Posts: 3134
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe that library uses Java class file features that aren't supported in Android, or it's meant for a newer JDBC version than Android uses (which is based on Java 5 with some later additions, if memory serves). Try using the -source and -target switches to generate code for Java 6 instead of Java 8 (which is what version 52.0 signifies).

But these are just wild guesses, I've never used JDBC on Android (and it doesn't strike me as a particularly good idea).
 
Dave Tolls
Ranch Hand
Posts: 2103
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Android's happy with Java 7 compilations, from what I've seen on Android Studio.

You need to find a version of the driver that's been compiled pre-8.
 
David Mesman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Android's happy with Java 7 compilations, from what I've seen on Android Studio.

You need to find a version of the driver that's been compiled pre-8.


I don't really understand. I tried googling for jdbc java pre-8 but all I got were unclear results. So do you have a link or something?
 
David Mesman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got an earlier version of jdbc (4-2.0) and still get the same error. I honestly don't know what to do.
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error:unsupported class file version 52.0

That looks like java 8
Java 7 has version number 51
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put this into Google for lots of background:  transformclasseswithdexfordebug
 
Ron McLeod
Bartender
Pie
Posts: 1044
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get an older jar here: mysql-connector-java-5.1.22-bin.jar.zip (797k).  The manifest for the jar shows that it was created with Java 1.5.0_22-b03.

I tried it myself in an Android app, and found that I needed to call Class.forName("com.mysql.jdbc.Driver") to get the driver loaded, and include <uses-permission android:name="android.permission.INTERNET" /> in the application's AndroidManifest.xml file to allow network access.


I agree with Tim's comment (my underlining):
Tim Moores wrote:... I've never used JDBC on Android (and it doesn't strike me as a particularly good idea).

I think a better solution would be to develop a simple HTTP/JSON based protocol for communications between the application and the data source, rather than connecting to the database server directly over a possibly unreliable and insecure wireless network.
 
David Mesman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:I think a better solution would be to develop a simple HTTP/JSON based protocol for communications between the application and the data source, rather than connecting to the database server directly over a possibly unreliable and insecure wireless network.


I tried this as well. But for that I need some other jar files. I downloaded the HTTPClient java package from apache.org.
Here is my full code:


This is a link to a screenshot of my project tree with the httpclient folder:
screenshot

I'm gonna try your solution, but if I get this working it is okay too.
 
David Mesman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Mesman wrote:I'm gonna try your solution, but if I get this working it is okay too.

By using your solution, there are no errors in Android Studio. But as soon as I try to start the app from my cellphone, it stops right away with the message "[appname] has stopped".
So no I am gonna try the HTTPClient. Which still doesn't work. (See one of my previous replies)
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stops right away with the message "[appname] has stopped". 

Can you look at the logcat to see what the problem was?
 
David Mesman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, I'm totally new to making android apps. What is logcat? And where do I find it?
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logcat is written on the Android device.  If you test your app while connected to the IDE with an USB cable, the IDE will be able to display the logcat's contents.
Another way is to use a VM to run an Android simulator all on the PC.  Then no device or cable is needed.
 
Ron McLeod
Bartender
Pie
Posts: 1044
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Mesman wrote:I downloaded the HTTPClient java package from apache.org.

You don't need to download anything - the HttpClient library is in the SDK (well up to SDK version 22 anyway), JSON as well.

Here's a simple example:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic