JDBC Back to Basics: Part 1: Connecting
In this JDBC Basics series we'll go through the basic steps needed to get a knowledge of JDBC. Following this series we'll go through more advanced features of JDBC drivers, such as advanced security (Kerberos, etc...), performance tuning, etc.. For completeness, we'll show code examples for SQL Server, Oracle, DB2, Sybase, Informix, and MySQL as we know everyone in the world doesn't use the same database to store data.
The following posts are currently available in this series. This list will be updated as posts are written and will be contained at the top of each post in the series.
- Connecting (this post)
So, you want to write a Java application to get some data - you've come to the right place! Getting connected and fetching your data is easy using the JDBC Standard API that comes in the JDK. Let's jump right in by throwing out some code for connecting to SQL Server:
Class.forName("com.ddtek.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:datadirect:sqlserver://nc-pgm1:1433;databaseName=master";
Connection con = DriverManager.getConnection(url, "user1", "pass1");
There we go, easy enough. The first line which has the Class.forName is not necessary if you are using a driver that supports the JDBC 4.0 autoloading feature and Java SE 6. Therefore, you could remove this line altogether leaving you with 2 lines to connect to your SQL Server database. The URL is easy to deciper as well - he first part is which driver you want to use "jdbc:datadirect:sqlserver", followed by the server machine name (or IP address), port number, and database that you want to connect to. Lastly, you get the actual connection object passing in the url, user, and password. We'll go over Kerberos, Windows Auth and other security specifics in another post.
Ok, so what do you do after connecting? In my demo application, I like to print out some server information from the DatabaseMetaData object (helps to make sure I'm using what I think I am). Here's the code:
DatabaseMetaData dbmd = con.getMetaData();
System.out.println( "\nConnected with " + dbmd.getDriverName()
+ " " + dbmd.getDriverVersion()
+ " to " + dbmd.getDatabaseProductName()
+ " " + dbmd.getDatabaseProductVersion() + "\n");
This gives us a nice line that shows what you got connected to and with what driver version. Helps a ton when you're checking your output or have to send the output to someone else to look at.
Well, that's it for connecting - pretty straightforward. We'll continue next time with a different driver and start creating and working with tables!


Performance is a huge concern when writing any Java code, especially since many still do not realize the benefits of coding to a hotspot vm and believe Java to be a "slower" language than something like C/C++. As someone with a hardware background, I frequently hear that faster computers are the answer. To an extent, they are correct. To this end, processor architects have been putting more processing power in our beloved machines by adding nifty new technology like hyper-threading, cores, and increasing the on-die caches in recent years. All this is great; however, we can tap this power to a greater extent if we software guys have ways to program closer to this type of hardware.



