Java JDBC FAQ: Can you share Java JDBC connection string examples for the most popular relational databases?
Some days we all need something simple, and today I needed the example syntax for a JDBC connection string (the JDBC URL) for MySQL and Postgresql databases. While I was digging through my old examples, I found JDBC connection string examples for other databases, and thought I'd share them all here.
To that end, here are some example Java JDBC connection string examples for various databases, including MySQL, Postgres, SQL Server, and DB2.
If you just need one of these Java JDBC connection strings, here's a quick table showing the syntax for JDBC URL's and drivers that I've used on recent projects.
(However, if you'd like to see more detailed JDBC URL and Driver examples for each database, see the sections below.)
| Database | URL (JDBC Connection String) JDBC Driver |
|---|---|
| MySQL | jdbc:mysql://HOST/DATABASE com.mysql.jdbc.Driver |
| Postgresql | jdbc:postgresql://HOST/DATABASE org.postgresql.Driver |
| SQL Server |
jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE (see the Comments section below for more information and changes) |
| DB2 | jdbc:as400://HOST/DATABASE; com.ibm.as400.access.AS400JDBCDriver |
Here's a sample MySQL JDBC connection string and JDBC driver string, taken from a Java properties file:
db_url = jdbc:mysql://HOST/DATABASE db_driver = com.mysql.jdbc.Driver db_username = USERNAME db_password = PASSWORD
Here's a similar MySQL JDBC connection string (URL) and driver inside of a little Java source code:
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://HOST/DATABASE";
conn = DriverManager.getConnection(url, "username", "password");
doTests();
conn.close();
}
catch (ClassNotFoundException ex) {System.err.println(ex.getMessage());}
catch (IllegalAccessException ex) {System.err.println(ex.getMessage());}
catch (InstantiationException ex) {System.err.println(ex.getMessage());}
catch (SQLException ex) {System.err.println(ex.getMessage());}
And while I'm in the neighborhood, here's a MySQL JDBC URL for a MySQL server that is listening on a very non-standard port (5150, in honor of Van Halen):
db_url = jdbc:mysql://HOST:5150/DATABASE db_driver = com.mysql.jdbc.Driver db_username = USERNAME db_password = PASSWORD
Here's a sample Postgresql JDBC connection string and JDBC driver string, taken from a Java properties file:
db_url = jdbc:postgresql://HOST/DATABASE db_driver = org.postgresql.Driver db_username = USERNAME db_password = PASSWORD
And here's the same Postgres JDBC driver and URL (connection string) shown in a snippet of Java source code:
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://HOST/DATABASE";
Connection conn = DriverManager.getConnection(url,"username", "password");
Here's a sample Microsoft SQL Server JDBC connection string and JDBC driver string, taken from a Java properties file:
db_url = jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE db_driver = com.microsoft.jdbc.sqlserver.SQLServerDriver db_username = USERNAME db_password = PASSWORD
And here's a sample JDBC connection string for a DB2 database (and DB2 JDBC driver string), in this case running on an IBM As/400 or iSeries computer. This example was taken from a Spring configuration file (a Spring application context file):
<bean id="as400DataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.ibm.as400.access.AS400JDBCDriver" /> <property name="url" value="jdbc:as400://HOST/DATABASE;" /> <property name="username" value="USERNAME" /> <property name="password" value="PASSWORD" /> </bean>
Shown in a Java properties file format, this DB2 JDBC URL looks like this:
db_url = jdbc:as400://HOST/DATABASE; db_driver = com.ibm.as400.access.AS400JDBCDriver db_username = USERNAME db_password = PASSWORD
MS SQL
The Driver for MS SQL is: "com.microsoft.sqlserver.jdbc.SQLServerDriver"
You need to swap your "jdbc" and "sqlserver" segments in your examples.
MS Driver
For clarity:
com.microsoft.jdbc.sqlserver.sqlserverdriver <--- MS SQL Server 2000 and older
com.microsoft.sqlserver.jdbc.sqlserverdriver <--- MS SQL Server 2005 and newer
MS SQL
Also the URL Changes with MS SQL Server 2005
jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE <--- MS SQL Server 2000
jdbc:sqlserver://HOST:1433;DatabaseName=DATABASE <--- MS SQL Server 2005 and newer
Thanks
Thanks for the SQL Server updates.
Post new comment