开发者

JDBC connection in Android

Is there anyone who tried JDBC connection in android because in Android 2.3 JDBC is supported.

I have to connect with Mysql without web service.

I have made application but it gives me error

public class MysqlConnect extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; 
String password = "root";
try {
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  conn.close();开发者_开发问答
  System.out.println("Disconnected from database");
} catch (Exception e) {
  e.printStackTrace();
}
}
}

i am getting error in getConnection. error is like java.lang.VerifyError : com.mysql.jdbc.MysqlIO

Thanks in advance.


I have successfully connected to MySQL, Oracle and SQL Server directly from an Android device using JDBC and the regular type 4 drivers used for desktop Java applications for the respective databases.

Just search for a desktop JDBC sample. The same code works without any modifications on Android. Make sure you add the .jar file of the driver to the project in Eclipse. The compiler will automatically convert the .jar driver into Dalvik compatible package.


The VerifyError probably happens because of wrong class file signature.

Check here:

  • Android java.lang.VerifyError?
  • java.lang.VerifyError

JDBC was included in previous releases as well - the key issue is to include the proper driver class for your database and make sure it can work with the Android runtime.


I had the same issue, two things solved my problem:

  1. You must remember to put your JDBC connection code in an AsyncTask, other wise you will BURN BURN BURN!

  2. Do NOT mistake "INTERNET" permission for the "uses INTERNET" permission. You need the latter to perform JDBC.

If none of above solved your problem, check the logcat line starting with "caused by:" in your warning lines. Googling that line gets you close to finding a solution.


I had a similar problem, try to run connection code from a new thread not main and change the localhost server address.


I've got the same error.

Just adding this line to your gradle dependency resolves the error.

compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.44'

And you can change the version according to your need.

After adding this, gradle sync will be taking care of everything, you just have to connect to DB in the same way as you do in simple java jdbc program. I've developed my app using the android studio.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜