1

I have installed mysql in my computer.and also have jetty.. My requirement:- I have a database student with table name ari; ari table is:-

+----+-------+
| id | name  |
+----+-------+
| 22 | Sandy |
+----+-------+

from My Android program I want to fetch it from database..I have done this:- MainActivity.java

public class MainActivity extends Activity {
    private static final String url = "jdbc:mysql://localhost:3306/student";
    private static final String user = "root";
    private static final String pass = "root";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testDB();
    }
public void testDB() {
        TextView tv = (TextView)this.findViewById(R.id.textView1);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, user, pass);
            String result = "Database connection success\n";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from ari");
            ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()) {
                result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
                result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
                        }
            tv.setText(result);
        }
        catch(Exception e) {
            e.printStackTrace();
            tv.setText(e.toString());
        }   
    }
}

and activity_main.xml is:--

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >
    <TextView
         android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>

and the manifext file is:---

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.TourBus.info"
    android:versionCode="1"
    android:versionName="1.0-SNAPSHOT" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" /> 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

But the problem is when I run that program in my android phone or emulator it is showing that:--

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:  
Could not create connection to database server. 

I also have the jar file:--

mysql-connector-java

can anybody help me??pls

7
  • androidhive.info/2012/05/how-to-connect-android-with-php-mysql Commented Nov 22, 2014 at 5:01
  • can you do that et al? To the best of my knowledge you need a webservice to connect to your database as your database is running on some server and your application is running on mobile Commented Nov 22, 2014 at 5:02
  • @Naveen Tamrakar,about site example is very good..but it is php..I don't know php..Is there any example in java+android+mysql?? Commented Nov 22, 2014 at 5:06
  • @subhodeepchatterjee first u think your device that connect on use panel so u think what u want to do Commented Nov 22, 2014 at 5:09
  • in android there is also buit in database sqlite database Commented Nov 22, 2014 at 5:09

1 Answer 1

0

If you want the emulator client to contact a server running on the same host, use the alias 10.0.2.2 to refer to the host computer's loopback interface. From the emulator's perspective, localhost or 127.0.0.1 refers to its own loopback interface. So change

private static final String url = "jdbc:mysql://localhost:3306/student";

to

private static final String url = "jdbc:mysql://10.0.2.2/student";

Check this link

EDIT

In your /etc/mysql/my.cnf try commenting out skip-networking by adding # at the beginning of that line.

If that doesn't solve your problem try checking this Solving a “communications link failure” with JDBC and MySQL for solving your com.mysql.jdbc.exceptions.jdbc4.CommunicationException:communication link failure error after changing your url as shown above.

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this one.... but its saying,"com.mysql.jdbc.exceptions.jdbc4.CommunicationException:communication link failure ;...The last package sent successfully to the server ..The driver has not receive any packets from server"
SHOW VARIABLES WHERE Variable_name = 'port'; use this command and check your port no. is 3306 or not. Also try the url without port no
where??in terminal???? iirc-Dell-DXP061:~$ SHOW VARIABLES WHERE Variable_name = 'port'; SHOW: command not found
"C:\Program Files\mysql\bin\my.ini try commenting out skip-networking by adding # at the beginning of that line." what will be the command for ubuntu?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.