Packaging Java application with Apache Derby as JAR Executable using Eclipse

This Tutorial is continuation of tutorial on Installing and using Apache Derby with Eclipse. So check it out, if you haven’t already.

The code I used in the video.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class Test {

	public static void main(String[] args) {

        Connection conn = null;
	    PreparedStatement prestat = null;
	    ResultSet result = null;

	    try {
	        Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
	    } catch (InstantiationException e) {
	        e.printStackTrace();
	    } catch (IllegalAccessException e) {
	        e.printStackTrace();
	    } catch (ClassNotFoundException e) {
	        e.printStackTrace();
	    }
	    try {
	        conn = DriverManager.getConnection("jdbc:derby://localhost:1527/MyDbTest;");

	        prestat = conn.prepareStatement("SELECT * FROM testTable");
	        result = prestat.executeQuery();

	        StringBuilder builder = new StringBuilder();
	        while (result.next())
	        {
	        	//System.out.println();
	        	builder.append(result.getInt(1) + ", " + result.getString(2));
	        	builder.append('\n');
	        }

	        JOptionPane.showMessageDialog(null, builder.toString());

	        result.close();
	        result = null;
	        prestat.close();
	        prestat = null;
	        conn.close();
	        conn = null;

	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    finally{
	        if (result != null){
	            try { result.close();} catch (SQLException e){;}
	            result = null;
	        }
	        if (prestat != null){
	            try { prestat.close();} catch (SQLException e){;}
	            prestat = null;
	        }
	        if (conn != null){
	            try {conn.close();} catch(SQLException e) {;}
	        conn = null;
	        }
	    }

	}

}

Also check my related post Starting Apache Derby Embedded in JAR Programmaticaly

Advertisements
This entry was posted in Programming and tagged , . Bookmark the permalink.

3 Responses to Packaging Java application with Apache Derby as JAR Executable using Eclipse

  1. sai says:

    It works good in eclipse IDE but when i export it as a jar, no output

  2. Pingback: Installing and using Apache Derby with Eclipse | Tech Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s