0

I have a simple database on my computer for testing purposed, and I'm trying to retrieve some information from the database, from my laptop. So I want my laptop to make a request to see the information inside my computers MySQL database. Below shows the java code I'm trying to run on my laptop to collect the first entry in the students table, which is located on my computer.

I have MySQL workbench installed on both my laptop and computer, is it necessary to be on both machines if the computer will store the data and the laptop only extracts data.

What I've learnt so far from researching is that the public ip should be used in the url instead of the ip for the computer, so I added that in but I received a CommunicationsException along with "Connection timed out" in the stack trace. I've read through this answer and this answer to a similar problem, but I'm having difficulty understanding both solutions, could someone refer me to a beginners guide to remotely accessing data from a database using MySQL.

public class TestRemote{
    //JDBC variables
    Connection connection;
    Statement statement;
    ResultSet resultSet;

    //String variables
    String url;
    String user;
    String password;

    public static void main(String[] args) {
        TestRemote sql = new TestRemote();
        ArrayList<String> firstnames = sql.getColumn("students", "firstname", "studentid=4");

        System.out.println(firstnames.get(0));
    }

    // Constructor
    public TestRemote()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("couldnt find class");
        }
        url = "jdbc:mysql://81.159.3.167:3306/test";           //?autoReconnect=true&useSSL=false";
        user = "user";
        password = "pass123";

        connection = null;
        statement = null;
        resultSet = null;      
   }

    private void closeConnection(){
        try{
           if(connection != null)
               connection.close();
           if(statement != null)
               statement.close();
           if(resultSet != null)
               resultSet.close();

           connection=null; resultSet=null; statement=null;
        }catch(Exception e){
               e.printStackTrace();
        }
     }

     public ArrayList<String> getColumn(String table, String column, String where) {
         ArrayList<String> resultsArray = new ArrayList<String>();
         try {
             connection = DriverManager.getConnection(url, user, password);
             statement = connection.createStatement();
             if(!where.equals(""))
                 resultSet = statement.executeQuery("SELECT "+column+" FROM "+table + " WHERE "+where);
             else
                 resultSet = statement.executeQuery("SELECT "+column+" FROM "+table);

             while(resultSet.next()) {
                 String val = resultSet.getString(1);
                 if(val==null)
                     resultsArray.add("");
                 else
                     resultsArray.add(val);
             }
             //resultsArray = (ArrayList<String>) resultSet.getArray(column);
         } catch (SQLException ex) {
             Logger lgr = Logger.getLogger(Model.class.getName());
             lgr.log(Level.SEVERE, ex.getMessage(), ex);
         }
         closeConnection();

         return resultsArray;
     }
}
Community
  • 1
  • 1
user217339
  • 169
  • 1
  • 2
  • 11
  • 2
    *MySQL workbench installed of both laptops* - you need MySQL database to connect, not workbench. Also, only on one is fine. Second can access it over the same network using it's IP address. – Gurwinder Singh Jan 03 '17 at 17:36

2 Answers2

2

I have MySQL workbench installed on both my laptop and computer, is it necessary to be on both machines if the computer will store the data and the laptop only extracts data.

No what you call the "Computer" is your server here. it doesn't need mysql workbench. it only needs mysql server

the public ip should be used in the url instead of the ip for the computer

A database should almost never be exposed on the public IP address. If you are having both computers on the LAN, the private network IP is what the server should listen on and that's what you should use on the connection string.

CommunicationsException along with "Connection timed out" in the stack trace

Because the server is not running, not listening on that ip:port or firewalled to drop packets.

e4c5
  • 52,766
  • 11
  • 101
  • 134
2

Your Java code is probably fine. But the question is, is on the other machine a MySQL server running and listening on port 3306 on the public IP? By default it should only listen on localhost, so you need to change your MySQL installation so that it listens to the public IP. Also make sure that no Firewall is blocking the access. Try connecting with the Workbench on the Laptop to reach the MySQL server on the other box. If you got this running, try your Java code again.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • can you direct me to a tutorial which suggests which setting to use to allow the laptop to query the database on my server – user217339 Jan 04 '17 at 01:57
  • In `/etc/mysql/my.cnf` set `bind-address` from `127.0.0.1` to the IP it should listen on, or to `0.0.0.0` if you want it to listen on all interfaces including `127.0.0.1`. And don't forget to comment it in if it is commented out with a `#` sign. – Vampire Jan 04 '17 at 09:12
  • I can't seem to find the directory etc in the MySQL folder I'm running on windows would it be located somewhere else? – user217339 Jan 04 '17 at 10:52
  • nevermind, I found the file through Server -> Options file, through the menu bar – user217339 Jan 04 '17 at 11:01