0

i've been trying to make a simple hello world app that uses jdbc to query mysql and retrieve/println results.

Im using this tutorial from tutorials point adopted to android code:

http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm

Here is my adopted version:

    package com.example.myfirstapp0006;

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import java.sql.*;

    public class MainActivity extends ActionBarActivity {

        // JDBC driver name and database URL
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        static final String DB_URL = "jdbc:mysql://localhost/EMP";

        // Database credentials
        static final String USER = "username";
        static final String PASS = "password";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Connection conn = null;
            Statement stmt = null;
            try{
                // STEP 2: Register JDBC driver
                Class.forName("com.mysql.jdbc.Driver");

                // STEP 3: Open a connection
                System.out.println("Connecting to database...");
                conn = DriverManager.getConnection(DB_URL,USER,PASS);

                // STEP 4: Execute a query
                System.out.println("Creating statement...");
                stmt = conn.createStatement();
                String sql;
                sql = "SELECT id, first, last, age FROM Employees";
                ResultSet rs = stmt.executeQuery(sql);

                // STEP 5: Extract data from result set
                while(rs.next()) {
                    // Retrieve by column name
                    int id = rs.getInt("id");
                    int age = rs.getInt("age");
                    String first = rs.getString("first");
                    String last = rs.getString("last");

                    // Display values
                    System.out.println("ID: " + id);
                    System.out.println(", Age: " + age);
                    System.out.println(", First: " + first);
                    System.out.println(", Last: " + last);
                }

                // STEP 6: Clean-up environment
                rs.close();
                stmt.close();
                conn.close();
            }catch(SQLException se){
                //Handle errors for JDBC
                se.printStackTrace();
            }catch(Exception e){
                //Handle errors for Class.forName
                e.printStackTrace();
            }finally{
                //finally block used to close resources
                try{
                    if(stmt != null)
                        stmt.close();
                }catch(SQLException se){
                    se.printStackTrace();
                }//end finally try
            }//end try
            System.out.println("Goodbye!");
        }//end onCreate

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}//end MainActivity

Im getting these errors:

11-03 18:55:14.854: W/System.err(389)   com.mysql.jdbc.CommunicationsException: Communications link failure
11-03 18:55:14.854: W/System.err(389):  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 
11-03 18:55:14.854: W/System.err(389):  at  com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1032)
11-03 18:55:14.864: W/System.err(389):  at  com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338) 
11-03 18:55:14.864: W/System.err(389):  at  com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2232)
11-03 18:55:14.864: W/System.err(389):  at  com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
11-03 18:55:14.864: W/System.err(389):  at  com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
11-03 18:55:14.864: W/System.err(389):  at  com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790) 
11-0 18:55:14.864:  W/System.err(389):  at  com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:392)
11-03 18:55:14.874: W/System.err(389):  at  com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
11-03 18:55:14.874: W/System.err(389):  at  java.sql.DriverManager.getConnection(DriverManager.java:191) 
11-0 18:55:14.874:  W/System.err(389):  at  java.sql.DriverManager.getConnection(DriverManager.java:226) 
11-0 18:55:14.874:  W/System.err(389):  at  com.example.myfirstapp0006.MainActivity.onCreate(MainActivity.java:32)
11-03 18:55:14.874: W/System.err(389):  at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-03 18:55:14.883: W/System.err(389):  at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-03 18:55:14.883: W/System.err(389):  at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-03 18:55:14.883: W/System.err(389):  at  android.app.ActivityThread.access$2300(ActivityThread.java:125) 
11-0 18:55:14.883:  W/System.err(389):  at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-03 18:55:14.883: W/System.err(389):  at  android.os.Handler.dispatchMessage(Handler.java:99) 
11-03 18:55:14.894: W/System.err(389):  at  android.os.Looper.loop(Looper.java:123) 
11-03 18:55:14.894: W/System.err(389):  at  android.app.ActivityThread.main(ActivityThread.java:4627) 
11-0 18:55:14.894:  W/System.err(389):  at  java.lang.reflect.Method.invokeNative(Native Method) 
11-0 18:55:14.894:  W/System.err(389):  at  java.lang.reflect.Method.invoke(Method.java:521) 
11-03 18:55:14.894: W/System.err(389):  at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-03 18:55:14.894: W/System.err(389):  at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-0 18:55:14.894:  W/System.err(389):  at  dalvik.system.NativeStart.main(Native Method) 
11-03 18:55:14.894: W/System.err(389):  Caused by: java.net.SocketException: Permission denied 
11-03 18:55:14.904: W/System.err(389):  at  org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method) 
11-03 18:55:14.904: W/System.err(389):  at  org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
11-03 18:55:14.904: W/System.err(389):  at  org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
11-03 18:55:14.904: W/System.err(389):  at  java.net.Socket.startupSocket(Socket.java:774) 
11-03 18:55:14.904: W/System.err(389):  at java.net.Socket.<init>(Socket.java:316) 
11-0 18:55:14.904:  W/System.err(389):  at  com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:243)
11-03 18:55:14.913: W/System.err(389):  at  com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297) 
11-03 18:55:14.913: W/System.err(389):  ... 22 more

Any clues on that?

VenomVendor
  • 15,064
  • 13
  • 65
  • 96
Jacs
  • 1,437
  • 4
  • 21
  • 31

3 Answers3

0

This often occurs when MySQL reaches its maximum number of concurrent connections. Try restarting MySQL.

If you require more concurrent connections, you can update my.cnf by adding this line under [mysqld]:

max_connections=x

Where x is the maximum number of concurrent connections.

You can view the number of active connections by running:

SHOW STATUS WHERE `variable_name` = 'Threads_connected'

There is more information on the MySQL website as well:

http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html

Andy Senn
  • 649
  • 5
  • 13
0

Android has inbuilt SqlLite database by default. Did you install mysql database as an additional package. Where is your mysql database located ?

Refer this link

Kakarot
  • 4,252
  • 2
  • 16
  • 18
0

Where did you install mysql? Try to change the host in the url; now you are trying to connect to localhost: static final String DB_URL = "jdbc:mysql://localhost/EMP";

You cannot run mysql on android phone, you can choose other db if you want to execute it on local(for example you can use SQLite).

ricgra
  • 1
  • 1
  • But if i choose to use SQLite, wouldnt the data be saved on the phone? – Jacs Nov 03 '14 at 20:20
  • SQLite will be run on your phone; so your data would be on the phone. If you want to query data on an external db you must have some webservices to make available your data to an android client. – ricgra Nov 03 '14 at 20:24
  • then how to use an external database (on a server) to store data and query it from the phone? – Jacs Nov 03 '14 at 20:29
  • There are some ways; take a look at these: http://stackoverflow.com/questions/15732853/how-to-connect-android-app-to-mysql-database http://www.tutorialspoint.com/android/android_php_mysql.htm – ricgra Nov 03 '14 at 20:30
  • Why do you want an external db? Do you have a lots of data to persit on your db? – ricgra Nov 03 '14 at 20:42
  • A good portion of my app will relay on data stored on a external db. The app includes social network features that can't be stored on everyone's phone... – Jacs Nov 04 '14 at 00:10