Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC connection in Android

Tags:

android

jdbc

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.

like image 718
djk Avatar asked Dec 15 '10 07:12

djk


People also ask

Can I use JDBC in Android?

The JDBC API is an alternative to the drop-in replacement. It is possible to build Berkeley DB SQL for Android in such a way that a JDBC API is exposed to Android application developers. This is done using the Android NDK. This section describes how to build and use the BDB JDBC driver for Android.

Can we use JDBC in Android Studio?

So How can I add The JDBC Driver to Android Studio ?? Please reconsider this. JDBC is not designed for use this way. In particular, you will wind up having to have database account data, including passwords, in your app, which is bad from a security standpoint.

What is JDBC connection with example?

getConnection() method to create a Connection object, which represents a physical connection with the database. Execute a query − Requires using an object of type Statement for building and submitting an SQL statement to the database. Extract data from result set − Requires that you use the appropriate ResultSet.

How to connect and read data from MySQL database directly from Android?

This example show you how to connect and read data from MySQL database directly from Android. The following steps and code snippet will show you how to do it. Add the MySQL JDBC driver into your project dependencies. Open the app/build.gradle file and add the dependency.

How do I add the MySQL JDBC driver to my project?

Add the MySQL JDBC driver into your project dependencies. Open the app/build.gradle file and add the dependency. ... ... dependencies { ... ... implementation 'mysql:mysql-connector-java:5.1.49' }

How do I Pass JDBC code to a MySQL server?

An other approach is to use a Virtual JDBC Driver that uses a three-tier architecture: your JDBC code is sent through HTTP to a remote Servlet that filters the JDBC code (configuration & security) before passing it to the MySql JDBC Driver. The result is sent you back through HTTP. There are some free software that use this technique.

How do I connect to MariaDB on Android?

If you want to connect to MariaDB you can change the JDBC driver dependency using 'org.mariadb.jdbc:mariadb-java-client:1.8.0', also update the JDBC url in the code snippet by replacing mysql with mariadb. Next, add internet permission to our application in AndroidManifest.xml file.


2 Answers

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.

like image 55
Monstieur Avatar answered Nov 15 '22 23:11

Monstieur


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.

like image 35
Sebastian Roth Avatar answered Nov 16 '22 00:11

Sebastian Roth