In the first tutorial of the series, we will show you how you can use Progress DataDirect JDBC drivers in your Java application to connect to your database.
JDBC drivers are the most conventional way that many developers use to connect to databases from their Java applications. These drivers, once primarily available for relational databases, are now available for almost any type of data source such as Big Data, No SQL and SaaS.
Even with all these different types of data sources, it’s still easy to connect to and work with them using Progress DataDirect JDBC drivers. To demonstrate this, we have a planned a “JDBC—Revisited” tutorial series of three parts, which covers various concepts from creating a connection, executing simple queries, extracting metadata and DB interoperability features of DataDirect drivers.
In the first tutorial of the series, we will show you how you can use Progress DataDirect JDBC drivers in your Java application to connect to your database. The main objectives of this tutorial are to demonstrate:
For the purpose of this tutorial, we will be using a relational database (Postgres) and SaaS data source (Salesforce) as our data sources. The goal is to demonstrate the simplicity of our Progress DataDirect JDBC drivers for Postgres and Salesforce for connecting and retrieving the data, irrespective of type of the data source.
We expect you have the following setup before proceeding further with this tutorial.
If you're interested in connecting to just one data source, then you only need to set up one of these.
create database dvdrentals;
With everything setup for you to connect to database, let’s start the fun by writing code to connect to your data source.
public class JDBCUtil {
String className, URL, user, password;
Connection connection;
public JDBCUtil(String className, String URL, String user, String password) {
this.className = className;
this.URL = URL;
this.user = user;
this.password = password;
this.connection = null;
}
public void getConnection() {
//Load the driver class
try {
Class.forName(className);
} catch (ClassNotFoundException ex) {
System.out.println("Unable to load the class. Terminating the program");
System.exit(-1);
}
//get the connection
try {
connection = DriverManager.getConnection(URL, user, password);
} catch (SQLException ex) {
System.out.println("Error getting connection: " + ex.getMessage());
System.exit(-1);
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
System.exit(-1);
}
}
}
public void executeQuery(String query)
{
ResultSet resultSet = null;
try
{
//executing query
Statement stmt = connection.createStatement();
resultSet = stmt.executeQuery(query);
//Get Number of columns
ResultSetMetaData metaData = resultSet.getMetaData();
int columnsNumber = metaData.getColumnCount();
//Printing the results
while(resultSet.next())
{
for(int i = 1; i <= columnsNumber; i++)
{
System.out.printf("%-25s", (resultSet.getObject(i) != null)?resultSet.getObject(i).toString(): null );
}
}
}
catch (SQLException ex)
{
System.out.println("Exception while executing statement. Terminating program... " + ex.getMessage());
}
catch (Exception ex)
{
System.out.println("General exception while executing query. Terminating the program..." + ex.getMessage());
}
}
I hope this tutorial helped you understand how easy it is to connect to your database and execute simple SQL queries in Java using Progress DataDirect JDBC drivers. Also, I have pushed the code that’s used in this tutorial to the GitHub for your reference. If you still have any issues connecting to your database using Progress DataDirect JDBC drivers, leave your comments below or contact support.
In the next part of this "JDBC—Revisited" tutorial series, we will show you how to get the metadata from your databases using JDBC drivers. Subscribe to our blog via Email or RSS feed for updates to this tutorial series.
Try Free For 15 Days
Saikrishna is a DataDirect Developer Evangelist at Progress. Prior to working at Progress, he worked as Software Engineer for 3 years after getting his undergraduate degree, and recently graduated from NC State University with Masters in Computer Science. His interests are in the areas of Data Connectivity, SaaS and Mobile App Development.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.