BoardiesITSolutions / Android-MySQL-Connector

Native MySQL Connector for Android
MIT License
51 stars 15 forks source link

'IConnectionInterface' is abstract; cannot be instantiated #22

Closed FoxTale-Labs closed 3 years ago

FoxTale-Labs commented 3 years ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

  1. Create new Connection
    Connection  mysqlConnection = new Connection("", "", "", 3306, "", new IConnectionInterface());
  2. Then you see an error ('IConnectionInterface' is abstract; cannot be instantiated)

Expected behavior It should work normally and not show error

Logcat If applicable, provide logcat output (ensure no sensitive information is in the output (if it is obfuscate it)

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

FoxTale-Labs commented 3 years ago

BTW: My full code:

Connection  connection = new Connection("", "", "", 3306, "", new IConnectionInterface());
Statement statement = connection.createStatement();
statement.executeQuery("SELECT * FROM users", new IConnectionInterface());

(Both IConnectionInterface() methods give same error)

boardy commented 3 years ago

Hey, For the IConnectionInterface you need to implement the methods of the interface, so you would need to do something like the below

connection = new Connection(server, username, password, port, database, new IConnectionInterface() {
            @Override
            public void actionCompleted()
            {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run()
                    {
                        Toast.makeText(MainActivity.this, "Connected to database successfully", Toast.LENGTH_SHORT).show();
                        //selectTableRecords();
                    }
                });
            }

            @Override
            public void handleInvalidSQLPacketException(InvalidSQLPacketException ex)
            {
                showError("Invalid SQL Packet Exception: ", ex.toString());
            }

            @Override
            public void handleMySQLException(MySQLException ex)
            {
                showError("MySQL Exception", ex.toString());
            }

            @Override
            public void handleIOException(IOException ex)
            {
                showError("IO Error", ex.toString());
            }

            @Override
            public void handleMySQLConnException(MySQLConnException ex)
            {
                showError("MySQLCon Exception", ex.toString());
            }

            @Override
            public void handleException(Exception exception)
            {
                showError("General Exception", exception.toString());
            }
        });
    }

It would be the same for the IResultInterface. If you are using something like Android Studio, it should normally allow you to auto implement each method stub so you don't need to type each method from the interface manually.

Thanks Chris

FoxTale-Labs commented 3 years ago

image Now it made things bulkier and still with errors, because it wants to implement more things. I think you also should try working out making that smaller and better ;D

boardy commented 3 years ago

Are you importing from com.BoardiesITSolutions.AndroidMySQLConnector.Connection instead of java.sql.Connection that might explain the errors.

Also worthwhile checking that you are using the latest tag with MySQL8 prefix as this is the recommended version to use.

Unfortunately there isn't really another way of implementing the library due to Android requring any network activity to be done on a separate thread, so there has to be callbacks used in order to know when the library completes a certain task.

There are several different call backs as there are several different error cases that can happen so it allows the user to treat each different error type differently if required.

You don't need to implement the IConnectionInterface everytime however, you can create a single instance of it and then just pass the instance when needed so you only need to implement it once.

For example:


private IConnectionInterface iConnInterface = new IConnectionInterface() {
            @Override
            public void actionCompleted()
            {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run()
                    {
                        Toast.makeText(MainActivity.this, "Connected to database successfully", Toast.LENGTH_SHORT).show();
                        //selectTableRecords();
                    }
                });
            }

            @Override
            public void handleInvalidSQLPacketException(InvalidSQLPacketException ex)
            {
                showError("Invalid SQL Packet Exception: ", ex.toString());
            }

            @Override
            public void handleMySQLException(MySQLException ex)
            {
                showError("MySQL Exception", ex.toString());
            }

            @Override
            public void handleIOException(IOException ex)
            {
                showError("IO Error", ex.toString());
            }

            @Override
            public void handleMySQLConnException(MySQLConnException ex)
            {
                showError("MySQLCon Exception", ex.toString());
            }

            @Override
            public void handleException(Exception exception)
            {
                showError("General Exception", exception.toString());
            }
        });
}

Connection connection = new Connection(server, username, password, port, database, iConnInterface);

Thanks Chris

FoxTale-Labs commented 3 years ago

Ahh yes, I forgot that I implemented java.sql.connection. Now it fixed it, thanks ;D.