简体   繁体   中英

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.

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:

https://stackoverflow.com/questions/26470117/can-we-connect-remote-mysql-database-in-android-using-jdbc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM