I have found the following links for executing script using java i.e. How to execute sql-script file in java? . But it is specific to postgre and mysql.
I am looking for same solution for executing script in Sql server 2005. I am not acquinted with SQL Command Line. I have to make a batch containing series of calling different method of a class which also include the executing the script in the middle of batch. I googled it. But, i am not able to find the solution for my problem. Thanks in advance.
Instead of a dirty solution like stored procedures, why don't you try the one found in How to execute sql-script file in java?? Instead of the psql command, try
Sqlcmd -Shostname\dbname -U username -p password -i sqlscriptfile
This should connect to the database, using SQL Server authentication, and run the script you specify. Pablo's answer in the referenced question should do the rest. Depending on your path, you might need to alter the 'Sqlcmd' bit to reflect the location of the program on your machine.
To learn more about the command line utility, check out this article on databasejournal.com.
Related
Working with Unix server... My requirement is to read the name of the file that is there at /a/b/c/node01/d.ear location on a Unix server and I have do the same through a java program. The problem is that the directory a is a restricted directory and is accessible only to certain users. On the Unix side, I first issue a become command like become a, then supply the password and then using cd command, I reach the d.ear directory and then get to see the name of the file.
How do I do all of this via a Java program?
I don't mind if my Java program calls a shell script that accesses the restricted directory and then reach d.ear and fetch the name of the file and returns the same to the java program. Do we have a way of doing this? Maybe issuing the become command inside the script which is called from the Java program and the password which is asked after become command is supplied as a parameter while calling the script???
Is this approach doable? I am very new to Unix commands and JSch library. Kindly provide the code or any other alternate solutions...
Thanks!!!
As I have suggested you already, your become command seems to behave the same way (from an interface/API point of view) as common *nix su or sudo.
So, use the same solution as for those. There are many questions on Stack Overflow covering use of su/sudo with JSch.
There's even an official JSch example Sudo.java:
http://www.jcraft.com/jsch/examples/Sudo.java.html
In short:
Execute become command
Feed a password to its input
Assuming the become starts a new shell (as su or sudo do), you feed the commands to be executed in the elevated environment to become input (the same was as the password).
Basically what I need is, I want to run a shell script present on remote linux machine along with that I need to pass one list collection as an argument to that script and fetch the result back in java code to print and store in object.
I think Old post in stackoverflow will help you on this
How to run Unix shell script from Java code?
Run shell script from Java Synchronously
I am using Runtime.exec() to run an executable file. I have been researching and found out that there could be security concerns when using this in a application. Are there any security concerns when using Runtime.exec() to run an executable file?
#Jeanne Boyarsky: Apparently you cannot inject into Runtime.exec() in the way you mentioned, unless Runtime.exec() first spawns a shell (cmd.exe on Windows or sh/bash/csh/ksh on Linux) to run the command. Here is a good link which talks about this.
I wrote a small program to test this out. It takes a command as user input. So if I enter 'pwd' (Linux system) it will print the current directory to the System Console. This works perfectly.
If however I try and run two commands, as is permitted in Linux, such as pwd;id it throws an Exception straight away. The Exception thrown is as follows.
javax.faces.el.EvaluationException: java.io.IOException: Cannot run program "pwd;ls": error=2, No such file or directory
Having said that though there is a situation when this can be a problem. If I have a piece of code as follows:
Process proc = runtime.exec(cmd);
... the user could provide an input of sh -c pwd;id, thus causing a shell to run and then chaining commands inside it.
So in short, best to not use Runtime.exec() if you can help it. If you MUST use it, make sure you canonicalize all user input and allow only specific characters and commands.
Here is a good read on how to write secure code.
The biggest one I can think of is Command Injection. YOu want to whitelist what gets run so someone can't run "rm /" via your Runtime.exec. There are more ways for this to happen than you might think. For example what if a "directory" name is passed in as "foo; rm -r ; ls".
Another one - if this is a web application - is that the permissions for the application (and therefore your Runtime.exec() command line aren't the same as what the person hitting the web page has. Which means the person could delete your Tomcat or insert data into a database or ...
I have a .sql file that was created using the MS SQL Management Studio Script Wizard. It contains the entire database schema including views, stored procedures, tables, indexes, etc.
I’m looking for a way to automate the process of creating the database with ColdFusion or Java or even .NET.
I’ve tried using CFQUERY but it throws all kinds of errors with the .sql file.
I’ve tried using ANT with sqljdbc4.jar but it also fails on the first line with a “[sql] Failed to execute: BEGIN TRAN” com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ''.
I highly doubt you can use the scripts generated by the wizard outside of an MS tool. The wizard makes copious use of GO which is not standard T-SQL. Also, commands like CREATE PROCEDURE need to be the first statement within the batch. To run the wizard scripts, you need to use an MS tool like sqlcmd.exe. With the correct permissions, you should be able to run sqlcmd.exe from <cfexecute>.
You should be able to execute all the SQL within a <cfquery assuming you have the appropriate permissions.
We generate all our databases like this with CREATES etc in a ColdFusion <cfquery
Do you have the appropriate permissions, what errors are you getting.
If you're using ORM, you may set this.ormsettings.sqlscript to point to your .sql file in Application.cfc and it'll be run on ORMReload() when this.ormsettings.dbcreate is set to dropcreate.
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSED380324-6CBE-47cb-9E5E-26B66ACA9E81.html
Good luck
I am calling mysql.exe from Java to load a database. Because the process just hangs, I need to create a command file and pass in the username and password.
Example contents of command.bat:
mysql --user="%1" --password="%2" mydatabase < myscript.sql
The problem is that I cannot see the output of the mysql command to see if there were any errors. They display on the command line, but I cannot seem to capture them in a file for parsing or an InputStream.
How can I see the output of the mysql command?
NOTE: Calling mysql.exe directly from Java hangs because the mysql does not appear to be sending the information to the buffer.
NOTE: We are using mysql.exe instead of JDBC because we need to update things like triggers. In order to submit all the statements to the db, we would need to parse all the commands and pass them in one at a time.
NOTE: This is a running MySQL database that needs to be upgraded.
If you are only accessing this database from within Java, a better solution would be Connector/MXJ. This will allow you to simply make a well formed JDBC call, and the library will take care of the database startup for you automatically.
Basically, the jar file contains in instance (or, for multiple platforms, instances) of the mysql server executable. It also contains a skeleton where you can load prepopulated data for your database.
The first time you access the JDBC connection, it will pull the proper mysql server out of the jar, and create the database in the current directory (using the prepopulated data from above). Any changes from that point on will be persistent, as expected.
Here's some more info:
Launching via JDBC
Launching via a Java Object
Not sure why you can't view its output. Try making it output to a file, then use Java to read from the file at the same time.
I haven't tried this myself.
You should look at Process Builder. It lets you get a handle on the input/oputput and error stream.
The problem is that I cannot see the output of the mysql command to see if there were any errors. > They display on the command line, but I cannot seem to capture them in a file for parsing or an
InputStream.
Use 2>yourfilename
E.g.
mysql --user="%1" --password="%2" mydatabase < myscript.sql 2>myoutput.txt
should capture the STDERR (to where MySQL.exe sends its output here)
E.g.
mysql --user="%1" --password="%2" mydatabase < myscript.sql >myoutput.txt 2>&1
will capture both STDOUT and STDERR in the same file (=myoutput.txt).