Reputation: 45
Assuming I have this Connection String on my local host:
public static Connection ConnectDB(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/supermarket", "root","");
return con;
}
When I am trying to Connect to this system from another. What should I change to access the database?
Say my IP Address on the system is : 192.168.137.1
Upvotes: 1
Views: 1231
Reputation: 610
Your connection is ok. But localhost represents your local machine IP.
You should change following things:
Make db.properties File
connection=jdbc:mysql://192.168.137.1:6036/supermarket
username=root
password=xyz
And Add following code in main method
public static Connection ConnectDB(){
try{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(p.getProperty("connection"),p.getProperty("username"),p.getProperty("password"));
return con;
}
Upvotes: 1
Reputation: 44965
To access a mysql
db remotely, you have 3 things to consider:
Command to execute to grant privileges to your user to access to the db from any remote hosts:
GRANT ALL PRIVILEGES
ON supermarket.*
TO 'root'@'%'
IDENTIFIED BY 'password';
Upvotes: 1
Reputation: 1097
your statement would become
Connection con = DriverManager.getConnection("jdbc:mysql://<IP Address>:<Port>/supermarket", "root","");
Use appropriate IP and Portnumber
Connection con = DriverManager.getConnection("jdbc:mysql://192.168.137.1:6036/supermarket", "root","");
Upvotes: 1