0

Trying to connect to a mysql database using JDBC automatic driver loading with syntax that requires the following: jdbc:subProtocolName:databaseURL

I know how to work it with a local host that's providing the mysql server, but I haven't done this remotely, and I want to get it right the first time, rather than coding my application and not being able to set breakpoints in the .xml file etc and confusing the source of other issues I might be having.

So far I have: "jdbc:mysql:[url???]:3306/dbname?autoReconnect=true"

Is it simply the ip address of the server, or is it myusername@[ipAddressofServer], or something else entirely? It doesn't sit with me that it would be name@localhost:[port#] because that would indicate the server resides locally. I've seen things online that say to use the 'server name' here. Is that equivalent to the hostname? Example would be great that doesn't involve mysql running on localhost. Thanks!

Charley Erd
  • 103
  • 1
  • 1
  • 7

1 Answers1

5

(Warning: Here be some slight oversimplifications.)

As you may be aware, computers (directly) connected to the Internet can be identified in two ways:

  1. an "IP address", historically of the form 151.101.1.69 (but may be longer and scarier nowadays), or
  2. a "DNS (Domain Name Service) name", e.g. stackoverflow.com, sometimes called a "host name".

When an application wants to connect to its own computer it can use two special cases of the above. 127.0.0.1 and localhost both mean "this here computer".

So if you want to connect to a remote machine you can use either its DNS name or its IP address as the "server name" in the connection string. For example, if there was a MySQL Server running on the machine in the examples above then your connection string could be

jdbc:mysql://151.101.1.69:3306/databasename?useUnicode=true&characterEncoding=utf8

or

jdbc:mysql://stackoverflow.com:3306/databasename?useUnicode=true&characterEncoding=utf8

Notes:

  • You don't actually need to specify port 3306 because that is the default port for MySQL. If you omit the port number from your connection string the driver will try to use 3306.
  • useUnicode=true&characterEncoding=utf8 are just examples of common attributes that are added to the connection string. There are lots of them, and you can read more about them here.
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418