0

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?

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Hei Ranch
  • 3
  • 1
  • 3

5 Answers5

1

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.

Community
  • 1
  • 1
nAndroid
  • 862
  • 1
  • 10
  • 25
0

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.

Paweł Nadolski
  • 8,296
  • 2
  • 42
  • 32
0

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

Barmaley
  • 16,638
  • 18
  • 73
  • 146
0

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.

K_Anas
  • 31,226
  • 9
  • 68
  • 81
  • i know and my former version used this type of connection, but my boss thinks different. we only use a LAN connection and dont want to publish this app. – Hei Ranch May 29 '12 at 10:14
  • @K_Anas : Can you please show code sample of what you have said? I mean creating web service to access mysql db. – GAMA Jan 07 '13 at 09:56
0

It should work, May be you should try with another JavaSDK and another Android Platform.

Peerapat A
  • 420
  • 4
  • 13