0

I'm trying to connect to MySQL in Android:

try{
            String url = "jdbc:mysql://192.168.0.105:3306/qrdb";
            String username = "root";
            String password = "****";
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

            String sqlCommand = "CREATE TABLE items (Id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), price INT)";

            try (Connection conn = DriverManager.getConnection(url, username, password)){

                Statement statement = conn.createStatement();
                // создание таблицы
                statement.executeUpdate(sqlCommand);

                Log.i("mytag","Database has been created!");
            }
        }
        catch (SQLException ex) {
            // handle any errors
            Log.i("mytag","SQLException: " + ex.getMessage());
            Log.i("mytag","SQLState: " + ex.getSQLState());
            Log.i("mytag","VendorError: " + ex.getErrorCode());
        }
        catch(Exception ex){
            Log.i("mytag","Connection failed...");

            Log.i("mytag", "" + ex);
        }

Throws an exception:

java.lang.UnsupportedOperationException

MySQL Connector/J 8.0 is installed. In the usual Java project, everything works.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
rywes
  • 11
  • 3
  • or tell me alternative methods – rywes Jan 15 '19 at 15:19
  • I need to connect to any database remotely – rywes Jan 15 '19 at 15:20
  • 1
    Just as a side note, directly connecting to a MySQL database from an Android app via jdbc is considered [very bad practice](https://stackoverflow.com/questions/26470117/can-we-connect-remote-mysql-database-in-android-using-jdbc) – Michael Dodd Jan 15 '19 at 15:27
  • so how should it be? through what? – rywes Jan 15 '19 at 15:36
  • Store your data locally using an SQLite or Room database, and sync with your remote server using a REST API. That way you can better handle situations arising from intermittent data connectivity, as you often find on mobile devices. – Michael Dodd Jan 15 '19 at 15:39
  • like @MichaelDodd said You need to make some kind of server side API ('s) (REST, SOAP, XML-RPC or Websockets as communication protocols) on which a Android app can connect – Raymond Nijland Jan 15 '19 at 15:41
  • Do you have a good example, example, doc? I don't understand how to google it. – rywes Jan 15 '19 at 15:49
  • I'm from Russia. Russian google is not searched – rywes Jan 15 '19 at 15:50
  • Here's a link to [documentation on data storage](https://developer.android.com/guide/topics/data/data-storage#db) – Bö macht Blau Jan 15 '19 at 19:01

0 Answers0