Home > Database > Mysql Tutorial > Why Can't I Directly Connect My Android App to a MySQL Database Using JDBC?

Why Can't I Directly Connect My Android App to a MySQL Database Using JDBC?

Linda Hamilton
Release: 2024-12-07 03:59:11
Original
955 people have browsed it

Why Can't I Directly Connect My Android App to a MySQL Database Using JDBC?

Can't Connect to MySQL from Android with JDBC

This code snippet attempts to connect to MySQL in localhost from Android using JDBC. However, it only displays the actions in the catch section. It is not clear if this is a connection problem or not.

package com.test1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Test1Activity extends Activity {
    ...

    public void onCreate(Bundle savedInstanceState) {
        ...
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://10.0.2.2:8080/example","root","");
            ...
        }
        catch(Exception e)
        {
            ...
        }
    }
}
Copy after login

The exception message indicates that:

  • java.lang.management.ManagementFactory.getThreadMXBean is referenced from method com.mysql.jdbc.MysqlIO.appendDeadlockStatusInformation
  • javax.naming.StringRefAddr is referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
  • javax.naming.Reference.get is referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom

Solution

It is not possible to directly connect to a MySQL database from Android natively.

Consider using a web service to pass requests to the database and return the response.

Alternatives

  • http://www.helloandroid.com/tutorials/connecting-mysql-database
  • http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/8339-connect-android-mysql-database-tutorial.html
  • http://codeoncloud.blogspot.com/2012/03/android-mysql-client.html

Security Considerations

Using JDBC to connect to a database from Android can expose sensitive information, such as usernames and passwords. Consider using a more secure method, such as a web service or a dedicated Android database library.

The above is the detailed content of Why Can't I Directly Connect My Android App to a MySQL Database Using JDBC?. 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