Pradeep's Blog

Google

Monday, May 01, 2006

Using Derby from Command Line

Is there a way to add tables into Derby from command line? The answer is yes. Let’s have a look at how it can be done. Derby provides us tools (java programs), that enable us to create, delete, select and update tables in the database from the command line. Given below is a batch file that enables us to access the database.

***********Batch File *****************
@echo off
rem -- Run Derby ij --
set LIBPATH=C:\Sun\AppServer\derby\lib
java -classpath
"%LIBPATH%\derbytools.jar;%LIBPATH%\derby.jar;%LIBPATH%\derbyclient.jar" org.apache.derby.tools.ij %1.
**********End of Batch File ************
This batch file actually sets that classpath and runs a java program with the argument that we pass along. This argument is a file containing the SQL command that we would like to run. Given below is a text file containing a SQL create statement.
*********Text File ********************
connect 'jdbc:derby://localhost/testdb;create=true' user 'app' password 'app';
CREATE TABLE test_table (
col1 VARCHAR(24) NOT NULL,
col2 VARCHAR(24),
col3 VARCHAR(24),
CONSTRAINT pk_test_table PRIMARY KEY (col1)
);
disconnect;
********* End Of Text File **************

The first and last line of the text file enable us to connects and disconnect with Derby. Any SQL statements that you want to run can be written in the middle. Considering that you have saved the batch file as runDerby.bat & the text file as sql.txt. You can run the SQL statement in the text file on Derby as follows.

Prompt> runDerby sql.txt

For more information look here.

1 Comments:

  • What a great site, how do you build such a cool site, its excellent.
    »

    By Anonymous Anonymous, at 7:53 PM  

Post a Comment

<< Home