<?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; Derby</title>
	<atom:link href="http://www.rogercuddy.com/tag/derby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rogercuddy.com</link>
	<description>This site holds copies of whatever I&#039;ve posted around the net. YMMV</description>
	<lastBuildDate>Sat, 03 Jul 2010 17:04:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<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&amp;utm_medium=feed&amp;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[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 [...]]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=4f56e4f86bf509535a047def1525d6f0&amp;default=http://use.perl.org/images/pix.gif' alt='No Gravatar' width=40 height=40/><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>
Author <a href="http://www.rogercuddy.com#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" >Roger Cuddy</a> claims no special knowledge of subject beyond a strong interest and slight opinion. Your mileage may vary.<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.rogercuddy.com%2Fprogramming%2Fcombining-derby-embedded-mode-and-server-mode%2F&amp;linkname=Combining%20Derby%20embedded%20mode%20and%20server%20mode"><img src="http://www.rogercuddy.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></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>
