Thursday, April 18, 2013

Blobs and JDBC

A blob is a database data type for storing raw, binary data. It stands for "binary large object". In this blog post, I'm going to show you how to use this data type to insert and retrieve a photo using JDBC.

To insert the photo, start by creating an InputStream object to the photo you want to insert. For example, if the photo resides in a file, create a FileInputStream object.

File file = new File("photo.jpg");
InputStream in = new FileInputStream(file);

Then, create a PreparedStatement object for your INSERT statement. The PreparedStatement should contain a parameter for where the binary data should go, just as if you were inserting "normal" data, like a string or an integer.

Connection conn = ...
PreparedStatement stmt = conn.prepareStatement("INSERT INTO test (photo) VALUES (?)");

To set the binary data, pass the InputStream object into the setBlob() method, and then execute the statement.

stmt.setBlob(1, in);
stmt.execute();

To retrieve blob data from the database, call the getBlob() method on the ResultSet object that is returned from the SELECT statement. This will return a Blob object. Then, invoke the Blob.getBinaryStream() method to get an InputStream to the binary data.

Connection conn = ...
PreparedStatement stmt = conn.prepareStatement("SELECT photo FROM test");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
  Blob blob = rs.getBlob(1);
  InputStream in = blob.getBinaryStream();
  ...
}

Tuesday, April 16, 2013

4 Ways to Initialize a List in Java

Creating a List and populating it with a set of elements is a common programming task in Java. In this blog post, I'm going to describe four ways to do this.

1. Collections.emptyList()

This method will return a List object that is empty. This is a convenient, shorthand alternative to explicitly instantiating a new List object. However, this list is immutable, which means that you cannot add any elements to it.

List<String> list = Collections.emptyList();

2. Arrays.asList()

This method takes an array and converts it to a List object. What makes this method special is the fact that the argument to this method is a vararg. This means that you can pass as many elements into the method as you like. The syntax is very compact because all of the elements can fit on one line.

But note that, just as with Collections.emptyList(), the list that is created is immutable, so you cannot add or remove elements to/from it.

List<String> list = Arrays.asList("one", "two", "three");

3. Anonymous child class

A somewhat trickier way of creating a list is to define your list as an anonymous, child class. The elements are added to the list by calling the add() method within the class' initializer block (notice the double braces).

List<String> list = new ArrayList<String>(){{add("one"); add("two"); add("three");}}

4. JUST CALL add(), EINSTEIN!

Of course, the traditional way to create a list is to instantiate it and then call the add() method for each element. But where's the fun in that?

List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");

Monday, April 15, 2013

5 Unique Features of Stackoverflow Chat

Stackoverflow, a technical question and answer site, has a web-based chat room system that allows you to talk with other programmers in real time. In this blog post, I'm going to describe five unique features of the Stackoverflow Chat system.

1. Mentions.

If you want to direct a message to a particular user, type a @ character, followed by the user's name. This will cause a "ping" sound to play in the user's browser, altering them to the fact that they were mentioned in a chat message. If the user's name has spaces in it, simply leave out the spaces.

You can also reply to specific message. To do this, hover your mouse over the message you want to reply to and click the "reply" icon on the right. Hovering your mouse over a reply will highlight the message that it was a reply to. The user who posted the message will receive a "ping".

2. Editing and deleting messages.

After sending a message, you may notice a spelling mistake or typo in your message. Stackoverflow chat allows you to edit and delete messages that are less than 2 minutes old. Hover your mouse over the message you want to edit or delete, and click the drop down arrow icon on the left.

This will open up a menu, which allows you to edit or delete your message.

3. Text formatting.

You can also format the text of your message for added emphasis. The following syntaxes are supported:

*italic*
**bold**
`code (monospaced font)`
---strikeout---

Note that, if you want to include a multi-line code sample, hold down the Shift key and then press Enter to insert a line break in your chat message (just pressing Enter will send the message). Then, you can click on the "fixed font" button to change the font of your entire message to monospace. The "fixed font" button will not appear unless your chat message has multiple lines.

4. Starred messages.

If someone posts a message that you really like, you can "star" it. Starred messages appear on the right-hand side of the window. The more stars a message has, the longer it will stay pinned to this location. To star a message, hover your mouse over the chat message you want to star, and click the "star" icon on the right.

5. Oneboxing.

If you paste URLs to particular websites into a chat messages, then the chat system will create a nicely formatted "widget" containing the content of that webpage. For example, pasting the URL to a Stackoverflow question will display various information about the question, such as the number of upvotes, the tags, and the question itself. This is called "oneboxing".

Supported websites include the Stack Exchange family of websites, Wikipedia, and Youtube.

For more information about Stackoverflow Chat, see the FAQ page.

Sunday, April 14, 2013

Javadoc and the @see tag

Javadoc, as you know, is a tool for documenting your source code in the Java language. It consists of specially-formatted comments that describe the classes, methods, and fields of your Java program. IDEs leverage Javadoc comments to help inform developers of how various APIs function (for example, hovering your mouse over a method in Eclipse will display that method's Javadoc). You can also run the "javadoc" command, which comes packaged with the JDK, to generate an HTML webpage containing the Javadoc comments of your entire code base.

Javadoc syntax defines a collection of tags. Tags allow you to describe specific aspects of your code, such as the return value of a method or the author of class. One of these tags is @see, which is like a "see also" reference. In this blog post, I'm going to describe the three ways to use @see.

1) Referring to a class or method. One way to use @see is to refer the user to another class or method within your code base. An import statement for the class you want to reference must be added to the Java code. Then, simply put the class name after the tag.

import com.example.RefClass;
/*
 * Description of this class.
 * @see RefClass
 */
public class MyClass{}

To refer to a method, put a #, followed by the method name, after the class name. If the method is overloaded, then be sure to include the parameters as well (enclosed in parenthesis) to remove any ambiguity as to which method you are referring to. Javadoc gurus will recognize this as the same syntax that's used with the @link tag.

import com.example.RefClass;
/*
 * Description of this class.
 * @see RefClass#execute(String, int)
 */
public class MyClass{}

2) Referring to a website. Another way of using @see is to refer to a website. For this, you must use the HTML <a> tag.

/*
 * Description of this class.
 * @see <a href="http://example.com">Library website</a>
 */
public class MyClass{}

3) Plain text. You can also just put plain text within the @see tag. However, it must be surrounded with double quotes in order to prevent the Javadoc parser from treating it like a class name.

/*
 * Description of this class.
 * @see "Library Documentation"
 */
public class MyClass{}