Home > Database > Mysql Tutorial > How to Connect Android to MySQL Using JDBC Driver?

How to Connect Android to MySQL Using JDBC Driver?

Susan Sarandon
Release: 2024-11-12 03:29:02
Original
480 people have browsed it

How to Connect Android to MySQL Using JDBC Driver?

Connecting Android to MySQL Using MySQL JDBC Driver

Problem: Difficulties connecting to MySQL from within an Android project using the JDBC driver, while the same code works in a Java project.

Solution:

To connect Android with MySQL effectively, follow these steps:

  1. Download and Include Driver: Obtain the mysql-connector-java-3.0.17-ga-bin.jar driver and add it to the "libs" folder within your Android project. To include it, go to Configure Build Path > Add JAR.
  2. Grant Permissions: In the androidManifest.xml file, include permissions for INTERNET connectivity: .
  3. Create Connection Code: Utilize the following code snippet for connection:
try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
    System.err.println("Cannot create connection");
}
try {
    connection = DriverManager.getConnection("jdbc:mysql://192.168.xx.xx:3306/dbname", "root", "password");
    Statement statement = connection.createStatement();
    
    String query = "SELECT column1, column2 FROM table1 WHERE column3 = ";
    query = query + "'" + variable + "'";
    ResultSet result = statement.executeQuery(query);
} catch (Exception e) {
    System.err.println("Error");
}
Copy after login

Additional Advice:

  • For certain versions of Target SDK, it might be necessary to remove the Target SDK version from the manifest to prevent connectivity issues.
  • Ensure that your firewall allows outbound connections on port 3306, which is the default MySQL port.

The above is the detailed content of How to Connect Android to MySQL Using JDBC Driver?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template