sql

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.

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 TypeDriverClassNameConnection String
MySQLcom.mysql.jdbc.Driverjdbc:mysql://<hostname>:<portNumber>/<databaseName>Default host: 127.0.0.1
Port# 3306
PostgreSqlorg.postgresql.Driverjdbc:postgresql://<hostname>:<portNumber>/<databaseName>Default host: 127.0.0.1
Port# 5432
SQLServercom.microsoft.sqlserver.jdbc.SQLServerDriverjdbc:sqlserver://<hostname>:<portNumber>;<databaseName>Default host: 127.0.0.1
Port# 1433
Oracleoracle.jdbc.driver.OracleDriverjdbc:oracle:thin:@<hostname>:<portNumber>:<databaseName>Default host: 127.0.0.1
Port# 1521
DB2COM.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.AS400JDBCDriverjdbc:as400://<hostname>:<portNumber>/<databaseName>;
Sybasecom.sybase.jdbc.SybDriverjdbc:Sybase:Tds:<hostname>:<portNumber>/<databaseName>
Sybasenet.sourceforge.jtds.jdbc.Driver
(jTDS driver)
jdbc:jtds:Sybase://<hostname>:<portNumber>/<databaseName>
Sybasecom.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

Joormana Brahma

She has done her graduation in Computer Science and Technology from Guwahati, Assam. She is currently working in a small IT Company as a Software Engineer in Hyderabad, India. She is a member of the Architecture team that is involved in development and quite a bit of R&D. She considers learning and sharing what has been learnt to be a rewarding experience.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button