LIN JAKE
LIN JAKE

Reputation: 35

How to make android connect directly to mysql

I want to use android built-in java.sql to drive html: https://developer.android.com/reference/java/sql/package-summary, But there is no way to access My code:

    public class MainActivity extends AppCompatActivity {
    private static final String DB_URL = "jdbc:mysql://127.0.0.1/pcDB";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String query = "SELECT * FROM gpstable";
        try {
            Class.forName("java.sql.Driver");
            Connection connection = DriverManager.getConnection(DB_URL,"koer3740","password");

            Toast.makeText(this,"Successful Connection",Toast.LENGTH_LONG).show();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

In addition, according to this teaching html: http://seotoolzz.com/android/android-dynamic-listview-mysql-jdbc.php, using the external jdbc, found that DriverManager.class is not in jdbc

My English is not good, sorry.

Upvotes: 0

Views: 3666

Answers (1)

waterbyte
waterbyte

Reputation: 251

Although it is not recommended for Android to connect with mysql directly, you can possibly do so. I actually found another stack overflow link that explains why it is not recommended (in first answer) and also gives answer of your original query (in second answer). Please have a look:

Can we connect remote MySQL database in Android using JDBC?

Upvotes: 1

Related Questions