OCP Book Now Available!

OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide: Exam 1Z1-809

Jeanne and I are thrilled to announce that the Kindle version of our second book, OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide: Exam 1Z0-809, is now available for purchase on Amazon! The OCP paperback edition, available for pre-order, is currently printing and will be shipping in the next month.

This book, which is nearly twice the size of our previous OCA Study Guide, is chock full of information on topics ranging from design patterns, to concurrency and NIO.2, to JDBC. Unlike many other older study guides, we have written our material centered entirely around Java 8, with an emphasis on lambda expressions and streams.

While some experienced developers can pass Oracle’s OCA exam with limited amount of studying, the same cannot be said for the OCP exam. Besides the large-scale inclusion of lambda expressions and streams, the exam covers includes much broader topics that seasoned developers may not be readily familiar with.

For example, you might have been using JDBC for years, but can you list the differences between obtaining a connection using the 3.0 and 4.0 versions of the drivers? You may have also been reading and writing files with java.io streams for years, but we bet many of you haven’t ever used the mark(), reset(), or skip() methods. It is for reasons like these that we strongly recommend you study carefully for the exam using our book or an equivalent study guide, avoiding the pitfalls (mentally and financially) of having to pay to take the exam multiple times.

Our book covers all 3 variations of the OCP exam, including a dedicated Appendix for those taking the Java 6 or earlier version of the OCP 8 upgrade exam, as it contains material not found in the other two versions of the exams.

We hope you enjoy reading this book as much as we enjoyed writing it!

OCP 8 Book Bonus: Creating a Derby Database in Java 8

While it is certainly possible to get all the JDBC questions on the exam without running any code or understanding any SQL, it is nice to be able to follow along. This blog post is meant to help anyone who has purchased our book, OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide: Exam 1Z0-809, download and run through the examples in the text. It also includes the database setup code so you can simply copy/paste it. The actual book covers what you need to know for the exam.


This blog post assumes you are reading chapter 10 of our OCP 8 book and have gotten up to the part that references this blog post.


Creating your initial database

Apache Derby is an open source database. It is really easy to use and comes with JDK 8. This means you don’t have to install anything special. You can even create and setup the database completely in Java. To start out, copy this code into a file named SetupDerbyDatabase.java.

import java.sql.*;

public class SetupDerbyDatabase {

   public static void main(String[] args) throws Exception {
      String url = "jdbc:derby:zoo;create=true";
      try (Connection conn = DriverManager.getConnection(url); 
           Statement stmt = conn.createStatement()) {
			
	   // stmt.executeUpdate("DROP TABLE animal");
	   // stmt.executeUpdate("DROP TABLE species");
			
	   stmt.executeUpdate("CREATE TABLE species ("
	        + "id INTEGER PRIMARY KEY, "
	 	+ "name VARCHAR(255), "
		+ "num_acres DECIMAL(4,1))");
		
	   stmt.executeUpdate("CREATE TABLE animal ("
		+ "id INTEGER PRIMARY KEY, "
		+ "species_id integer REFERENCES species (id), "
		+ "name VARCHAR(255), "
		+ "date_born TIMESTAMP)");

	   stmt.executeUpdate("INSERT INTO species VALUES (1, 'African Elephant', 7.5)");
   	   stmt.executeUpdate("INSERT INTO species VALUES (2, 'Zebra', 1.2)");

 	   stmt.executeUpdate("INSERT INTO animal VALUES (1, 1, 'Elsa', '2001-05-06 02:15:00')");
	   stmt.executeUpdate("INSERT INTO animal VALUES (2, 2, 'Zelda', '2002-08-15 09:12:00')");
	   stmt.executeUpdate("INSERT INTO animal VALUES (3, 1, 'Ester', '2002-09-09 10:36:00')");
	   stmt.executeUpdate("INSERT INTO animal VALUES (4, 1, 'Eddie', '2010-06-08 01:24:00')");
	   stmt.executeUpdate("INSERT INTO animal VALUES (5, 2, 'Zoe', '2005-11-12 03:44:00')");
			
	   ResultSet rs = stmt.executeQuery("select count(*) from animal");
	   rs.next();
	   System.out.println(rs.getInt(1));
      }
   }
}

Then compile as usual:

javac SetupDerbyDatabase.java

Running it is a bit different as you need to include the Derby jar file in your classpath. If you don’t know how to find it or encounter problems see the next sections of this blog post. Notice the classpath contains the following three things:

  1. The relative or absolute path of the Derby jar file
  2. A separator (semicolon on Windows, colon on Mac/Linux)
  3. A dot (which means the current directory)

For example, on my Mac either of these works:

java -cp "$JAVA_HOME/db/lib/derby.jar:." SetupDerbyDatabase
java -cp "/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/db/lib/derby.jar:." SetupDerbyDatabase

If all goes well, the program will output the number 5.

Alternatively, you could have added Derby to your CLASSPATH environment variable and just run the program as

java SetupDerbyDatabase

How do I set up the classpath to run the Java program?

If you know where the JDK ($JAVA_HOME) is on your computer, you can start there and then look in the db/lib directory to find the derby.jar file. The most likely location for the JDK install is:

Operating System Most likely location
Windows c:\program files or c:\program files (x86)
Mac /Library/Java/JavaVirtualMachinges
Linux /usr

Or you can search for derby.jar to get the exact path. On Mac and Linux, the search command is:

find / -name derby.jar -print 2> /dev/null

What does this program actually do?

The main method starts out by obtaining a connection to the Derby database. It then creates a statement object so it can run updates. it would have been more efficient to use a PreparedStatement, but those aren’t on the exam. We aren’t taking user input here so there is no security risk with SQL Injection.

Then the code runs two SQL statements to create tables in the zoo database. The commands each include:

  • the table name – species and animal
  • the fields in each table along with their type. Integer is like a Java int. Decimal is like a Java double. Timestamp is like a Java LocalDateTime or old java Date. Varchar stands for variable character and is like a String. The variable length part means that the database doesn’t need to allocate space for all 255 characters and should only use the space for the actual length of the string. (This matters when you frequently update the field with values of different lengths)
  • the primary key for each table – this tells the database how to you uniquely identify each row

Then the code runs seven SQL statements to insert rows into these tables. The order of the data matches the order the fields were defined in the create statements.

Finally, the code runs a query to check the rows were added to the database. The count(*) function in SQL always returns a number. For an empty table, this number is zero. Therefore, we can call rs.next() outside of a conditional or loop. We know there is always a number being returned.

Derby will create a “zoo” directory and a derby.log file in whatever directory you ran the program in. The zoo directory is your database.

Frequently Encountered Problems

If you have an error that isn’t here or have trouble with these instructions, feel free to ask a question in the CodeRanch forums

Error #1 – Derby is not in your classpath or points to an invalid location

Exact error message:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:derby:zoo;create=true

at java.sql.DriverManager.getConnection(DriverManager.java:689)

at java.sql.DriverManager.getConnection(DriverManager.java:270)

at derby.SetupDerbyDatabase.main(SetupDerbyDatabase.java:9)

Solution:

Check you are actually pointing to Derby in your classpath. Also check your classpath has the three required components

Error #2 – The current directory is not in your classpath

Exact error message:

Could not find or load main class derby.SetupDerbyDatabase

Solution:

Check you have the current directory (dot) in your classpath. Also, check you have the correct separator for your operating system (semicolon for Windows, colon for Mac/Linux).

Error #3 – The tables already exist

Exact error message:


Exception in thread "main" java.sql.SQLException: Table/View 'SPECIES' already exists in Schema 'APP'.

at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)

at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)

Solution:

The program can only be run once as is. If you want to run it again, uncomment the two “drop table” lines.

I can’t find derby.jar in my Java install directory?

Make sure you are looking at your JDK and not a JRE. The JRE has less things included. For example, it doesn’t have the javac command. And it doesn’t have Derby.

what does it mean to be OCP Java 8 Programmer II certified?

There are now three paths for one to become OCP 8 (Java Programmer II certified.) So what does it mean someone certified had to know to pass the exam. Well, that varies too.

The three paths

  1. Starting out with Java 8 – take the OCP 8 (IZ0-809)
    Pre-req: OCA 8 (1Z0-808)
  2. Holding a Java 7 Professional cert – take the Java 7 to 8 upgrade exam (IZo-810)
    Pre-req: OCP 7 (IZ0-804) or Java 7 upgrade exam (IZo-805)
  3. Holding any Java 6 or older Professional cert – take the Java 6 or earlier to 8 upgrade exam (IZo-813)
    Pre-req: SCJP/OCJP 6 (IZO-851) or Java 6 upgrade (IZ0-852) or SCJP/OCJP 5 (IZo-853) or Java 5 upgrade (1Zo-854) or Java 4 or lower Professional cert

What one would expect

It seems reasonable to assume some things here.

  1. People taking the OCP 8 directly should be tested on the topics that entail being Java 8 certified.
  2. vennPeople taking the upgrade from Java 7 should be tested on just the topics that were added in Java 8. This is the purple in the Venn diagram. There’s no reason to retest on the topics that the were already on the Java 7 exam. That’s the overlap in the Venn diagram. (This is a bit simplified. It’s really that the topics should be those on the OCA 8 or OCP 8, but not on the OCA 7 or OCP 7. Luckily the topics added on the OCA 8 are also on the OCP 8.)
  3.  venn2People taking the upgrade from older versions of Java have a more interesting situation. When taking a very old exam, lots of topics are different. For example, new topics include generics and the enhanced for loop for those upgrading from Java 5. These topics are so old that it is reasonable to assume the candidate knows this as these syntax changes are covered as part of questions on all sorts of topics. Since the exam changed a lot between Java 6 and 7, let’s just imagine all upgrade candidates in this group took the Java 6 exam. Which would imply the topics covered should be the purple OCP 8 circle except for the overlap with the OCP 6 circle. It doesn’t imply the topics covered in OCP 7 but not OCP 6 or 8 should be covered. After all, those topics were removed from the OCP 8 exam so shouldn’t be needed to get a Java 8 certification.

These assumptions turn out to not match what Oracle actually did. The rest of this blog post describes the surprises.

What people starting out with Java 8 were tested on, but those upgrading from Java 7 were not

Topics:

  1. The concept of immutability
  2. The concepts of deadlock, starvation, livelock, and race conditions. They are tested when upgrading indirectly but with less emphasis.

My thoughts: No big deal here

What people starting out with Java 8 were tested on, but those upgrading from Java 6 were not

  1. The concept of immutability
  2. The singleton pattern
  3. The concepts of deadlock, starvation, livelock, and race conditions. They are tested when upgrading indirectly but with less emphasis.
  4. The entire topic of JDBC

My thoughts: Leaving out the first three isn’t a big deal. Leaving out JDBC is bizarre. That’s a whole topic that is part of core Java. It started being needed for Java 7 (or 8) certification. Upgrading from an older version seems like it should require it. But nope.

What people upgrading from Java 7 were tested on, but those starting out with Java 8 were not

  1. The computeIfAbsent() and computeIfPresent() methods on Map
  2. Also merge()  [removed from main exam in October 2015 and removed from upgrade exam in November 2015]

My thoughts: Odd to add topics on an upgrade exam. These are so similar, I’d almost think they were implied on the Java 8 exam.

What people upgrading from Java 6 were tested on, but those starting out with Java 8 were not

  1. The computeIfAbsent() and computeIfPresent() methods on Map
  2. Also merge()  [removed from main exam in October 2015]
  3. IO.2 – DirectoryWatcher, FileVisitor and WatchService
  4. Locks package in concurrency API
  5. DecimalFormat and SimpleDateFormat

My thoughts: This is bizarre. These three NIO.2 classes, the locks package and the two format classes were on the OCP 7 exam, but not on the OCP 8 exam. Oracle changing their mind and taking something off the exam for a later version is perfectly reasonable. However, I would think that would mean you don’t put it on the upgrade exam! Why should the upgrade exam cover something that isn’t on the target exam.

Summary

If I was writing the objectives for the upgrade exam from Java 6,  I’d have dropped some topics and added JDBC. But I’m not writing the exam objectives so will remain puzzled.