Last active
November 25, 2021 06:11
-
-
Save anjijava16/1fac0e3ef2b54e2bc713419a5d42322b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.iwinner.runanysql; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.Reader; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import org.apache.ibatis.jdbc.ScriptRunner; | |
public class JdbUtils { | |
// Connect to your database. | |
// Replace server name, username, and password with your credentials | |
public static void main(String[] args) { | |
String connectionUrl = | |
"jdbc:sqlserver://iwinnerdb.database.windows.net:1433;database=welcomedb123;user=sqladminuser;password=ssssss"; | |
ResultSet resultSet = null; | |
try (Connection connection = DriverManager.getConnection(connectionUrl); | |
Statement statement = connection.createStatement();) { | |
// Create and execute a SELECT SQL statement. | |
// String selectSql = "SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer"; | |
ScriptRunner sr = new ScriptRunner(connection); | |
Reader reader = new BufferedReader(new FileReader(C:\\any_welcome.sql")); | |
//Running the script | |
sr.runScript(reader); | |
sr.setStopOnError(false); | |
System.out.println("#################### Start End Operation here "); | |
System.out.println("#####################################"); | |
String selectSql = "SELECT * from dbo.Locations_TAR"; | |
resultSet = statement.executeQuery(selectSql); | |
// Print results from select statement | |
while (resultSet.next()) { | |
System.out.println(resultSet.getString(1) + " " + resultSet.getString(2)+ " " + resultSet.getString(3)); | |
} | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
====================================== | |
any_welcome.sql | |
==================== | |
drop table [dbo].[Locations_TAR]; | |
drop table [dbo].[Locations_STG]; | |
CREATE TABLE [dbo].[Locations_STG]( | |
[LocationID] [int] NULL, | |
[LocationName] [varchar](100) NULL, | |
[Location Address] [varchar](100) NULL | |
); | |
CREATE TABLE [dbo].[Locations_TAR]( | |
[LocationID] [int] NULL, | |
[LocationName] [varchar](100) NULL, | |
[Location Address] [varchar](100) NULL | |
); | |
INSERT INTO Locations_STG values (1,'Richmond Road','afkak'),(2,'Brigade Road','afadf') ,(3,'Houston Street','USA '); | |
INSERT INTO Locations_TAR values (1,'Richmond Cross','n') ,(3,'Houston Street','afadf'), (4,'Canal Street' ,'IN USA'); | |
MERGE Locations_STG T | |
USING Locations_TAR S ON T.LocationID=S.LocationID | |
WHEN MATCHED THEN | |
UPDATE SET LocationName=S.LocationName;; | |
SELECT * from dbo.Locations_TAR; | |
================================================ | |
pom.xml | |
====================== | |
<dependencies> | |
<dependency> | |
<groupId>com.microsoft.sqlserver</groupId> | |
<artifactId>mssql-jdbc</artifactId> | |
<version>6.2.2.jre8</version> | |
</dependency> | |
<dependency> | |
<groupId>org.mybatis</groupId> | |
<artifactId>mybatis</artifactId> | |
<version>3.5.7</version> | |
</dependency> | |
</dependencies> | |
==================================================== | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment