Enterprise Java

JDBC Connections Cheat Sheet

Abstract

This is a quick reference for JDBC connections for common databases. I seem to have to lookup this information a lot, so I figured it be good to have a reference all in one place.
 
 
 
 
 

Derby

01
02
03
04
05
06
07
08
09
10
11
12
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.11.1.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.11.1.1</version>
    <scope>test</scope>
</dependency>

Embedded (in-memory)

1
2
3
4
5
6
7
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
 
String connectionUrl
    = "jdbc:derby:C:/My Databases/Derby/Test;user=;password=;create=true";
 
Connection conn
    = DriverManager.getConnection(connectionUrl);

Remote

01
02
03
04
05
06
07
08
09
10
Class.forName("org.apache.derby.jdbc.ClientDriver");
 
String connectionUrl
    = "jdbc:derby://localhost:1527/widget";
 
String user = "sa";
String pass = "sa";
 
Connection conn
    = DriverManager.getConnection(connectionUrl, user, pass);

PostgeSQL

1
2
3
4
5
6
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.1.4.jre7</version>
    <scope>test</scope>
</dependency>
01
02
03
04
05
06
07
08
09
10
Class.forName("org.postgresql.Driver");
 
String connectionUrl
    = "jdbc:postgresql://localhost:5432/widget";
 
String user = "widgetapp";
String pass = "widgetapp";
 
Connection conn
    = DriverManager.getConnection(connectionUrl, user, pass);

Oracle

Download JDBC drivers from http://www.oracle.com/technetwork/database/features/jdbc/index.html

1
2
3
4
5
6
7
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.4</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/ojdbc6.jar</systemPath>
</dependency>
01
02
03
04
05
06
07
08
09
10
11
12
13
Class.forName("oracle.jdbc.driver.OracleDriver");
 
String SID
    = "xe";
 
String connectionUrl
    = "jdbc:oracle:thin:@localhost:1521:" + SID;
 
String user = "hr";
String pass = "hr";
 
Connection conn
    = DriverManager.getConnection(connectionUrl, user, pass);

Summary

That’s it…enjoy!

Published on Java Code Geeks with permission by Michael Remijan, partner at our JCG program. See the original article here: JDBC Connections Cheat Sheet

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Michael Remijan

Michael Remijan is a System Architect at the Federal Reserve Bank St. Louis. He is co-author of 'EJB 3 In Action Second', an active blogger in the Java EE community, a Java EE Guardian, and JavaOne presenter. He has developed enterprise systems for B2C and B2B commerce, manufacturing, astronomy, agriculture, telecommunications, national defense, healthcare, and financial areas.
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