
If you have a SQL script file and you want to run it using Java code, you can do that using JDBC,
Script File: sqlScript.sqlSelect * from students;
Select * from students where student_name="Mike";
Select * from students where student_address="Ohio";
Example:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
public class JDBCReadScriptExample {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
String url ="jdbc:mysql://localhost:3306/my_uat";
String userName="root";
String password ="root123";
Connection connection = DriverManager.getConnection(url,userName,password);
Statement statement = connection.createStatement();
Path sqlScriptPath = Paths.get("/Users/code2care/my-projects-22/java-examples/sqlScript.sql");
List<String> sqlScript = Files.readAllLines(sqlScriptPath);
for(String script: sqlScript) {
ResultSet resultSet = statement.executeQuery(script);
System.out.println("Query: " + script);
while(resultSet.next()) {
System.out.println(resultSet.getInt(1)+"\t"+
resultSet.getString(2)+"\t"+
resultSet.getString(3)+"\t"+
resultSet.getString(4));
}
System.out.println("");
}
}
}
Output:
Query: Select * from students;
1 Mike 2001-07-22 00:00:00 New York City
2 Alex 2002-08-13 00:00:00 Chicago
3 Sam 2022-01-25 00:00:00 Ohio
4 Andy 2001-10-10 00:00:00 Japan
5 Andrew 1999-07-21 00:00:00 Ohio
6 Chris 1998-11-18 10:20:10 Ohio
7 Huge 2022-09-06 18:59:47 Ohio
Query: Select * from students where student_name="Mike";
1 Mike 2001-07-22 00:00:00 New York City
Query: Select * from students where student_address="Ohio";
3 Sam 2022-01-25 00:00:00 Ohio
5 Andrew 1999-07-21 00:00:00 Ohio
6 Chris 1998-11-18 10:20:10 Ohio
7 Huge 2022-09-06 18:59:47 Ohio
Facing issues? Have Questions? Post them here! I am happy to answer!
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
- Java C# foreach loop equivalent in Python Programming - Python
- Remove AirDrop Icon from macOS Menu Bar - MacOS
- How to check if a variable is set in Bash Script or Not - Bash
- Vertical align two div's in Bootstrap [HTML CSS] - Bootstrap
- Amp Hello World Example - AMP
- What is exit(0), exit(1) ... exit(8) codes in Python Programming - Python
- Java 8: Convert Stream to Array - Java
- [Microsoft Teams 4c7] Modern authentication failed here, but you'll still be able to sign in. Your status code is 4c7 error - Microsoft