0

For example I have this SQL script and I want to exectute it from java:

USE msdb ;  
GO

EXEC dbo.sp_add_operator  
    @name = N'Dan Wilson',  
    @enabled = 1,  
    @email_address = N'danwi',  
    @pager_address = N'5551290AW@pager.Adventure-Works.com',  
    @weekday_pager_start_time = 080000,  
    @weekday_pager_end_time = 170000,  
    @pager_days = 62 ;  
GO
Ilyes
  • 14,640
  • 4
  • 29
  • 55
Renato
  • 1
  • [Like so](https://stackoverflow.com/questions/2071682/how-to-execute-sql-script-file-in-java) or perhaps [Using JDBC](https://stackoverflow.com/q/1044194/6167855) – S3S Sep 15 '17 at 16:42
  • 7
    Possible duplicate of [How to execute sql-script file in java?](https://stackoverflow.com/questions/2071682/how-to-execute-sql-script-file-in-java) – MatSnow Sep 15 '17 at 16:43
  • Note that `GO` is not a T-SQL statement but a batch separator. Tools and utilities send the preceeding batch of statements when those are encountered but you'll need to do that yourself from Java, – Dan Guzman Sep 15 '17 at 21:08

2 Answers2

-1

What you're looking for is Java Database Connectivity (JDBC). Oracle provides a tutorial on their website.

https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html

Strikegently
  • 2,251
  • 20
  • 23
  • perhaps i need to explain more, when i execute the query i get the following: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'GO', what i do is put all the script in a String, then call the method executeQuery(String); – Renato Sep 15 '17 at 17:00
  • OP has a SQL-Server tag – Simon Sep 15 '17 at 21:34
-1

Well I need to know which database are you using. For oracle you can try:

import java.sql.* ;
String URL = "jdbc:sqlserver:thin:@amrood:1521:EMP";
String USER = "username";
String PASS = "password"
Connection conn = DriverManager.getConnection(URL, USER, PASS)
PreparedStatement pstmt = null;
String SQL = "dbo.sp_add_operator  @name = N'Dan Wilson',  
    @enabled = 1,  
    @email_address = N'danwi',  
    @pager_address = N'5551290AW@pager.Adventure-Works.com',  
    @weekday_pager_start_time = 080000,  
    @weekday_pager_end_time = 170000,  
    @pager_days = 62 ; ";
pstmt = conn.prepareStatement(SQL);
stmt.executeUpdate(); or stmt.executeQuery();
kranz
  • 5
  • 5