2

I have allowed access to phpMyAdmin on first PC. I can open phpMyAdmin by opening in browser http://192.168.100.10:8080/phpmyadmin on another PC in local LAN. And also I can open via browser DB in phpMyAdmin on another PC

But I can't create connection to the DB via java code:

Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
Properties info = new Properties();
info.put("characterEncoding", "UTF-8");
info.put("user", "root");
info.put("password", "");
info.put("autoReconnect", "true");
info.put("useSSL", "false");
info.put("failOverReadOnly", "false");
info.put("maxReconnects", "10");
info.put("connectTimeout", "2000");

DriverManager.setLoginTimeout(10);
mConnection = DriverManager.getConnection(
    "jdbc:mysql://192.168.100.10:8080/flats_flx", info
);//here it's stuck 

mStatement = mConnection.createStatement();

Here https://stackoverflow.com/a/27700425/2425851 I found that I need to "Allow Network Access to MySQL" How to do that in phpMyAdmin?

Community
  • 1
  • 1
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138

2 Answers2

2

Try to use this way check your port number i think MySql is 3306:

public class CreateConnection {
    String driver = "com.mysql.jdbc.Driver";
    String DB_username = "username";
    String DB_password = "password";
    String DB_URL = "jdbc:mysql://192.168.100.10:3306/flats_flx";

    public Connection getConnection() {
        try {
            Class.forName(driver);
            java.sql.Connection con = DriverManager.getConnection(DB_URL, DB_username, DB_password);
            System.out.println(con);
            return con;

        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Exception " + e);
            return null;
        }
    }
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2
Class.forName(driver);
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://ip_of_host_server:3306/Database_name", DB_username, DB_password);
Gaurav Varshney
  • 502
  • 5
  • 15