0

I want to create a script that initializes an apache derby database by opening ij and telling ij to run the setup.sql script.

I've managed to open ij, but how can i pass commands to the ij window from the script window? I belive this is possible with here documents in linux, but as far as i understood, that is not possible in windows command prompt.

Edit: I use this line to open ij.

"C:\Program Files\Java\jdk1.8.0_101\db\bin\ij.bat"

And i want this line to be executed inside the window opened by the previous command:

run 'setup.sql'
User892313
  • 197
  • 17

2 Answers2

1

It is dumb but should work... What my idea is that you just place other code in your batch file telling it to actually type it into the window. This is usually done with SendKeys. More on the Keys here.

So this would be the script:

@if (@CodeSection == @Batch) @then
@echo off
start "C:\Program Files\Java\jdk1.8.0_101\db\bin\ij.bat"
set SendKeys=CScript //nologo //E:JScript "%~f0"
timeout /t 3
%SendKeys% "run 'setup.sql'"
%SendKeys% "{ENTER}"
Goto:eof

@end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

Looks massive but is rather harmless ;)

@if (@CodeSection == @Batch) @then

and

@end

are required to seperate the batch-script from the magic that types keys; the JScript.

@echo off will simply block any output that the batch file would print for every command.
The next line starts your batch-file that itself starts another command-line application.
Setting the variable SendKeys is not 100% required but makes it easier to read and it saves the string to activate the JScript-Section (-> Keystroke simulation) to make it easier to use.
Change the timeout value based on your needs. I do not know how long it takes for ij to start.
The next line will stupidly write the command character for character (no worries... You will see nothing!).
Goto:eof is required to stop the batch-script before reading through the JScript and throwing errors all over.

I am not 100% sure as I do not have tested it but you might need to start your batch-file over the command-line itself like this:

start /min <your batch-file here>

This will start the file minimized to make sure it will type in the correct window.

Feel free to ask questions!

Community
  • 1
  • 1
geisterfurz007
  • 3,390
  • 5
  • 29
  • 46
  • This script seems to do the following: - open window 1 and window 2 - navigate window 2 to IJ location - let window one count down from 3 - write 'setup.sql' in window 2 after the countdown finished in window 1 Window 2 never starts IJ.bat, just navigates to the folder containing it. I belive i've found another workaround tho. Thanks alot for your time! Much appreciated! :) – User892313 Nov 23 '16 at 11:14
  • Oops I am an idiot! Forgot the Enter... Will quickly edit that. The countdown results from the Timeout placed in the script. Take it out and the countdown will be away. It was just to ensure ij started – geisterfurz007 Nov 23 '16 at 11:21
0
  1. Make a sql file and list all the commands that you want to run from ij.
    For example:
    1. I want to connect to the derby database.
    2. Then I want to run DDL script to create table
    3. Then I want to run DML script to insert data into tables.

So my sql file will look like this:

CONNECT 'jdbc:derby://<my_hostname>:1527/<database_name>;create=true;user=akshay;password=akshay';

RUN 'C:\Users\akshay_chopra\Downloads\db-script\derby\derby_ddl.sql';

RUN 'C:\Users\akshay_chopra\Downloads\db-script\derby\derby_dml.sql';

Let this sql file name be derby_config.sql

  1. Make a batch file to run ij tool and execute commands from derby_config.sql

Now, make a batch file and add the following lines:

@echo off
cls

java -cp D:\derby\db-derby-10.14.2.0-bin\db-derby-10.14.2.0-bin\lib\* org.apache.derby.tools.ij "C:\Users\akshay_chopra\Desktop\derby_config.sql"

cmd /k

The format of above command is

java -cp <derby-installation>\lib\* org.apache.derby.tools.ij "<sql-file-location- created-in-step-1>"

D:\derby\db-derby-10.14.2.0-bin\db-derby-10.14.2.0-bin\lib\* represents the jars where it can find ij java class. These jars can be found in \lib folder. The * in the end represent that it will search all the jar files to find ij class.

  1. Run this batch file
    When you will run this batch file, it will open the ij tool and will execute the commands that you have written in the sql file created in step-1.
Akshay Chopra
  • 465
  • 5
  • 8