Making sample JDBC code easier

I’m writing a lot of small JDBC examples for our upcoming practice tests book. I got tired of writing the same code over and over so I wrote this helper class. It can:

  • Drop the table if it already there (because I run the same examples many times)
  • Run a varargs of SQL statements (to create tables and insert data)
  • Spit out the contents of a table (to confirm what happened)


import java.sql.*;
public class DerbyUtil {

    public static void dropTablesIfExist(String url, String... tables) throws SQLException {
        try (var conn = DriverManager.getConnection(url)) {
            for (String table : tables) {
                try (PreparedStatement ps = conn.prepareStatement("DROP TABLE " + table)) {
                    ps.executeUpdate();
                } catch (SQLException e) {
                    // ignore exceptions on drop since table might not exist
                }
            }
        }
    }

    public static void run(String url, String... sqlStatements) throws SQLException {
        try (var conn = DriverManager.getConnection(url)) {
            for (String stmt : sqlStatements) {
                try (PreparedStatement ps = conn.prepareStatement(stmt)) {
                    ps.executeUpdate();
                }
            }
        }
    }

    public static void outputTable(String url, String tableName) throws SQLException {
        String sql = "SELECT * FROM " + tableName;
        try (var conn = DriverManager.getConnection(url);
             var ps = conn.prepareStatement(sql);
             var rs = ps.executeQuery()) {

            var metadata = rs.getMetaData();
            int numColumns = metadata.getColumnCount();

            while (rs.next()) {
                outputRow(rs, numColumns);
            }
        }
    }

    private static void outputRow(ResultSet rs, int numColumns) throws SQLException {
        for (int i = 1; i <= numColumns; i++) {
            System.out.print(rs.getString(i));
            System.out.print('\t');
        }
        System.out.println();
    }
}

Don’t Panic: Oracle announces new Java OCP 11 Exam!

Update (11/05/2020): Read The 1Z0-819 Exam page to learn how you can easily our Java 11 Study Guides to prepare for Oracle’s 1Z0-819 Exam, as well as the 1Z0-817 Upgrade Exam.

Oracle has announced a new Java SE 11 Developer 1Z0-819 exam. In doing so, they are also retiring the 1Z0-815 and 1Z0-816 exams, effective October 1st, 2020. Don’t panic! The new exam is basically a combined version of the 1Z0-815 and 1Z0-816 exams.

In fact, Jeanne and I worked with Oracle staff to help design the objective set for the new 1Z0-819 exam. For example, we’re responsible for removing CallableStatement‘s and assertions. You’re welcome!

This means our existing study guides are perfectly suited to help you pass this new exam. We recommend studying for the new exam with one of the following two options:

Is the new exam easier?

Not really. The new 1Z0-819 exam covers a broad range of material. And you have to know it all on the same day instead of split across two different exams.

What are the benefits of the new 1Z0-819 exam?

To begin with, it costs less. One exam instead of two. Additionally, there are less questions.

The breadth of material does have an advantage: if you don’t do well on one topic, it is likely to be less points on the exam.That said, you only have 90 minutes on the new exam, as opposed to 180 minutes on the old exams, but you have less questions to answer.

What about the Upgrade Exam?

Oracle is keeping the Upgrade OCP Java 11 1Z0-817 exam. Like the new 1Z0-819 exam, our Complete Study Guide can be used for the upgrade exam. You also just use the Programmer II book, as we added an Upgrade Appendix just for this exam.

What don’t I need to study?

You need to know almost everything that was on the 1Z0-815 and 1Z0-816 exams for the 1Z0-819 exam. There are a few topics that you no longer need to worry about for the exam:

  • Characteristics of Java (platform independence, backwards compatibility, etc)
  • Running a basic program from the command line (except modules)
  • Java Assertions
  • JDBC CallableStatement

How do the new objectives map to our Java 11 Study guides?

We created a helpful mapping so you know what to study for the new exam.

1Z0-819 ObjectivesProgrammer I (1Z0-815) ChapterProgrammer II (1Z0-816) ChapterComplete Study Guide Chapter
Working with Java data types3 – Operators
4 – Making Decisions
5 – Core Java APIs
6 – Lambdas and Functional Interfaces
1 – Java Fundamentals3 – Operators
4 – Making Decisions
5 – Core Java APIs
6 – Lambdas and Functional Interfaces
12 – Java Fundamentals
Controlling Program Flow4 – Making Decisions4 – Making Decisions
Java Object-Oriented Approach2 – Java Building Blocks
7 – Methods and Encapsulation
8 – Class Design
9 – Advanced Class Design
1 – Java Fundamentals2 – Java Building Blocks
7 – Methods and Encapsulation
8 – Class Design
9 – Advanced Class Design
12 – Java Fundamentals
Exception Handling10 – Exceptions5 – Exceptions, Assertions, and Localization10 – Exceptions
16 – Exceptions, Assertions, and Localization
Working with Arrays and Collections5 – Core Java APIs3 – Generics and Collections5 – Core Java APIs
14 – Generics and Collections
Working with Streams and Lambda expressions6 – Lambdas and Functional Interfaces4 – Functional Programming
7 – Concurrency
6 – Lambdas and Functional Interfaces
15 – Functional Programming
18 – Concurrency
Java Platform Module System11 – Modules6 – Modular Applications11 – Modules
17 – Modular Applications
Concurrency7 – Concurrency18 – Concurrency
Java I/O API8 – I/O
9 – NIO.2
19 – I/O
20 – NIO.2
Secure Coding in Java SE Application11 – Security22 – Security
Database Applications with JDBC10 – JDBC21 – JDBC
Localization5 – Exceptions, Assertions, and Localization16 – Exceptions, Assertions, and Localization
Annotations2 – Annotations13 – Annotations