Installing and using Apache Derby with Eclipse

Here is the code in the Video.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class Test {
	
	public static void main(String[] args) {

        Connection conn = null;
	    PreparedStatement prestat = null;
	    ResultSet pw = 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");
	        pw = prestat.executeQuery();
	        
	        while (pw.next())
	        {
	        	System.out.println(pw.getInt(1) + ", " + pw.getString(2));
	        }
	        
	        pw.close();
	        pw = null;
	        prestat.close();
	        prestat = null;
	        conn.close();
	        conn = null;

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

	}
}

I have added a post on Packaging Java application with Apache Derby as JAR Executable using Eclipse, which is a continuation of this post. Do check it.

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

7 Responses to Installing and using Apache Derby with Eclipse

  1. punit says:

    please accept a request to make a video for how to create installers in java…

  2. Jamil says:

    Thank you for the post. It was really helpful. Please how do I attach the java script code, like the one above to an existing html file. For example, if I have a html page where members can register and a java file to manipulate my data base. How do I get the two together?
    Thank you once more.

    • vinayakgarg says:

      I wrote the code with desktop application in mind. I don’t have experience with JSP, which will allow you to execute java as server side language. And Java script is different from java. For manipulating database, you need to learn JSP, and for client side script, you need javascript. A good place to learn javascript is http://www.w3schools.com/js/.

  3. Igor horta correa says:

    I did every thing u said but when i click on the project and try to add apache derby nature it apears a error mensage saying : Error adding derby jars to the project:
    org.eclipse.ui.internal.workbenchWindow cannot be cast to:
    org.eclipse.jface.window.aplicationWindow.

    Please Help me!! and i am sorry for my english

  4. nivar says:

    can you let us know which eclipse version are you using?

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