Reputation: 245
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'
Upvotes: 0
Views: 3868
Reputation: 1253
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
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.
Upvotes: 0
Reputation: 5874
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!
Upvotes: 1