0

I have always used a database located in my pc, so the steps to connect to it are:

  • connect to my local db using jdbc
  • perform queries
  • show results

now, i want to know what are the steps if the database is located in an online server?

some guides,tutorials are very appreciated.

OiRc
  • 1,602
  • 4
  • 21
  • 60
  • The steps are the same. – Dragondraikk May 28 '15 at 07:57
  • well, i have to define a server-client comunication or something else? – OiRc May 28 '15 at 07:58
  • 2
    The url has to be accessed via internet, with an ip or host name. Nothing else changes – aksappy May 28 '15 at 07:59
  • 2
    if your DB on the server is listening, then all you have to do is give jdbc your server's IP and the SQL port and login info. – Dragondraikk May 28 '15 at 07:59
  • You connection String will change with the IP of the Online Server where db is and the port no on which it is running ! – Neeraj Jain May 28 '15 at 08:00
  • 2
    Preferably, you would have a service layer between you and your database (like a web service) which would "hide" you database from unauthorized tampering – MadProgrammer May 28 '15 at 08:01
  • @Dragondraikk so, an http connection, and then perfom the query? – OiRc May 28 '15 at 08:03
  • @MadProgrammer can u be more specific? – OiRc May 28 '15 at 08:03
  • @OiRc you only need to adjust your jdbc connection string to point to the server's DB instead of localhost. Most jdbc tutorials should show you how if you don't know what I mean. – Dragondraikk May 28 '15 at 08:05
  • 2
    @OiRc This depends on you intended deployment, but it is generally considered a security risk to allow access to your database from unknown IP's addresses. Typically, you would limit the number of IP's which could access the database remotely and hide it behind a service layer, like a web server/service, which would act as a gate keeper and control access to and from the database. – MadProgrammer May 28 '15 at 08:07
  • 1
    possible duplicate of [Connect Java to a MySQL database](http://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database) – Jamie Bull May 28 '15 at 08:12

2 Answers2

1

You have to change database url only to be like jdbc:mysql://IP:3306/databasename

also username and password.

some hosting company like godaddy create database in localhost(in the same host) which doesn't need to change anything in your code.

Bashar Abutarieh
  • 854
  • 1
  • 8
  • 18
1

Since you will be connecting to a remote database I would advise you to read about secure connections using JDBC. See this question, for example. You don't want to interact with a remote database without something like SSL to protect the confidentiality of the data.
Once you think you have secured your connection, you can use a tool like Wireshark to make sure that the packets that are going to and coming from your database are in fact opaque.

Furthermore, as others have said, not much changes if you already have a working connection to a local database, it's a matter of changing your URL from jdbc:mysql://localhost:port/database to jdbc:mysql://ipaddress:port/database.
From my experience, I found that some hosting companies block database access from unknown IP addresses, so it's possible that you'll have to go to your CPanel and whitelist your IP address.

Once your database connection is setup, the code you use to query the database should look the same.

Some useful links:
JDBC Best practices
JDBC Tutorials from Oracle

Community
  • 1
  • 1
goncalotomas
  • 1,000
  • 1
  • 12
  • 29