![Read SQL Script file using Java JDBC Read SQL Script file using Java JDBC](https://code2care.org/java/run-sql-script-file-using-java-jdbc-code-example/images/Read Sql Script file using Java JDBC.jpg)
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!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- How to create MD5 digest in Notepad++ - NotepadPlusPlus
- Word count in Sublime Text Editor - Sublime-Text
- Read/Write Java Object to a File - Java
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error - Android
- How to know the Docker Sandbox ID of a Container Network? - Docker
- Make a list bullet and numbering in TextEdit on Mac - MacOS
- Fix: Error: error:0308010C:digital envelope routines::unsupported NodeJs/Vue/React - JavaScript
- How to Show the Status Bar on Windows 11 New Notepad - Windows-11