« srpen 2005 »
PoÚtStČtSoNe
1
2
3
4
5
14
    
       
Today

Navigation

Speaker Profile
Roumen's Weblog
Login
Sun Bloggers
Technorati Profile

Am I popular?

Today's Page Hits: 525

Contacts

Name: Roman Strobl
E-mail: roman dot strobl
at sun dot com

NetBeans

Java Sites

Javalobby
The Server Side
Java Tips
Java Blogs
java.net
java.sun.com
java.cz

Blogs

NetBeans:
Geertjan
Brian Leonard
Gregg Sporar
Lukas Hasik
Ludovic Champenois
Vincent Brabant
Alexis Moussine-Pouchkine
Jullion-Ceccarelli
Tom Ball
Tim Boudreau
Jesse Glick
Petr Blaha
Ruth Kusterer
Jara Uhrik
xzajo
Jan Lahoda
James Branam
nbextras.org

Sun:
Kazem - bug cartoons ;-)
Tor Norbye
Romain Guy
James Gosling
Chief Gaming Officer
Bill Vass
Jim Grisanzio
Jonathan Schwartz

Planets:
Planet Netbeans
Planet Sun
Planet Eclipse

Other:
netbeans-blog.org
Joel Spolsky
Bruce Eckel

License info

Creative Commons License
This work is licensed under a Creative Commons License.

Recent Entries

Map of visits

Locations of visitors to this page
« Previous day (Aug 6, 2005) | Main | Next day (Aug 7, 2005) »
20050807 Neděle srpen 07, 2005
Setting Up Derby Database with NetBeans

Inspired by this article I thought I'd try out the Derby database. Here are steps how to install Derby and make it work from NetBeans:

1. Download Derby from here.
2. Unzip it to some directory.
3. Set the environmental variable DERBY_INSTALL to the directory where you unzipped Derby.
4. Check if you have environmental variable JAVA_HOME set properly (I've tried it with JDK 1.5.0_04). Note that Windows startup script is a bit broken, it doesn't work if JAVA_HOME contains spaces.
5. Start the network server from $DERBY_INSTALL/frameworks/NetworkServer/bin/startNetworkServer.
6. Start NetBeans and create a new project.
7. Add a library $DERBY_INSTALL/lib/derbyclient.jar through project libraries node.
8. Create a class with following code:

package derbytest;

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

public class DerbyTest {
    
    public DerbyTest() {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException cnfe) {
            System.err.println("Derby driver not found.");
        }
        try {
            Connection conn = DriverManager.getConnection("jdbc:derby://localhost/test;create=true;user=APP;pass=APP");
            Statement s = conn.createStatement();
            s.execute("CREATE TABLE test (id integer primary key not null, text varchar(32))");            
            s.execute("INSERT INTO test VALUES (1, 'hello world!')");
            s.execute("SELECT * FROM test");
            ResultSet rs = s.getResultSet();
            while (rs.next()) {
                System.out.println("Derby says: "+rs.getString("text"));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public static void main(String[] args) {
        new DerbyTest();
    }    
}

9. Run the class. You should be connected and see a hello world message from Derby.

The database browser in Resources | Database works fine as well with the network driver (but I didn't make it work with embedded driver, not sure why, I'll have to RTFM). The tricky part was to find out the default username and password. I know, I should read manuals, but I am just too lazy to do that.

BTW, there is a really great story behind Derby graduation I recommend to read.


    Disclaimer: The contents of my blog represent my personal opinions which may differ from official views of my employer, Sun Microsystems.