Reputation: 3
I found some answers but nothing was useful.
I added mysql-connector-java-5.1.7-bin.jar to referenced libraries and the following method to my main class activity.
private void getServerData(String id, String type) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
{
Log.i("Database Connection", " requesting data from server");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/db_easy_maintenance", "root", "maintenance");
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("SELECT Name FROM industrial_object");
String entry;
while(rs.next())
{
entry=rs.getString("Name");
Log.i("Database Connection", "getting " + entry);
}
con.close();
}
I start my app and get an java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.lang.Class.classForName.
If I don't use try ...catch... then i get this Exception before compiling.
What's wrong?
Upvotes: 0
Views: 6714
Reputation: 960
see: http://developer.android.com/search.html#q=dx&t=0 and Android JDBC not working: ClassNotFoundException on driver - my answer on the bottom gives ridiculously specific instructions, although I am curious how switching to 1.6 was useful.
Upvotes: 1
Reputation: 430
It should work, May be you should try with another JavaSDK and another Android Platform.
Upvotes: 0
Reputation: 31466
JDBC is infrequently used on Android, and I certainly would not recommend it.
JDBC is designed for high-bandwidth, low-latency, highly-reliable network connections (e.g., desktop to database server, Web application server to database server). Mobile devices offer little of these, and none of them consistently.
So the alternative that i advice you to use consist on:
Create a Web service around your database and access that from Android.
As side benefits, you improve security (vs. leaving your database open), can offload some business logic from the client, can better support other platforms (e.g., Web or Web-based mobile frameworks), etc.
Upvotes: 0
Reputation: 16363
It might not work since Android uses a bit different bytecode. Tyr to check whether MySQL JDBC library correctly translated to Dalvik's format. There's a tool named dex for translating JVM bytecode into Dalvik's one. If JDBC driver consists classes not portable to Android ones or some native code - you can't use it in Android
Upvotes: 0
Reputation: 8474
Verify that the library is exported (has exported="true"
attribute in .classpath
file). You can also do that in appropriate tab in project settings in Eclipse.
Upvotes: 0