0

I have the right IP:Port, so I don't know where is the problem, since communicationsexception comes from broken url:port for the mysql database.

package com.bbb.aaa;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

private static final String url = "jdbc:mysql://uk1.khttp.net:2083/thedatabase"; // uk1.khttp.net == 95.154.195.28
private static final String user = "theusername";
private static final String pass = "thepassword";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.out.println("About to try instantiating driver");


TextView tv = (TextView)this.findViewById(R.id.text_view);
try {
    Class.forName("com.mysql.jdbc.Driver");
    System.out.print("success");
    Connection con = DriverManager.getConnection(url, user, pass);
    String result = "Database connection success\n";
    System.out.print("success2");
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM testingregistration");
    System.out.print("success3");
    ResultSetMetaData rsmd = rs.getMetaData();

    while(rs.next()) {
        result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
        result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
        result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
    }
    tv.setText(result);

    tv.setText("Driver instantiated");
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block

    tv.setText(".....Class com.mysql.jdbc.Driver not found!");
    e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        tv.setText(e.toString());
    }

}

}

ERROR LOG: The last packet sent successfuly to the server was 0 milliseconds ago. The driver has not recieved any packets from the server

The console reads and prints "success" but then it crashes after that so it doesnt print "success2" meaning the problem is at connection.

I tried searching the internet for this problem but all they said was that your ip:port and database should be correct but for me this seems correct since I can use that url and port with the login and password details to login to the mysql and then from there I click phpMyAdmin and im in the database.

Thanks in advance

  • Make sure you've got the permission to connect to the Internet in your manifest. – Bartek Jan 07 '13 at 00:37
  • Thanks for reminding me to add the permission. Though I still get the same error and the script bugs after the first "success" is printed out in the console – user1953302 Jan 07 '13 at 00:45
  • Have a look at the suggestions in this answer - http://stackoverflow.com/a/12233178/392781. – Bartek Jan 07 '13 at 21:05
  • possible duplicate of [Connecting to MySQL from Android with JDBC](http://stackoverflow.com/questions/12233145/connecting-to-mysql-from-android-with-jdbc) – Burhan Khalid Jan 09 '13 at 05:45

1 Answers1

1

use AsyncTask. your try catch block code write in doInBackground method of asynctask. try it.

Ankur
  • 5,086
  • 19
  • 37
  • 62
user868927
  • 31
  • 1