-1

Good day,

Im currently creating an android program that would connect SQL Database remotely... Im using XAMPP from another computer.. and here is my code to connect: (mysql-connector already imported)

String url = "jdbc:mysql://xxx.xxx.x.xx:3306/mydatabase?allowMultiQueries=true&useOldAliasMetadataBehavior=true";
        String user = "root";
        String password = "mypassword";
        int a = 0;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            a = 1;
            Connection con = DriverManager.getConnection(url, user, password);
            a = 12;
            Statement st = con.createStatement();
            a = 123;
            ResultSet rs = st.executeQuery("SELECT * FROM mytable");
            a = 1234;
            while(rs.next())
            {


            }
            tDisplayS.setText("YES");
        }
        catch(Exception e)
        {
            tDisplayS.setText(a+"");
        }

When I run my program.. I always get a catch of an error and only displays "1" ... what may seem to be lacking in this code? or Im doing it wrong, please help...

Thank you..

Katsuhiro
  • 19
  • 2
  • It would seems like your code hits an exception after a = 1, try to put e->getMessage() in your catch block to display the error and troubleshoot from there. – SteD Oct 31 '14 at 02:35
  • It looks like you're having basic connection problems. I'd try the connection first using mysql in the command line, or a tool like MySQL Workbench. – Patrick Read Oct 31 '14 at 02:39
  • Hello @Sted thanks for your reply... Here is the message of e.getMessage() Communications link Failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. – Katsuhiro Oct 31 '14 at 02:44
  • hi @PatrickRead .. I guess im stuck with this basic problem :D .. hmmm ok Ill try using MySQL Workbench.. thanks – Katsuhiro Oct 31 '14 at 02:47
  • You might want to check this out: http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql – shyam Oct 31 '14 at 04:49
  • connect directly to remote sql server from device is very bad practice, because it is highly affect to the security. – Biraj Zalavadia Oct 31 '14 at 04:54
  • Hello there @BirajZalavadia .. I also tried to connect it to my local database but still I get the error message in my try catch "Communications link Failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server." – Katsuhiro Oct 31 '14 at 05:38
  • @shyam .. I also saw that post.. I tried it but still no luck :( – Katsuhiro Oct 31 '14 at 05:38

1 Answers1

0

I think It is because you are trying to connect remote database from main thread.

Try to connect from Thread.

public void connectToMySql(){

        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "jdbc:mysql://xxx.xxx.x.xx:3306/mydatabase?allowMultiQueries=true&useOldAliasMetadataBehavior=true";
                String user = "root";
                String password = "mypassword";
                int a = 0;
                try
                {
                    Class.forName("com.mysql.jdbc.Driver");
                    a = 1;
                    Connection con = DriverManager.getConnection(url, user, password);
                    a = 12;
                    Statement st = con.createStatement();
                    a = 123;
                    ResultSet rs = st.executeQuery("SELECT * FROM mytable");
                    a = 1234;
                    while(rs.next())
                    {


                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tDisplayS.setText("YES");        
                        }
                    });

                }
                catch(final Exception e)
                {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tDisplayS.setText(e.printStackTrace());        
                        }
                    });

                }
            }
        }).start();

    }

Just copy this method and call it from onCreate() of your activity.

NOTE:

Make sure that your database must grant all host to access. You need to fire query to grant access to all remote host on your database.

This is the query to grant access to app ip.

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77