<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Roger Cuddy &#187; Java</title>
	<atom:link href="http://www.rogercuddy.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rogercuddy.com</link>
	<description>This site just holds copies of whatever I&#039;ve posted around the net. YMMV</description>
	<lastBuildDate>Wed, 20 Jul 2011 03:59:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>JDK 7 &#8211; Try with Resources</title>
		<link>http://www.rogercuddy.com/programming/jdk-7-try-with-resources/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.rogercuddy.com/programming/jdk-7-try-with-resources/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 23:38:23 +0000</pubDate>
		<dc:creator>Roger Cuddy</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[JDK 7]]></category>
		<category><![CDATA[Roger Cuddy]]></category>
		<category><![CDATA[try-with-resources]]></category>

		<guid isPermaLink="false">http://www.rogercuddy.com/?p=251</guid>
		<description><![CDATA[<p>JDK 7 has several enhancements to the try-catch construct. Try with resources allows for ensuring that resources are closed and freed up without the need to explicitly call the close function. Any class implementing java.lang.AutoCloseable can be automatically closed without the need for a finally section in the try block.</p> <p>The syntax is a [...]]]></description>
			<content:encoded><![CDATA[<p>JDK 7 has several enhancements to the try-catch construct. Try with resources allows for ensuring that resources are closed and freed up without the need to explicitly call the close function. Any class implementing java.lang.AutoCloseable can be automatically closed without the need for a finally section in the try block.</p>
<p>The syntax is a try with parens and the resources to automatically close out inside the parens.<br />
<b>try ( resources ) { code } catch (exception) { } </b></p>
<p>snippet 1 shows the syntax for try with resources:</p>
<pre class="brush: java; ">

try (
    BufferedReader reader = new BufferedReader(
        new FileReader(&quot;c:/dev/jdk7test/t1.ofx&quot;));
   BufferedWriter writer = new BufferedWriter(
        new FileWriter(&quot;c:/dev/jdk7test/output.txt&quot;))
   ) {
         // read and write
     }
</pre>
<p>Another change to try is that the catch can now handle multiple exception types in one block. Multiple catch blocks are optional and still work fine if you prefer to separate the handling. Just separate the exception types with | .</p>
<p>Snippet 2 catch with multiple exception types:</p>
<pre class="brush: java; ">

catch (java.io.FileNotFoundException | java.io.IOException ex) {
    System.out.println(ex.getMessage());
}
</pre>
<p>Lastly here is a small simple class that shows both features in action. </p>
<pre class="brush: java; ">

package jdk7test;
import java.io.*;
public class Jdk7TryWithResourcesTest {
    public static void main(String[] args) {
        Jdk7TryWithResourcesTest test = new Jdk7TryWithResourcesTest();
        test.execute();
    }

    public void execute() {
        try (
                BufferedReader reader = new BufferedReader(new FileReader(&quot;c:/dev/jdk7test/t1.ofx&quot;));
                BufferedWriter writer = new BufferedWriter(new FileWriter(&quot;c:/dev/jdk7test/output.txt&quot;))
            ) {
            String line;
            while ((line = reader.readLine()) != null){
                writer.write(line);
            }
        } catch (java.io.FileNotFoundException | java.io.IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
}
</pre>
Standard disclaimer - At the time of posting I believe  all information contained to be accurate but there is absolutely no warranty or guarantee.  ]]></content:encoded>
			<wfw:commentRss>http://www.rogercuddy.com/programming/jdk-7-try-with-resources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JDK 7 &#8211; use Strings in switch statements</title>
		<link>http://www.rogercuddy.com/programming/jdk-7-use-strings-in-switch-statements-2/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.rogercuddy.com/programming/jdk-7-use-strings-in-switch-statements-2/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 20:29:30 +0000</pubDate>
		<dc:creator>Roger Cuddy</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[JDK 7]]></category>
		<category><![CDATA[Roger Cuddy]]></category>

		<guid isPermaLink="false">http://www.rogercuddy.com/?p=247</guid>
		<description><![CDATA[<p>JDK 7 adds the ability to use String objects as the case in switch blocks. It&#8217;s not exactly an earth shattering addition but I find it quite useful in avoiding long if-then chains. Plus the release docs claim that using a switch is more efficient.</p> <p>At the bottom of this post is a small [...]]]></description>
			<content:encoded><![CDATA[<p>JDK 7 adds the ability to use String objects as the case in switch blocks. It&#8217;s not exactly an earth shattering addition but I find it quite useful in avoiding long if-then chains. Plus the release docs claim that using a switch is more efficient.</p>
<p>At the bottom of this post is a small java class that provides a complete example of use. Here is the part we&#8217;re concerned about. The switch takes a String argument that should be the abbreviated name of a month and sets the return value to the quarter of the year that month is in.</p>
<pre class="brush: java; ">

switch(monthIn) {
            case &quot;Jan&quot;:
            case &quot;Feb&quot;:
            case &quot;Mar&quot;:
                retval = &quot;Q1&quot;;
                break;
            case &quot;Apr&quot;:
            case &quot;May&quot;:
            case &quot;Jun&quot;:
                retval = &quot;Q2&quot;;
                break;
            case &quot;Jul&quot;:
            case &quot;Aug&quot;:
            case &quot;Sep&quot;:
                retval = &quot;Q3&quot;;
                break;
            case &quot;Oct&quot;:
            case &quot;Nov&quot;:
            case &quot;Dec&quot;:
                retval = &quot;Q4&quot;;
                break;
            default:
                throw new IllegalArgumentException(&quot;Invalid Month argument: &quot; + monthIn);
}
</pre>
<p>That&#8217;s a pretty geedunk example but it shows the functionality and is simple to read/understand. Here is a complete class that shows the same switch statement in use:</p>
<pre class="brush: java; ">

package jdk7test;
import java.util.Calendar;

public class Jdk7test {
    public static void main(String args[]) {
        Jdk7test test = new Jdk7test();
        test.execute();
    }

    public void execute() {
        String[] strMonths = new String[] { &quot;Jan&quot;, &quot;Feb&quot;, &quot;Mar&quot;, &quot;Apr&quot;, &quot;May&quot;, &quot;Jun&quot;, &quot;Jul&quot;, &quot;Aug&quot;,
                                            &quot;Sep&quot;, &quot;Oct&quot;, &quot;Nov&quot;, &quot;Dec&quot; };
        Calendar cal = Calendar.getInstance();
        try {
            String monthName = strMonths[cal.get(Calendar.MONTH)];
            String quarter = getQuarter(monthName);
            System.out.println(&quot;Month: &quot; +  monthName + &quot; is in quarter: &quot;
                               + quarter);
        } catch (IllegalArgumentException e) {
            System.out.println(&quot;The month sent to getQuarter was invalid&quot;);
            System.out.println(e.getMessage());
        }
    }

    public String getQuarter(String monthIn) {
        String retval;
        switch(monthIn) {
            case &quot;Jan&quot;:
            case &quot;Feb&quot;:
            case &quot;Mar&quot;:
                retval = &quot;Q1&quot;;
                break;
            case &quot;Apr&quot;:
            case &quot;May&quot;:
            case &quot;Jun&quot;:
                retval = &quot;Q2&quot;;
                break;
            case &quot;Jul&quot;:
            case &quot;Aug&quot;:
            case &quot;Sep&quot;:
                retval = &quot;Q3&quot;;
                break;
            case &quot;Oct&quot;:
            case &quot;Nov&quot;:
            case &quot;Dec&quot;:
                retval = &quot;Q4&quot;;
                break;
            default:
                throw new IllegalArgumentException(&quot;Invalid Month argument: &quot; + monthIn);
        }
        return retval;
    }
}
</pre>
Standard disclaimer - At the time of posting I believe  all information contained to be accurate but there is absolutely no warranty or guarantee.  ]]></content:encoded>
			<wfw:commentRss>http://www.rogercuddy.com/programming/jdk-7-use-strings-in-switch-statements-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JDK 7 release candidate now available</title>
		<link>http://www.rogercuddy.com/programming/jdk-7-first-release-candidate/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.rogercuddy.com/programming/jdk-7-first-release-candidate/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 16:17:28 +0000</pubDate>
		<dc:creator>Roger Cuddy</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java 7]]></category>
		<category><![CDATA[Roger Cuddy]]></category>

		<guid isPermaLink="false">http://www.rogercuddy.com/?p=231</guid>
		<description><![CDATA[<p>Build 147 for JDK 7 is now available with the goal of a generally available release later this month. Some good resources are: Oracle&#8217;s Java 7 wiki Java 7 project page Get the release from the download page Oracle&#8217;s Java SE 7 documentation <p>As I test out the release I&#8217;ll do some posts on [...]]]></description>
			<content:encoded><![CDATA[<p>Build 147 for JDK 7 is now available with the goal of a generally available release later this month. Some good resources are: 
<ul>
<li><a target="_blank" href="http://wikis.sun.com/display/JDK7/Home">Oracle&#8217;s Java 7 wiki</a></li>
<li><a target="_blank" href="http://openjdk.java.net/projects/jdk7/">Java 7 project page</a></li>
<li>Get the release from the <a target="_blank" href="http://jdk7.java.net/download.html">download page</a></li>
<li>Oracle&#8217;s <a target="_blank" href="http://download.oracle.com/javase/7/docs/index.html">Java SE 7 documentation</a></li>
</ul>
<p>As I test out the release I&#8217;ll do some posts on new features. </p>
<p>&nbsp;</p>
<p class="technorati-tags"><a href="http://technorati.com/tag/Java" rel="tag">Java</a>, <a href="http://technorati.com/tag/Java%207" rel="tag">Java 7</a>, <a href="http://technorati.com/tag/JDK%207" rel="tag">JDK 7</a>, <a href="http://technorati.com/tag/Roger%20Cuddy" rel="tag">Roger Cuddy</a></p>
Standard disclaimer - At the time of posting I believe  all information contained to be accurate but there is absolutely no warranty or guarantee.  ]]></content:encoded>
			<wfw:commentRss>http://www.rogercuddy.com/programming/jdk-7-first-release-candidate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IBM and Oracle finally get together on OpenJDK</title>
		<link>http://www.rogercuddy.com/general/ibm-and-oracle-finally-get-together-on-openjdk/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.rogercuddy.com/general/ibm-and-oracle-finally-get-together-on-openjdk/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 14:42:40 +0000</pubDate>
		<dc:creator>Roger Cuddy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[IBM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenJDK]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://www.rogercuddy.com/general/ibm-and-oracle-finally-get-together-on-openjdk/</guid>
		<description><![CDATA[<p>This is something I’ve personally hoped would happen eventually. Having all the big players in the space working with the open source community will help to prevent divergence between the open and commercial implementations. Throughout the Sun/Oracle bumps, Java has remained a popular choice for application development and this action should help continue it’s [...]]]></description>
			<content:encoded><![CDATA[<p>This is something I’ve personally hoped would happen eventually. Having all the big players in the space working with the open source community will help to prevent divergence between the open and commercial implementations. Throughout the Sun/Oracle bumps, Java has remained a popular choice for application development and this action should help continue it’s popularity. The press release from Oracle is available at <a title="http://www.oracle.com/us/corporate/press/176988" href="http://www.oracle.com/us/corporate/press/176988">http://www.oracle.com/us/corporate/press/176988</a> . </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rogercuddy.com/general/ibm-and-oracle-finally-get-together-on-openjdk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combining Derby embedded mode and server mode</title>
		<link>http://www.rogercuddy.com/programming/combining-derby-embedded-mode-and-server-mode/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.rogercuddy.com/programming/combining-derby-embedded-mode-and-server-mode/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 10:25:21 +0000</pubDate>
		<dc:creator>Roger Cuddy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Derby]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.rogercuddy.com/combining-derby-embedded-mode-and-server-mode-60.htm</guid>
		<description><![CDATA[<p>Recently there have been several occasions where I have needed an application to use an embedded driver but also needed to allow for the occasional login to the database externally while the application is executing. Derby works wonderfully for this and starting a server within the application is exceedingly simple. Here I will give [...]]]></description>
			<content:encoded><![CDATA[<p>Recently there have been several occasions where I have needed an application to use an embedded driver but also needed to allow for the occasional login to the database externally while the application is executing. Derby works wonderfully for this and starting a server within the application is exceedingly simple. Here I will give just a brief example that should be enough to demonstrate the method. The code fragments here are admittedly simplistic but they work and show the principles cleanly. </p>
<pre><span style=" font-family:'Courier New,courier';"> </span></pre>
<pre><span style=" font-family:'Courier New,courier';">//First a method to get an embedded connection:</span></pre>
<pre></pre>
<pre><span style=" font-family:'Courier New,courier';">public Connection getConnection() {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">    try {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">        return DriverManager.getConnection("jdbc:derby:"+dbPath);</span></pre>
<pre><span style=" font-family:'Courier New,courier';">    } catch (SQLException e) {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">        log.error(e.getMessage(),e);</span></pre>
<pre><span style=" font-family:'Courier New,courier';">        return null;</span></pre>
<pre><span style=" font-family:'Courier New,courier';">    }</span></pre>
<pre><span style=" font-family:'Courier New,courier';">}</span></pre>
<pre></pre>
<pre><span style=" font-family:'Courier New,courier';">//Now a method to start the server for external connections.</span></pre>
<pre></pre>
<pre><span style=" font-family:'Courier New,courier';">private void startDBServer() {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">	try {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">		server = new NetworkServerControl(InetAddress.getByName("Localhost"),1527);</span></pre>
<pre><span style=" font-family:'Courier New,courier';">		server.start(null);</span></pre>
<pre><span style=" font-family:'Courier New,courier';">	} catch (Exception e) {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">		log.error(e.getMessage(),e);</span></pre>
<pre><span style=" font-family:'Courier New,courier';">	}</span></pre>
<pre><span style=" font-family:'Courier New,courier';">}</span></pre>
<pre></pre>
<pre><span style=" font-family:'Courier New,courier';">//And lastly let's add a shudown hook to ensure the server gets a chance to exit clean.</span></pre>
<pre></pre>
<pre><span style=" font-family:'Courier New,courier';">private void setShutdownHook() {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">	Runtime.getRuntime().addShutdownHook(new Thread() {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">		public void run() {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">			log.info("**** Application ending ****");</span></pre>
<pre><span style=" font-family:'Courier New,courier';">			try {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">				server.shutdown();</span></pre>
<pre><span style=" font-family:'Courier New,courier';">				} catch (Exception e) {</span></pre>
<pre><span style=" font-family:'Courier New,courier';">					log.error(e.getMessage(),e);</span></pre>
<pre><span style=" font-family:'Courier New,courier';">				}</span></pre>
<pre><span style=" font-family:'Courier New,courier';">			}</span></pre>
<pre><span style=" font-family:'Courier New,courier';">    } );</span></pre>
<pre><span style=" font-family:'Courier New,courier';">}</span></pre>
<pre></pre>
<p> That&#8217;s the basics and it doesn&#8217;t get much easier.</p>
Standard disclaimer - At the time of posting I believe  all information contained to be accurate but there is absolutely no warranty or guarantee.  ]]></content:encoded>
			<wfw:commentRss>http://www.rogercuddy.com/programming/combining-derby-embedded-mode-and-server-mode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

