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.