
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
Have Questions? Post them here!
- How to get Java Thread name Example [Program]
- Java 8: Get First and Last Date of the Week for Given Date
- Convert String to int in Java
- How to Get List of All Country Codes in Java Using Locale Class
- Convert Multidimensional Array toString In Java
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Create a Zip file using Java Code programmatically
- [Fix] java.net.MalformedURLException: unknown protocol
- [fix] Java JDBC ConnectException: Connection refused
- Read Json File and Convert to Java Object using Jackson
- list of jars required for hibernate 4.x.x
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java: The value of the local variable string is not used
- [fix] URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) IntelliJ
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Run SQL Script file using Java JDBC Code Example
- Remove Trailing zeros BigDecimal Java
- Java JDBC IN Clause Example with PreparedStatement MySQL
- Convert Java List to Json String using Jackson
- Java 8 foreach loop code examples
- error: file not found: HelloWorld.java
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Struts 2 Hello World Example in Eclipse
- Step-by-Step: Setting up Docker + Ubuntu Linux + Git + GitHub Tutorial - Git
- Bash Hello World! Script Tutorial - Bash
- How to delete SharePoint Online List Item using REST API - SharePoint
- How to write JSON file in Python Program - Python
- Add Current Date and Time in Notepad++ - NotepadPlusPlus
- Sublime Text 3 spell check shortcut - Sublime
- How to query database table names [MySQL/Oracle/SQL Server] - MySQL
- JavaScript: Generate Random Numbers between 1 and 3 - JavaScript