setting up my new mac from my old mac

My previous Mac turned four years old recently and failed. The Apple store looked at it when it started failing and couldn’t find anything wrong. Then two days later, it died all together. The next step was to send it offsite and hope they could fix it. This would have been the second time my Mac would be out of my possession in the same year. (The first was for an issue with 2011 machines that they fixed for free, but took a week)  I decided to buy a new Mac rather than pay a decent percentage of the cost of a new machine and hope they could fix it. I would have replaced the machine in another year or two anyway so decided to start over clean. I am happy with going back to Mac. I didn’t have a series of small problems like I had on Windows. (Just the big one). I didn’t have to deal with viruses. I had a real UNIX command line.

My choice

I had a 15 inch laptop last time and wanted to stay with that.I chose the higher end of the two 15 inch choices. This is the first time I’ve bought a laptop without adding RAM. (Adding RAM wasn’t an option). The specs:

  • 2.5 GHz quad-core Intel Core i7 processor (Turbo Boost up to 3.7GHz)
  • 512 GB flash storage (I used a little under 256GB on my old machine. I started researching laptops when the machine started showing symptoms so I knew this before it died)
  • 16GB 1600MhZ RAM

Buying the laptop

Since I wasn’t customizing anything, I was able to buy the laptop in the same visit to the store where they confirmed my old laptop was dead. This was in the evening (about 9pm) because I went after being at Maker Faire all day. When I gave the Apple sales rep my credit card, it came back as declined. I’m puzzled because I used it earlier in the day. Luckily the Apple employee wasn’t puzzled. Apparently this happens often. She said multiple credit card companies view a large purchase “late at night” from an electronics store to be suspicious. I called and she was right. The credit card company asked me two security questions. The first I knew. The second was “what is your username for online access to your credit card account.” I have no clue. At the time, that information is in autocomplete on my old computer! (and in a Time Machine backup.) Luckily they asked me a third question. Gotta love security questions that even the authorized person knows the answer to.

Setting up

Time Machine is awesome! All my programs and data automatically showed up. It even remembered which sites I was logged into and what was open when the computer died. I didn’t even have to turn Time Machine back on. It just kept updating from the new computer. I did say that I wasn’t going back to the old computer and not worry about backward compatibility.

I had to reset some of the settings:

  1. Pair trackpad. (I have a wired keyboard).
  2. Cover camera with sticker.
  3. Sign back into Dropbox. I was prompted for my two factor credentials.
  4. Turn off notifications for IM. (I don’t like getting a visual notification) Or more specifically change alert style to “none” for everything. I leave the sound on so I know about it.
  5. Turn on firewall.
  6. Agree to XCode license. I was prompted with “Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo” on my first git pull. I think this was coincidence as there was an XCode update right around the time I changed laptops.
  7. Re-enter product key for Office. (Oddly the Windows 8 in my VM didn’t ask for the product key again)
  8. That’s it. Everything else was remembered.

I just have to label the laptop, but I want to wait one more week for that – until the return period passes.

The missing feature – locks

I have a Kensington combination lock for my laptop. Granted anyone with a power tool (wire cutter or saw) could still steal it. But that takes longer and makes it less likely that a maintenance/delivery person could just swipe it. Apple got rid of the place to plug in the lock. They also got rid of the DVD player, but I don’t use that.

I bought the KTech Bracket which you attach to the laptop and has an adapter to plug the existing lock into. The bracket was easy to attach. The idea is that you unscrew four screws from the laptop and rescrew their longer ones with the product in between. They even give you a screwdriver. I managed to lose one of the screws, but KGear said they will mail me a replacement. I’m not thrilled about the need for a product like this in the first place. Now you just need a screwdriver rather than a wire cutter/saw to disconnect the laptop from the lock. I’m disappointed Apple got rid of this feature from the body of the laptop.

 

maker faire 2015

2015-maker-faireThis is the sixth year that I’ve co-organized the NYC FIRST robotics area at World Maker Faire. Every year, it is so much fun. This year, was our first year in the robotics tent. The last two years, we had a standalone small tent. Being in the robotics tent was great. More cross pollination from related exhibits. Plus we were near one of the entrances so got traffic very early in the day.

Maker Faire was very nice in letting us “expand” outside the tent so kids could play with and drive the large five feet tall FRC robots. It was like having a backyard.

We won two ribbons this year. This was the first year we won two in the same year. The blue one is “Editor’s Choice.” The red is “Best in Class.” Not sure what our class was, but still cool.

We got to deal with two our contingency plans this year. Luckily, both were minor. Our tent experienced a brief power outage (maybe 10-15 minutes.) It also drizzled for less than 5 minutes. Everything went smoothly though and I had a great time at the event.

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.