JDBC Connection Strings for Popular RDBMS

1. Introduction

In Java applications, JDBC (Java DataBase Connectivity) API is used for connecting to relational databases from clients. The API uses Drivers which are nothing but Java library files exposed by different database vendors and Connection Strings/URLs to connect to the databases. Of course, there are other parameters that could be specified like username, password, the database schema to be used etc.

In this article, we are presenting a consolidation of the different Connection Strings that are to be used for connecting to some of the most popular RDBMS (Relational DataBase Management Systems). We have also included information on the drivers exposed by these database vendors but the version of these drivers might be different based on the version of the database used. Also kindly note that the parameters specified within brackets (< >) are optional and should be substituted with the values of the computer hosting the database. In most databases, if not specified the default value is picked up. We begin by an example code snippet on how a connection could be established with an RDBMS using JDBC and then follow it up with a table listing the Connection Strings of some of the popular databases. A short example project on making a connection to the database and reading some data has also been provided at the end of the article which is available for download.

Want to be a JDBC Master ?

Subscribe to our newsletter and download the JDBC Ultimate Guide right now!

In order to help you master database programming with JDBC, we have compiled a kick-ass guide with all the major JDBC features and use cases! Besides studying them online you may download the eBook in PDF format!

2. Connecting to an RDBMS

The following code snippet illustrates how the connection parameters are used to retrieve a database connection.

try{
    Class.forName("com.mysql.jdbc.Driver").newInstance(); //using MySQL JDBC Driver
    String url = "jdbc:mysql://hostname:port/database"; //format of MySQL connection string
    conn = DriverManager.getConnection(url, "username", "password");  
    
} catch (Exception e){...}
finally {
    conn.close();
}

3. JDBC Connection Strings

The following table lists the Connection Strings of some of the popular RDBMS.

DataBase Type DriverClassName Connection String
MySQL com.mysql.jdbc.Driver jdbc:mysql://<hostname>:<portNumber>/<databaseName>

Default host: 127.0.0.1
Port# 3306

PostgreSql org.postgresql.Driver jdbc:postgresql://<hostname>:<portNumber>/<databaseName>

Default host: 127.0.0.1
Port# 5432

SQLServer com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc:sqlserver://<hostname>:<portNumber>;<databaseName>

Default host: 127.0.0.1
Port# 1433

Oracle oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@<hostname>:<portNumber>:<databaseName>

Default host: 127.0.0.1
Port# 1521

DB2 COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver
(DB2 JDBC Type 4 Driver)
jdbc:db2://<hostname>:<portNumber>/<databaseName>

Default host: 127.0.0.1
Port# 50000

DB2
(on As/400)
com.ibm.as400.access.AS400JDBCDriver jdbc:as400://<hostname>:<portNumber>/<databaseName>;
Sybase com.sybase.jdbc.SybDriver jdbc:Sybase:Tds:<hostname>:<portNumber>/<databaseName>
Sybase net.sourceforge.jtds.jdbc.Driver
(jTDS driver)
jdbc:jtds:Sybase://<hostname>:<portNumber>/<databaseName>
Sybase com.sybase.jdbc2.jdbc.SybDriver
(jdbc2 driver version)
jdbc:Sybase:Tds:<hostname>:<port>?ServiceName=<databaseName>

4. Conclusion

This concludes our article. Hope it serves as a useful quick reference of the Connection Strings to be used with different relational databases.

5. Download the Eclipse project

This is an example of a simple Eclipse project that shows connecting to a MySQL database using JDBC.

Download
You can download the full source code of this example here : JdbcConnectionExample
Exit mobile version