1

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

asked Oct 19, 2016 at 16:25
3
  • replace "localhost" with that ip address. Commented Oct 19, 2016 at 16:27
  • Start by replacing localhost to the IP Address. I think you'll have to provide the port number as well. Something like x.x.x.x:yyyy Commented Oct 19, 2016 at 16:30
  • your statement would become Connection con = DriverManager.getConnection("jdbc:mysql://192.168.137.1:6036/supermarket", "root",""); Use appropriate IP and Portnumber Commented Oct 19, 2016 at 16:33

3 Answers 3

1

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",""); 
answered Oct 19, 2016 at 16:34
Sign up to request clarification or add additional context in comments.

Comments

1

To access a mysql db remotely, you have 3 things to consider:

  1. Provide the right ip address and port of your remote server
  2. Make sure that the firewall of your server is properly configured to allow remote accesses on your server port
  3. Grant the required privileges to your user to allow remote accesses

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';
answered Oct 19, 2016 at 16:43

Comments

1

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;
 }
answered Oct 19, 2016 at 16:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.