0

In Mysql table I have the database:-

mysql> use student
mysql> select * from ari;
+----+-------+
| id | name  |
+----+-------+
| 22 | Sandy |
+----+-------+

I have installed jetty and mysql in my computer the ports are:--

http://localhost(172.0.0.1):8080/
http://localhost(172.0.0.1):3306/

This is my java code:--(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 the 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"
        />
</RelativeLayout>

In my project I also add mysql-connector-java-5.1.34.jar

But When I run it shows the error in my android device(phone):--

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

can anyone tell me where is the problem????????????????

2 Answers 2

0

If you are running your app in your phone, obviously it needs to connect to your PC's ip address. Not localhost. And your PC and your phone needs to be in the same network.

E.g. jdbc:mysql://your-pc-ip-address:3306/student

1
  • Try connect to your PC's LAN address 192.168.1.XXX:3306
    – Calvin
    Nov 24, 2014 at 6:30
0

You can check 2 points.

1. DB running PC and Your phone are same network?

2. If 'root' user has not '%' permissions, you should add this permissions.

create user 'root'@'@' identified by 'root';
8
  • I am using wifi in my mobile,So this is not the same network.I understand the problem.. But when I use emulator to run this..same error is occuring.. Nov 24, 2014 at 6:37
  • As just easy ways, Smartphone and Your PC connected same wi-fi.
    – Cary
    Nov 24, 2014 at 7:15
  • But, I am not recommendate this way. So add permission to 'root' user.
    – Cary
    Nov 24, 2014 at 7:17
  • create user 'root'@'@' identified by 'root'; ERROR 1105 (HY000): Malformed hostname (illegal symbol: '@') Nov 24, 2014 at 8:22
  • "create user 'root'@'@' identified by 'root';" are you sure that this command is correct? Nov 24, 2014 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.