0

First up, I apologise if this is a duplicate in any way, but I have been trying many different things from this site for several hours now with no luck. And for the record, I am running OS X 10.11.5.

I have made this simple application using JDBC to connect to a database I created that is stored on my localhost (I am using phpMyAdmin, if that is any help):

@Override
public void actionPerformed(ActionEvent e) {

// The ID to search for
int search = Integer.parseInt(textField.getText());
// Setting up the connection to the database.
    String url = "jdbc:mysql://my_ip_address:3306/javaDatabase";
    String user = "root"; // root user
    String password = ""; // no password
    Connection con = null; 
    PreparedStatement searchID; // I used a prepared statement so I could include user input
    ResultSet result = null; // results after SQL execution

    try {
        con = DriverManager.getConnection(url, user, password); // connect to MySQL
        // Creating the prepared statement
        searchID = con.prepareStatement("SELECT * FROM Names WHERE ID = ?");
        // Setting the parameter to the user input
        searchID.setInt(1, search);
        result = searchID.executeQuery(); // execute the SQL query

        while (result.next()) { // loop until the end of the results

            String ID = result.getString("ID");
            String FirstName = result.getString("FirstName");
            String LastName = result.getString("LastName");

            textArea1.setText("ID: " + ID + "\n" + 
            "First Name: " + FirstName + "\n" +
            "Last Name: " + LastName + "\n");
        }
    } catch(SQLException e1) {
        System.out.println("Exception caught " + e1.getMessage());
    } finally {
        try {
            if (result != null) {
                result.close();
            }

            if (con != null) {
                con.close();
            }
        } catch(SQLException e2) {
            System.out.println("SQLException caught " + e2.getMessage());
        }
    }
} 

public static void main(String[] args) {

    JavaWithSQL newJava = new JavaWithSQL();

}

Now, I am packaging this application up as an executable .JAR file, and want to be able to run it on someone else's computer and have it access the database and return the records.

I have tried instructions from here and here, without any luck. I looked at opening port 3306 on my Mac, but my firewall is off, but that doesn't seem to be the problem. I have also attempted to use GRANT privileges on the database in question using:

GRANT ALL PRIVILEGES ON javaDatabase.* TO '%'@'%' IDENTIFIED BY '';

to no avail. However, it does work on other computers when I explicitly write the computers IP address, like this:

GRANT ALL PRIVILEGES ON javaDatabase.* TO 'root'@'other_computer_ip' IDENTIFIED BY '';

But I need to be able to run it on my lecturer's computer, and in theory, other people's computers, without having to know everyone's IP addresses.

How can I do this? Am I missing something?

EDIT:

Okay, I have run the command

GRANT SELECT ON javaDatabase.* TO 'remoteUser'@'%' IDENTIFIED BY '';
FLUSH PRIVILEGES;

And now it works perfectly on any computer connected to the same network, but I need it to work even if I am connected to a different network.

I really need a pointer on this one.

1 Answer 1

0

You are using TO '%'@'%' IDENTIFIED BY '';

I'm not sure if that works. It should address the root user in your case.

GRANT ALL PRIVILEGES ON javaDatabase.* TO 'root'@'%' IDENTIFIED BY '';

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

7 Comments

I have also tried that. I put a blank in the IDENTIFIED BY because the root has no password currently.
I didn't meant the password. Its the username part that confuses me... '%'@'%' doesn't sound right to me
Your second query uses root as username .. That why it's working in that case.
Hmm. That's a query I saw somewhere else on SO and tried. I'll give the second query a go...
Aha! It worked, thank you! I used your last query, and restarted the server, and it works on two different computers in my house, I even changed the IP address on one, just to check. Big thanks!
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.