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

1 Answers1

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.

Community
  • 1
  • 1
Johny
  • 2,128
  • 3
  • 20
  • 33
  • 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" – subhodeep chatterjee Nov 22 '14 at 05:19
  • `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 – Johny Nov 22 '14 at 05:24
  • where??in terminal???? iirc-Dell-DXP061:~$ SHOW VARIABLES WHERE Variable_name = 'port'; SHOW: command not found – subhodeep chatterjee Nov 22 '14 at 05:27
  • "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? – subhodeep chatterjee Nov 22 '14 at 05:43