<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Mike's Code</title>
	<atom:link href="http://mikescode.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mikescode.wordpress.com</link>
	<description>Programming tips for stuff I think is relevant</description>
	<lastBuildDate>Wed, 28 Oct 2009 20:37:22 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='mikescode.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/25de8ff5b0f4c045ee3cbbb44ffa5d95?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Mike's Code</title>
		<link>http://mikescode.wordpress.com</link>
	</image>
			<item>
		<title>Configuring Project Voldemort to use MySQL</title>
		<link>http://mikescode.wordpress.com/2009/10/28/configuring-project-voldemort-to-use-mysql/</link>
		<comments>http://mikescode.wordpress.com/2009/10/28/configuring-project-voldemort-to-use-mysql/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 20:33:37 +0000</pubDate>
		<dc:creator>mikescode</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[mysql java voldemort database replication]]></category>

		<guid isPermaLink="false">http://mikescode.wordpress.com/?p=14</guid>
		<description><![CDATA[Project Voldemort (v0.51) uses BerkeleyDB by default and the steps necessary to use MySQL as the backing data store are slightly different than what is on the website. This is what I did:
1. Copied mysql-connector-java-5.0.4.jar into voldemort/lib
2. Set mysql as the persistence engine in stores.xml:
&#60;stores&#62;
  &#60;store&#62;
    ...
    &#60;persistence&#62;mysql&#60;/persistence&#62;
    ...
  &#60;/store&#62;
  ...
&#60;/stores&#62;
3. Added this line to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=14&subd=mikescode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a title="Project Voldemort" href="http://project-voldemort.com/" target="_blank">Project Voldemort</a> (v0.51) uses BerkeleyDB by default and the steps necessary to use MySQL as the backing data store are slightly different than what is on the website. This is what I did:</p>
<p>1. Copied mysql-connector-java-5.0.4.jar into voldemort/lib<br />
2. Set mysql as the persistence engine in stores.xml:</p>
<pre><code>&lt;stores&gt;
  &lt;store&gt;
    ...
    &lt;persistence&gt;mysql&lt;/persistence&gt;
    ...
  &lt;/store&gt;
  ...
&lt;/stores&gt;</code></pre>
<p>3. Added this line to server.properties to enable mysql:</p>
<p>storage.configs=voldemort.store.mysql.MysqlStorageConfiguration, voldemort.store.memory.InMemoryStorageConfiguration, voldemort.store.memory.CacheStorageConfiguration, voldemort.store.bdb.BdbStorageConfiguration</p>
<ul>
<li>The documentation says to set &#8220;enable.mysql.engine=true&#8221;, this does not work. The engine loading config property has since been generalized so that you can import your own data stores.</li>
</ul>
<p>4. Set up login information in server.properties:</p>
<p># Mysql<br />
mysql.host=localhost<br />
mysql.port=3306<br />
mysql.user=root<br />
mysql.password=password<br />
mysql.database=voldemort_single</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mikescode.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mikescode.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mikescode.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mikescode.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mikescode.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mikescode.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mikescode.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mikescode.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mikescode.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mikescode.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=14&subd=mikescode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mikescode.wordpress.com/2009/10/28/configuring-project-voldemort-to-use-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fee2b0cffb1f69242d7aae4076f75b17?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mike</media:title>
		</media:content>
	</item>
		<item>
		<title>Tomcat and MBeanTrustPermission</title>
		<link>http://mikescode.wordpress.com/2009/05/28/tomcat-mbeantrustpermission/</link>
		<comments>http://mikescode.wordpress.com/2009/05/28/tomcat-mbeantrustpermission/#comments</comments>
		<pubDate>Thu, 28 May 2009 04:07:25 +0000</pubDate>
		<dc:creator>mikescode</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jmx]]></category>
		<category><![CDATA[mbean]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://mikescode.wordpress.com/?p=8</guid>
		<description><![CDATA[Here&#8217;s a problem a savvy web developer might run into. If you have a war that exports MBeans for JMX, upon dropping this war into Tomcat you may run into this nasty exception:
java.security.AccessControlException: access denied (javax.management.MBeanTrustPermission register)
This is caused by an overrestrictive security policy. Edit conf/catalina.policy to include this line:
grant {
permission javax.management.MBeanTrustPermission &#8220;register&#8221;;
};
Then add to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=8&subd=mikescode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Here&#8217;s a problem a savvy web developer might run into. If you have a war that exports MBeans for JMX, upon dropping this war into Tomcat you may run into this nasty exception:</p>
<p>java.security.AccessControlException: access denied (javax.management.MBeanTrustPermission register)</p>
<p>This is caused by an overrestrictive security policy. Edit conf/catalina.policy to include this line:</p>
<p>grant {<br />
permission javax.management.MBeanTrustPermission &#8220;register&#8221;;<br />
};</p>
<p>Then add to your CATALINA_OPTS the following VM argument:</p>
<p>-Djava.security.policy=&lt;path to catalina.policy&gt;</p>
<p>I&#8217;m not sure if there&#8217;s a way to have Tomcat read in catalina.policy automatically, but if there is, I haven&#8217;t figured it out yet.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mikescode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mikescode.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mikescode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mikescode.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mikescode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mikescode.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mikescode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mikescode.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mikescode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mikescode.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=8&subd=mikescode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mikescode.wordpress.com/2009/05/28/tomcat-mbeantrustpermission/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fee2b0cffb1f69242d7aae4076f75b17?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mike</media:title>
		</media:content>
	</item>
		<item>
		<title>Java and XML quick start tips</title>
		<link>http://mikescode.wordpress.com/2008/06/06/java-and-xml-quick-start-tips/</link>
		<comments>http://mikescode.wordpress.com/2008/06/06/java-and-xml-quick-start-tips/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 03:20:52 +0000</pubDate>
		<dc:creator>mikescode</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikescode.wordpress.com/?p=5</guid>
		<description><![CDATA[I see a lot of code that makes using Java and XML libraries overly verbose and complex. It bothers me because Java has a ton of XML support packaged right into the JRE. However, there&#8217;s a couple simple things that aren&#8217;t no brainer one-liners that I wanted to post.
Creating a new DOM document. For better [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=5&subd=mikescode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I see a lot of code that makes using Java and XML libraries overly verbose and complex. It bothers me because Java has a ton of XML support packaged right into the JRE. However, there&#8217;s a couple simple things that aren&#8217;t no brainer one-liners that I wanted to post.</p>
<p><strong>Creating a new DOM document. </strong>For better or for worse, Java wasn&#8217;t interested in making a simple DOM Document bean that you can initialize with Document document = new Document(). But, you shouldn&#8217;t be wasting more than three lines on your document initialization:</p>
<pre class="brush: java;">
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
</pre>
<div class="ArwC7c ckChnd"><strong>Printing XML DOM. </strong>You&#8217;d think spitting your DOM out to a string would be simply encapsulated in toString() or some other single function. Unless your using some higher level library like XMLBeans, this isn&#8217;t so true. However, it does buy you a lot of flexibility with how to output your xml. This is the simplest way I&#8217;ve encountered:</div>
<div class="ArwC7c ckChnd">
<pre class="brush: java;">
protected void printXmlDocument(Document document, Writer writer) throws TransformerException {
 TransformerFactory transformerFactory = TransformerFactory.newInstance();
 transformerFactory.setAttribute(&quot;indent-number&quot;, 2);
 Transformer transformer = transformerFactory.newTransformer();
 transformer.setOutputProperty(OutputKeys.INDENT, &quot;yes&quot;);
 DOMSource source = new DOMSource(document);
 StreamResult result = new StreamResult(writer);
 transformer.transform (source, result);
}
</pre>
</div>
<div class="ArwC7c ckChnd">Note that above I included two extra lines to provide some nice pretty printing with 2 space indents. If you omit these lines you can output your XML with even less code, but it&#8217;ll all be one string.</div>
<div class="ArwC7c ckChnd">Well, that&#8217;s all that was on my mind, but those are two blocks of code I find myself referencing all the time.</div>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mikescode.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mikescode.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mikescode.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mikescode.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mikescode.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mikescode.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mikescode.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mikescode.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mikescode.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mikescode.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mikescode.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mikescode.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=5&subd=mikescode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mikescode.wordpress.com/2008/06/06/java-and-xml-quick-start-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fee2b0cffb1f69242d7aae4076f75b17?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mike</media:title>
		</media:content>
	</item>
		<item>
		<title>JMX and Spring is Magic</title>
		<link>http://mikescode.wordpress.com/2008/02/22/jmx-and-spring-is-magic/</link>
		<comments>http://mikescode.wordpress.com/2008/02/22/jmx-and-spring-is-magic/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 01:22:40 +0000</pubDate>
		<dc:creator>mikescode</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[jconsole]]></category>
		<category><![CDATA[jmx]]></category>

		<guid isPermaLink="false">http://mikescode.wordpress.com/?p=4</guid>
		<description><![CDATA[Adding JMX to your spring project is a snap. If you need to ask what JMX is, basically think of the most awesome way to manage your Java beans and applications and multiply it by 50. Then divide by 25. But seriously, JMX provides you a way to execute methods from your Java beans by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=4&subd=mikescode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Adding <a href="http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html">JMX</a> to your <a href="http://www.springframework.org">spring</a> project is a snap. If you need to ask what JMX is, basically think of the most awesome way to manage your Java beans and applications and multiply it by 50. Then divide by 25. But seriously, JMX provides you a way to execute methods from your Java beans by connecting to your running application via RMI. It&#8217;s a great way to check on the status of objects in your app while it is running. Best of all, you get it all for almost free. Just add this bean to your application context:</p>
<pre class="brush: xml;">

&lt;bean id=&quot;exporter&quot; class=&quot;org.springframework.jmx.export.MBeanExporter&quot;&gt;
    &lt;property name=&quot;beans&quot;&gt;
        &lt;map&gt;
            &lt;entry key=&quot;someCategory:name=cacheManager&quot;
                   value-ref=&quot;cacheManager&quot;/&gt;
            &lt;entry key=&quot;fooCategory:type=fooSubCategory,name=FooBar&quot;
                   value-ref=&quot;fooBar&quot;/&gt;
        &lt;/map&gt;
    &lt;/property&gt;
&lt;/bean&gt;</pre>
<p>Inside the map, insert an entry for every bean you want to expose. When a bean is exposed, its public functions will be available to JMX clients. This is particularly useful for methods that take primitives as parameters and/or return primitives. The value-ref field points to a bean in your application context. The key defines a folder for your bean, an optional subfolder, and the name of your bean. This helps organize things in a visual JMX application like JConsole.<br />
To get your JMX server running, execute your Java program with the following command line options:</p>
<p>-Dcom.sun.management.jmxremote<br />
-Dcom.sun.management.jmxremote.port=13590<br />
-Dcom.sun.management.jmxremote.ssl=false  -Dcom.sun.management.jmxremote.authenticate=false</p>
<p>The first parameter turns on the server/RMI. The second parameter makes JMX available on a specified port (just a little useful for remote connections). Turning SSL off and authenticate off in the third and fourth parameters is useful for internal networks. If you want to expose your JMX console to the world, you&#8217;ll want to research adding some sort of authentication via these options. If you&#8217;re putting your application inside Tomcat, you can modify the JAVA_OPTS variable inside bin/catalina.sh or bin/catalina.bat. Just add the same options and restart Tomcat and you&#8217;ll be ready to go.</p>
<p>Finally, you&#8217;ll probably want to see the results of all this configuration. In Java 5 and 6, all you need to do is run:</p>
<pre>jconsole</pre>
<p>at the command prompt and it will detect all running JMX applications on your localhost. Alternatively, you can use it to connect to a remote host running JMX.</p>
<p>I think JMX is a great tool both for development and production and is yet another reason to develop your apps in Java. Check it out today if you haven&#8217;t already.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mikescode.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mikescode.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mikescode.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mikescode.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mikescode.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mikescode.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mikescode.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mikescode.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mikescode.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mikescode.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mikescode.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mikescode.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=4&subd=mikescode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mikescode.wordpress.com/2008/02/22/jmx-and-spring-is-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fee2b0cffb1f69242d7aae4076f75b17?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mike</media:title>
		</media:content>
	</item>
		<item>
		<title>Exporting a MySQL table to Excel XLS in Java</title>
		<link>http://mikescode.wordpress.com/2008/02/16/exporting-a-mysql-table-to-excel-xls-in-java/</link>
		<comments>http://mikescode.wordpress.com/2008/02/16/exporting-a-mysql-table-to-excel-xls-in-java/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 01:54:31 +0000</pubDate>
		<dc:creator>mikescode</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[poi]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mikescode.wordpress.com/?p=3</guid>
		<description><![CDATA[I decided to get things rolling on this blog with a short but useful example using JDBC and Apache POI to output a MySQL database table to an Excel spreadsheet. You&#8217;ll need two jars to get this code compiled and running: POI 3.0 and the MySQL java connector. Of course, you&#8217;ll also need a MySQL [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=3&subd=mikescode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I decided to get things rolling on this blog with a short but useful example using JDBC and <a href="http://poi.apache.org/" title="Apache POI" target="_blank">Apache POI</a> to output a MySQL database table to an Excel spreadsheet. You&#8217;ll need two jars to get this code compiled and running: <a href="http://www.mirrorgeek.com/apache.org/poi/release/bin/" target="_blank">POI 3.0</a> and the <a href="http://dev.mysql.com/downloads/connector/j/5.0.html" target="_blank">MySQL java connector</a>. Of course, you&#8217;ll also need a MySQL database&#8230; but that&#8217;s a whole other bag of worms.</p>
<p>I tried to keep this code as simple as possible. The MysqlToXls object is initialized with the database name, user name, and password. Its generateXls() function takes the name of the table to output and the filename to output to. Finally, a close() method closes the database connection&#8230; and that&#8217;s all there is to it! I included a main method that uses this object to connect to a database named test as user root with an empty password. It then dumps the person table as person.xls.</p>
<pre class="brush: java;">
import java.io.*;
import java.sql.*;
import org.apache.poi.hssf.usermodel.*;
import java.util.*;

public class MysqlToXls {

  private Connection connection = null;

  public MysqlToXls(String database, String user, String password)
  	throws ClassNotFoundException, SQLException {

		// Create MySQL database connection
    Class.forName(&quot;com.mysql.jdbc.Driver&quot;);

    String url = &quot;jdbc:mysql://localhost:3306/&quot; + database;
    connection = DriverManager.getConnection(url, user, password);
  }

  public void generateXls(String tablename, String filename)
  	throws SQLException, FileNotFoundException, IOException {

    // Create new Excel workbook and sheet
    HSSFWorkbook xlsWorkbook = new HSSFWorkbook();
    HSSFSheet xlsSheet = xlsWorkbook.createSheet();
    short rowIndex = 0;

    // Execute SQL query
    PreparedStatement stmt =
    connection.prepareStatement(&quot;select * from &quot; + tablename);
    ResultSet rs = stmt.executeQuery();

    // Get the list of column names and store them as the first
    // row of the spreadsheet.
    ResultSetMetaData colInfo = rs.getMetaData();
    List&lt;String&gt; colNames = new ArrayList&lt;String&gt;();
    HSSFRow titleRow = xlsSheet.createRow(rowIndex++);

    for (int i = 1; i &lt;= colInfo.getColumnCount(); i++) {
      colNames.add(colInfo.getColumnName(i));
      titleRow.createCell((short) (i-1)).setCellValue(
        new HSSFRichTextString(colInfo.getColumnName(i)));
      xlsSheet.setColumnWidth((short) (i-1), (short) 4000);
    }

    // Save all the data from the database table rows
    while (rs.next()) {
      HSSFRow dataRow = xlsSheet.createRow(rowIndex++);
      short colIndex = 0;
      for (String colName : colNames) {
        dataRow.createCell(colIndex++).setCellValue(
          new HSSFRichTextString(rs.getString(colName)));
      }
    }

    // Write to disk
    xlsWorkbook.write(new FileOutputStream(filename));
  }

  // Close database connection
  public void close() throws SQLException {
    connection.close();
  }

  public static void main(String[] args) {
    try {
      MysqlToXls mysqlToXls = new MysqlToXls(&quot;test&quot;, &quot;root&quot;, &quot;&quot;);
      mysqlToXls.generateXls(&quot;person&quot;, &quot;person.xls&quot;);
      mysqlToXls.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}</pre>
<p>So there you have it! Take your data out of a great storage mechanism and throw it into a crappy spreadsheet! This code could easily be ported to Postgres or Oracle with the proper JDBC connector.</p>
<p>You might also want to play around with setting the column type in POI. The HSSFCell.setCellValue() can take an int, a boolean, a Calendar object, a Date, or a double instead of just a string.</p>
<p>A few things you might want to watch out for: POI has an unholy obsession with using shorts. Also, JDBC refers to columns starting with #1 instead of the usual 0 index.  Finally, I have no idea where these column width numbers come from&#8230; but 4000 looked good to me!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mikescode.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mikescode.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mikescode.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mikescode.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mikescode.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mikescode.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mikescode.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mikescode.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mikescode.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mikescode.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mikescode.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mikescode.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikescode.wordpress.com&blog=2877594&post=3&subd=mikescode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mikescode.wordpress.com/2008/02/16/exporting-a-mysql-table-to-excel-xls-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fee2b0cffb1f69242d7aae4076f75b17?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mike</media:title>
		</media:content>
	</item>
	</channel>
</rss>