TechnoBuzz

A Techno Blog, mainly about Java

Derby Again

I have used derby a while back. Several years later, I wanted to get it installed again on my machine.

In short, Derby is the Java Database that IBM contributed to the open source community. It was known then as cloudscape.

I start off by getting the bin download from the derby database

create DERBY_HOME environment variable : C:\JAVA\derby10\db-derby-10.2.2.0-bin\
Make sure I have my JAVA_HOME environment variable properly set
add to the PATH: C:\JAVA\derby10\db-derby-10.2.2.0-bin\bin\

from the command line:

>sysinfo

>ij

ij is the derby sql client command line

ij>connect ‘jdbc:derby://localhost:1527/mydb;
create=true;traceFile=trace.out;user=user1;password=secret4me’;

C:\JAVA\derby10\db-derby-10.2.2.0-bin\bin>

setNetworkClientCP.bat

C:\JAVA\derby10\db-derby-10.2.2.0-bin\demo\programs\simple>set CLASSPATH=.;%DERB
Y_HOME%\lib\derby.jar;%DERBY_HOME%\lib\derbynet.jar;%DERBY_HOME%\lib\derbyclient
.jar;%DERBY_INSTALL%\lib\derbytools.jar

C:\JAVA\derby10\db-derby-10.2.2.0-bin\frameworks>startNetworkserver.bat

C:\JAVA\derby10\db-derby-10.2.2.0-bin\demo\programs\simple>java org.apache.derby
.tools.sysinfo -cp embedded SimpleApp.class
FOUND IN CLASS PATH:

Derby embedded engine library (derby.jar)
C:\JAVA\derby10\db-derby-10.2.2.0-bin\lib\derby.jar

user-specified class (SimpleApp)
C:\JAVA\derby10\db-derby-10.2.2.0-bin\demo\programs\simple

SUCCESS: All Derby related classes found in class path.

get ibm jars for derby
C:\JAVA\derby10\db-derby-10.2.2.0-bin\demo\programs\simple>set CLASSPATH=.;%DERB
Y_HOME%\lib\db2jcc.jar;%DERBY_HOME%\lib\db2jcc_license_c.jar

C:\JAVA\derby10\db-derby-10.2.2.0-bin\frameworks>startNetworkserver.bat
C:\JAVA\derby10\db-derby-10.2.2.0-bin\demo\programs\simple>java SimpleApp jccjdb
cclient
SimpleApp starting in jccjdbc mode.
Loaded the appropriate driver.
Connected to and created database derbyDB
Created table derbyDB
Inserted 1956 Webster
Inserted 1910 Union
Updated 1956 Webster to 180 Grand
Updated 180 Grand to 300 Lakeshore
Verified the rows
Dropped table derbyDB
Closed result set and statement
Committed transaction and closed connection
SimpleApp finished

/* the default framework is embedded*/
public String framework = “embedded”;
public String driver = “org.apache.derby.jdbc.EmbeddedDriver”;
public String protocol = “jdbc:derby:”;

public static void main(String[] args)
{
new SimpleApp().go(args);
}

void go(String[] args)
{
/* parse the arguments to determine which framework is desired*/
parseArguments(args);

System.out.println(“SimpleApp starting in ” + framework + ” mode.”);

try
{
/*
The driver is installed by loading its class.
In an embedded environment, this will start up Derby, since it is not already running.
*/
Class.forName(driver).newInstance();
System.out.println(“Loaded the appropriate driver.”);

Connection conn = null;
Properties props = new Properties();
props.put(“user”, “user1”);
props.put(“password”, “user1”);

/*
The connection specifies create=true to cause
the database to be created. To remove the database,
remove the directory derbyDB and its contents.
The directory derbyDB will be created under
the directory that the system property
derby.system.home points to, or the current
directory if derby.system.home is not set.
*/
conn = DriverManager.getConnection(protocol +
“derbyDB;create=true”, props);

System.out.println(“Connected to and created database derbyDB”);

conn.setAutoCommit(false);

/*
Creating a statement lets us issue commands against
the connection.
*/
Statement s = conn.createStatement();

/*
We create a table, add a few rows, and update one.
*/

private void parseArguments(String[] args)
{
int length = args.length;

for (int index = 0; index < length; index++)
{
if (args[index].equalsIgnoreCase(“jccjdbcclient”))
{
framework = “jccjdbc”;
driver = “com.ibm.db2.jcc.DB2Driver”;
protocol = “jdbc:derby:net://localhost:1527/”;
}
if (args[index].equalsIgnoreCase(“derbyclient”))
{
framework = “derbyclient”;
driver = “org.apache.derby.jdbc.ClientDriver”;
protocol = “jdbc:derby://localhost:1527/”;
}
}
}

July 5, 2008 Posted by | SW Tools | Leave a comment