Batakj
Batakj

Reputation: 12743

How to execute t-sql script using java?

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.

Upvotes: 1

Views: 3671

Answers (1)

Matt Luongo
Matt Luongo

Reputation: 14829

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.

Upvotes: 2

Related Questions