1

I want to write a java application that uses a mysql database to store and retrieve information. I am still just a beginner and I do not have a lot of knowledge on web hosting providers and server architecture. In this application, several clients will have to access this remote database located on a server machine maybe.

Currently, I can connect to the database from the same computer that runs the mysql server (I am using workbench by the way). Any suggestions on what should I do?

public static Connection openDatabase()
{ 
    Connection myConn;
    try {

      myConn = 
            DriverManager.getConnection(Configs.getProperty("DatabaseURL"));

     return myConn;
   } catch (SQLException e) {

      e.printStackTrace();
   }

   return null;
}

The url is:

jdbc:mysql://Atomic-PC:3306/test?autoReconnect=true&rewriteBatchedStatements=true&useSSL=false&user=root&password=password
2
  • Add your db connecting code, so we can provide you in depth answer Commented Dec 27, 2017 at 3:26
  • @ChathuraBuddhika, I had added my code so far Commented Dec 31, 2017 at 17:47

3 Answers 3

1

First of all you have to find connection URL, port and credential from MySql workbench you are using. If hosting provider provides a public IP for DB server you should be able to access it from anywhere by using following code in Java.

import java.sql.Connection;
import java.sql.DriverManager;

public class Main {
  public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String connection = "jdbc:mysql://localhost:3306/YourDBName";
    String user = "root";
    String password = "root";
    Class.forName(driver);
    Connection con = DriverManager.getConnection(connection, user, password);
    if (!con.isClosed()) {
      con.close();
    }
  }
}

For this reason you have to import mysql connection driver to your application first.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help! Do you know how can I get a hostname that is visible to clients in other networks?
You can use Query SELECT USER(); to get username and hostname. It will return like username@hostname
0

If you are trying to connect to remote database then yo need to change the database url from localhost to remote server ip address.

jdbc:mysql://Atomic-PC:3306/test

to

jdbc:mysql://<db-server-ip-address>:<db-server-port>/<db-name>

Assuming, remote server ip address is 10.234.05.123 and database port number is 3300 and database name is remoteDB. Then,

jdbc:mysql://10.234.05.123:3300/remoteDB

2 Comments

Hello, thanks for the help. I am using mysql as my rdbms. I manage my database manually through mysql workbench on my local machine. How can I get the ip address of my db? Would I be able to access the db from another network?
@MariaGonzalez You need ip-address of server where you have installed mysql database.
0

your web hosting company should be able to provide you with the url:port which you can use in your connection string to connect to MySQL(or any other remote db for that matter)

2 Comments

What web hosting companies would you recommend? Which plan should I get?
There are sooo many of them..just do a google search for best web hosting companies..All you need to make sure they provide you with MySQL server to connect to.

Your Answer

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.