Java JDBC – an SQL SELECT query example


In our JDBC connection article we demonstrated how to connect your Java applications to standard SQL databases like MySQL, SQL Server, Oracle, SQLite, and others using JDBC.  In our examples we showed how to connect to two different databases —  Mini SQL (mSQL), and Interbase — just so you can see how the code changes when you switch from one database to another.

In this “JDBC SELECT query” tutorial we’ll take JDBC to the next step — we’ll show you how to create and execute a SQL SELECT statement in your Java code.

JDBC SELECT query – our sample database

Before getting into our JDBC examples, it will help to to know what our database table looks like.  In all of our examples, we’re going to be accessing a database named Demo.  In our JDBC query examples, we’re going to be accessing a database table named Customers, that’s contained in the Demo database.

Here’s what the Customers database table looks like:

Cnum Lname Salutation City Snum
1001 Simpson Mr. Springfield 2001
1002 MacBeal Ms. Boston 2004
1003 Flinstone Mr. Bedrock 2003
1004 Cramden Mr. New York 2001

Table 1: Our sample Customers database table will contain these four sample records.

How to perform a JDBC SELECT query against a database

Today we’ll show you that querying an SQL database with JDBC is often a simple three step process.  The three steps are:

  1. Create a JDBC ResultSet object.
  2. Execute the SQL query (a SELECT query) you want to run.
  3. Read the results.

The hardest part of the process is defining the query you want to run, and then writing the code to read and manipulate the results of your SELECT query.

Creating a valid SQL SELECT query

In today’s example, we’ll create a simple SQL query.  We’ll keep the statement simple, and we’ll just say this:

select Lname from Customers
where Snum = 2001;

This statement returns each Lname (last name) record from our Customers database where Snum(salesperson id-number) equals 2001. In plain English, you might say “give me the last name of every customer where the salesperson id-number is 2001”.

Okay, now that we know the information we want to retrieve, how do we put this SQL statement into a Java program?  It’s actually very simple.  Here’s the JDBC code necessary to create and execute our query:

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");

When we use JDBC, our query results are returned in a Java ResultSet object, so we first create the object rsto hold our results.  This creates the object, but at this point no query has been defined.  In the second statement (executeQuery) we specify the desired SQL query and send it to the SQL database.

Reading the JDBC SELECT query results (i.e., a Java JDBC ResultSet)

After we execute our SQL query, how do we read the results? Fortunately, JDBC makes this pretty easy also.  In many cases, you can just use the next() method of the ResultSet object.  After the previous two lines, you might add a reading loop like this:

while ( {
String lastName = rs.getString("Lname");
System.out.println(lastName + "\n");

This loop reads the last name returned in each record, and prints it to the screen using the normalSystem.out.println() method. In the case of our sample database, the printed results look like this:

Our JDBC SELECT query example program –

The full source code for our example JDBC program ( is shown in Listing 1.

//  Query an mSQL database using JDBC. 

import java.sql.*;

 * A JDBC SELECT (JDBC query) example program.
class Query1 {

    public static void main (String[] args) {
        try {
            String url = "jdbc:msql://";
            Connection conn = DriverManager.getConnection(url,"","");
            Statement stmt = conn.createStatement();
            ResultSet rs;

            rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
            while ( ) {
                String lastName = rs.getString("Lname");
        } catch (Exception e) {
            System.err.println("Got an exception! ");

The source code for the program shows how to query an SQL database for the information you want, using Java JDBC methods.

Download our example JDBC select program

If you’re interested, you can download the Java source code for our program.  You can test this JDBC example code on your own system, but note that you’ll need to change the lines where we create oururl and conn objects to reflect your own database configuration.


Querying an SQL database with JDBC is a simple three step process, once you know how to do it.  Just (1) create a ResultSet object, (2) execute the query, and then (3) read the results.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: