BAEL-6674 Added code for mybatis sqlscript (#14487)
This commit is contained in:
parent
a90532946c
commit
891652322d
@ -18,9 +18,16 @@
|
|||||||
<artifactId>mybatis</artifactId>
|
<artifactId>mybatis</artifactId>
|
||||||
<version>${mybatis.version}</version>
|
<version>${mybatis.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>${h2database.version}</version>
|
||||||
|
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
<h2database.version>2.2.220</h2database.version>
|
||||||
<mybatis.version>3.2.2</mybatis.version>
|
<mybatis.version>3.2.2</mybatis.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
118
mybatis/src/main/java/com/baeldung/mybatis_sqlscript/Main.java
Normal file
118
mybatis/src/main/java/com/baeldung/mybatis_sqlscript/Main.java
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
package org.baeldung.mybatis_sqlscript;
|
||||||
|
|
||||||
|
import org.apache.ibatis.jdbc.ScriptRunner;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
// JDBC driver name and database URL
|
||||||
|
static final String JDBC_DRIVER = "org.h2.Driver";
|
||||||
|
static final String DB_URL = "jdbc:h2:mem:testDB";
|
||||||
|
|
||||||
|
// Database credentials
|
||||||
|
static final String USER = "sa";
|
||||||
|
static final String PASS = "";
|
||||||
|
private static Connection conn;
|
||||||
|
private static Statement stmt;
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
try {
|
||||||
|
//Register JDBC driver
|
||||||
|
Class.forName(JDBC_DRIVER);
|
||||||
|
|
||||||
|
//Open a connection
|
||||||
|
System.out.println("Connecting to database...");
|
||||||
|
conn = DriverManager.getConnection(DB_URL, USER, PASS);
|
||||||
|
stmt = conn.createStatement();
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
//Handle errors for Class.forName
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Enter full path of the file: ");
|
||||||
|
String filePath= sc.next();
|
||||||
|
Path path = Paths.get(filePath);
|
||||||
|
|
||||||
|
System.out.println("Press 1: Execute SQL Script Using Plain Java: ");
|
||||||
|
System.out.println("Press 2: Execute SQL Script Using Apache iBatis: ");
|
||||||
|
List<String> sqlLines=Files.readAllLines(path);
|
||||||
|
int choice = sc.nextInt();
|
||||||
|
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
// Running SQL Script using Plain Java
|
||||||
|
for (String sql : sqlLines) {
|
||||||
|
System.out.println("Query: " + sql);
|
||||||
|
|
||||||
|
if (sql.contains("SELECT")) {
|
||||||
|
ResultSet rs = stmt.executeQuery(sql);
|
||||||
|
System.out.print("ID" + "\t" + "Name" + "\t" + "Surname" + "\t" + "Age");
|
||||||
|
System.out.println("");
|
||||||
|
// Extract data from result set
|
||||||
|
while (rs.next()) {
|
||||||
|
// Retrieve by column name
|
||||||
|
int id = rs.getRow();
|
||||||
|
int age = rs.getInt("age");
|
||||||
|
String name = rs.getString("name");
|
||||||
|
String surname = rs.getString("surname");
|
||||||
|
|
||||||
|
// Display values
|
||||||
|
System.out.print(id + "\t" + name + "\t" + surname + "\t" + age);
|
||||||
|
System.out.println("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
stmt.execute(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
ScriptRunner scriptExecutor = new ScriptRunner(conn);
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(filePath));
|
||||||
|
scriptExecutor.runScript(reader);
|
||||||
|
reader.close();
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
catch(SQLException se)
|
||||||
|
{
|
||||||
|
//Handle errors for JDBC
|
||||||
|
se.printStackTrace();
|
||||||
|
} catch(Exception e)
|
||||||
|
{
|
||||||
|
//Handle errors for Class.forName
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
System.out.println("Reached End of Code!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
10
mybatis/src/main/resources/script.sql
Normal file
10
mybatis/src/main/resources/script.sql
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
CREATE TABLE CUSTOMER (id number, name varchar(20), surname varchar(20), age number);
|
||||||
|
INSERT INTO CUSTOMER VALUES (1, 'John', 'Doe', 25);
|
||||||
|
INSERT INTO CUSTOMER VALUES (2, 'Jill', 'Johnson', 18);
|
||||||
|
INSERT INTO CUSTOMER VALUES (3, 'Jake', 'Peralta', 29);
|
||||||
|
INSERT INTO CUSTOMER VALUES (4, 'Jack', 'Ryan', 35);
|
||||||
|
UPDATE CUSTOMER SET AGE = 19 WHERE name = 'Jake';
|
||||||
|
SELECT * FROM CUSTOMER;
|
||||||
|
DELETE FROM CUSTOMER WHERE name='John';
|
||||||
|
SELECT * FROM CUSTOMER ORDER BY NAME;
|
||||||
|
SELECT * FROM CUSTOMER WHERE AGE > 18;
|
Loading…
x
Reference in New Issue
Block a user