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 JDBC Driver, 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.
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:
String url = "jdbc:datadirect:sqlserver://nc-pgm1:1433;databaseName=master";
Connection con = DriverManager.getConnection(url, "user1", "pass1");[/cc]
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:
[cc lang="java"]DatabaseMetaData dbmd = con.getMetaData();
System.out.println( "nConnected with " + dbmd.getDriverName()
+ " " + dbmd.getDriverVersion()
+ " to " + dbmd.getDatabaseProductName()
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!
As Senior Director of Research & Development, Jesse is responsible for the daily operations, product development initiatives and forward looking research for Progress DataDirect. Jesse has spent nearly 20 years creating enterprise data products and has served as an expert on several industry standards including JDBC, J2EE, DRDA and OData. Jesse holds a bachelor of science degree in Computer Engineering from North Carolina State university.
Copyright © 2018 Progress Software Corporation and/or its subsidiaries or affiliates.
All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.