0

When the data are correct, the method works, but when I change the password in the database, this method works indefinitely. I would like to if the password or the user is incorrect, the application stopped and show an information notification "or user password is wrong".

To display the information I plan to use AlertDialog and I know how to do it, just I do not know how to connect this with my method myMethod()

Any ideas ?

 public void myMethod()

    {
        Connection conn = null;
        Statement stmt = null;
        try {
        StrictMode.ThreadPolicy ab = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(ab);

            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection(url2, user2, pass2);

         stmt = conn.createStatement();

         String sql = "SELECT abc FROM this";

         ResultSet rs = stmt.executeQuery(sql);

         while (rs.next()) {

         Double ba3 = rs.getDouble("abc");

         list.add(ba3, rs.getDouble("abc"));               

            }


        } catch (SQLException se)
{

 se.printStackTrace();
        } catch (Exception e) 
{
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null)
                    conn.close();
            } catch (SQLException se) 
{
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) 
{
                se.printStackTrace();
            }
        }
    }

edit1:

   button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

            myMethod();

            }
        });
Rambo
  • 11
  • 3

1 Answers1

1

There's a number of things that concern me about this code. Firstly, if you need to connect to a remote database, direct connections are not the way to go for a number of reasons.

  • Security- If the user has direct access, they can get anything from your database. Plus they will have a password into your database. As a result, if the SQL server you are using has a flaw, then they can exploit it. Also, if your permissions are set up wrong, the could wipe your database.

  • Speed- If the users frequently use large queries, then it can bog down your system quickly and needlessly. If you go through a web interface, you can throttle it.

  • Accessibility- Web queries are supported by almost everything. It requires special clients to access SQL databases directly.

As well as the fragile nature of mobile data meaning it may be difficult to keep a socket open. I would look into creating a REST API to act as a middle man.

Secondly:

StrictMode.ThreadPolicy ab = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(ab);

Here you're basically telling Android to ignore any network calls on the UI thread (or permitting any sort of intensive work to happen on the UI thread). Again, this is a very bad idea as any blocking calls such as your database connection will cause big slowdown issues for your UI. For what you need, I recommend looking at this question - How to get data to/from a socket in a thread?

However, if you absolutely have to implement it this way, I'd structure it like this:

public void callerOnBackgroundThread() {
    try {
        myMethod();
    } catch (Exception se) {
        e.printStackTrace();

        if (e instanceof ClassNotFoundException) {
            // Error is related to your jdbc driver
        } else if (e instanceof SQLException) {
            // Error is related to your SQL connection
        } else {
            // Error is something else
        }
    }
}

private void myMethod() throws SQLException {
    Connection conn = null;
    Statement stmt = null;
    SQLException exception = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(url2, user2, pass2);
        stmt = conn.createStatement();

        String sql = "SELECT abc FROM this";
        ResultSet rs = stmt.executeQuery(sql);

        while (rs.next()) {
            Double ba3 = rs.getDouble("abc");
            list.add(ba3, rs.getDouble("abc"));
        }
    } catch (Exception e) {
        exception = e;
    } finally {
        try {
            if (stmt != null)  conn.close();
        } catch (SQLException se) {
            exception = se;
        }
    }

    if (exception != null) throw exception;
}

With callerOnBackgroundThread() being called on a background thread for the reasons mentioned above.


Regarding using instanceof to sort your exceptions when showing the dialog, I meant something like this:

if (e instanceof ClassNotFoundException) {
    // Error is related to your jdbc driver
} else if (e instanceof SQLException) {
    // Error is related to your SQL connection
} else {
    // Error is something else
}

So you can use your AlertDialog appropriately depending on the type of error.

Community
  • 1
  • 1
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • that's what I was suggesting – salvolds Jan 12 '17 at 21:40
  • i should now call callerOnBackgroundThread() instead myMethod() in my button ? – Rambo Jan 12 '17 at 21:50
  • Yes, but make sure it's wrapped in a new thread. Just adding `callerOnBackgroundThread()` from your onClickListener() will still call it on the UI thread! – Michael Dodd Jan 12 '17 at 21:52
  • now androidStudio underlines this: "Class.forName("com.mysql.jdbc.Driver");" and say "unhandled exception java.lang.ClassNotFoundException" – Rambo Jan 12 '17 at 21:59
  • Change the instances of `SQLException` to `Exception`. I still advise against a direct connection though. – Michael Dodd Jan 12 '17 at 22:08
  • did not work, AlertDialog appear when password is incorrect, but also appear when password is correct – Rambo Jan 12 '17 at 22:12
  • That's because a different type of `Exception` is being thrown - something is still going wrong. You'll need to tailor your dialog based on the type of exception thrown - look into using `instanceof` against the exception. – Michael Dodd Jan 12 '17 at 22:18
  • you mean it, I swapped all the 'exception' on the 'instanceof' ? I see it for the first time on the eyes – Rambo Jan 12 '17 at 22:28
  • No. I mean using the keyword `instanceof` to determine what sort of exception was thrown. Will edit. – Michael Dodd Jan 12 '17 at 22:31
  • I do not understand ;/ – Rambo Jan 12 '17 at 22:32
  • now i know, this error is with JDBC driver, still with correct password Alert appear , but if i use other method first, and then callerOnBackgroundThread(), Alert not appear – Rambo Jan 12 '17 at 22:53
  • Check this question for details on the various reasons `SQLException` may be thrown, and how you can organise your exceptions to catch specific cases. I won't be able to help from this point onwards as this is starting to get outside my level of knowledge, and further away from the original question. http://stackoverflow.com/questions/35590708/jdbc-catching-login-failure-exception-and-re-promts-for-input – Michael Dodd Jan 12 '17 at 22:57