简体   繁体   中英

What should be my connection URL for my database on localhost and website developed using Java working online?

I have my website https://www.riyajobcard.herokuapp.com working on an online server. I want this website to be online and its database to be working on localhost of my computer. Is this possible?

My connectionDB.java file's code look like this:

package modal;
import java.sql.*;

public class ConnectionDb {

         public static Connection getConnection(){  
                Connection con=null;  
                try{  
                    Class.forName("com.mysql.jdbc.Driver");  
                    con= DriverManager.getConnection("jdbc:mysql://localhost:3306/jcps","root","");                
                }catch(Exception e){System.out.println(e);}  
                return con; 
         }  
}

I expect that the connection of a live website to a local database should be done successfully.

When you define a connection with localhost for an app, it would resolve to the server on which the app is hosted. It is not advised to run an app on a different server with the database on your local computer, since it would lose the connection when your computer is either shutdown or goes offline.

But even if you must do it, you would need to provide the IP of your computer, and allow access via the firewall with the necessary port open for the app running on a different server to access your database. So essentially, you should replace localhost with the public IP address of your computer on which the database is running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM