How to Execute SQL File with Java using File and IO Streams?
Last Updated :
28 Apr, 2025
In many cases, we often find the need to execute SQL commands on a database using JDBC to load raw data. While there are command-line or GUI interfaces provided by the database vendor, sometimes we may need to manage the database command execution using external software. In this article, we will learn how to execute an SQL file containing thousands of comments in just a few seconds, without relying on any external library or dependency. This process requires only a minimum knowledge of IO streams and JDBC connectivity.
Understanding the Concepts
Before we dive into the article, let's familiarize ourselves with the necessary concepts:
- Class name: The name of the JDBC driver class.
- Connection URL: The URL to connect to the database.
- Username and Password: Credentials for the database connectivity.
- SQL file: The file containing SQL commands without any syntax errors.
- Database connector jar file: In order to use JDBC, you need to include the appropriate database connector jar file in your project. The JDBC driver is a software component that allows Java applications to interact with the database.
Creating the SQLExecutor Class
To execute SQL commands efficiently, we'll create a class named "SQLExecutor" responsible for connecting to JDBC and executing the SQL file. The class will have three instance properties: URL, username, and password. Upon object initialization, the Driver class will be loaded, and any errors during this process will result in the constructor throws a "ClassNotFoundException.".
Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.*;
public class SQLExecutor {
private final String url;
private final String userName;
private final String password;
public SQLExecutor(String className, String url, String userName, String password)
throws ClassNotFoundException {
Class.forName(className);
this.url = url;
this.userName = userName;
this.password = password;
}
}
And this class contains the following methods.
1. Getting a Connection:
We'll implement a utility method, "getConnection," that returns a new database connection using the provided URL, username, and password.
Java
private Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, userName, password);
}
2. Printing Metadata:
We'll create another method, "printMetaData," to display the metadata about the database connection, such as the database name, database version, driver name, and version.
Java
private void printMetaData(Connection connection)
throws SQLException
{
DatabaseMetaData metaData = connection.getMetaData();
String format = "\nDatabase metadata\n"
+ "Database name : %s\n"
+ "Database version : %s\n"
+ "Database driver name : %s\n"
+ "Database driver version : %s\n\n";
System.out.printf(format,
metaData.getDatabaseProductName(),
metaData.getDatabaseProductVersion(),
metaData.getDriverName(),
metaData.getDriverVersion());
}
3. Executing the SQL File:
Now, let's move on to the "executeFile" method. This method accepts the file path as a parameter and executes the SQL commands present in the file. It creates a buffered reader for the file, establishes a new database connection, and gets a statement from the connection. it uses the try with resources block to close the resources automatically.
Java
public void executeFile(String path)
{
try (FileReader reader = new FileReader(path);
// Wrap the FileReader in a BufferedReader for
// efficient reading.
BufferedReader bufferedReader
= new BufferedReader(reader);
// Establish a connection to the database.
Connection connection = getConnection();
// Create a statement object to execute SQL
// commands.
Statement statement
= connection.createStatement();) {
printMetaData(connection);
System.out.println("Executing commands at : "
+ path);
StringBuilder builder = new StringBuilder();
String line;
int lineNumber = 0;
int count = 0;
// Read lines from the SQL file until the end of the
// file is reached.
while ((line = bufferedReader.readLine()) != null) {
lineNumber += 1;
line = line.trim();
// Skip empty lines and single-line comments.
if (line.isEmpty() || line.startsWith("--"))
continue;
builder.append(line);
// If the line ends with a semicolon, it
// indicates the end of an SQL command.
if (line.endsWith(";"))
try {
// Execute the SQL command
statement.execute(builder.toString());
// Print a success message along with
// the first 15 characters of the
// executed command.
System.out.println(
++count
+ " Command successfully executed : "
+ builder.substring(
0,
Math.min(builder.length(), 15))
+ "...");
builder.setLength(0);
}
catch (SQLException e) {
// If an SQLException occurs during
// execution, print an error message and
// stop further execution.
System.err.println(
"At line " + lineNumber + " : "
+ e.getMessage() + "\n");
return;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Using the SQLExecutor Class
To utilize the SQLExecutor class, we'll create a main method that takes input from the user and initiates the execution process. The main method provided above allows users to interactively enter the required input, including the class name, connection string, username, password, and file path. It then proceeds to execute the SQL commands from the specified file.
Java
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("*** Welcome to SQl file executer ***\n");
System.out.print("Enter Class Name : ");
String className = scanner.nextLine();
System.out.print("Enter Connection string : ");
String url = scanner.nextLine();
System.out.print("Enter username : ");
String user = scanner.nextLine();
System.out.print("Enter password : ");
String password = scanner.nextLine();
try {
SQLExecutor executor = new SQLExecutor(
className, url, user, password);
System.out.print("Enter file path : ");
executor.executeFile(scanner.nextLine());
scanner.close();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
The SQLExecutor class can be utilized independently as a standalone tool or included in a project. Let's describe the use cases for both scenarios:
Standalone Use Case
When used independently as a standalone tool, the SQLExecutor class can be compiled against the JDBC driver's jar file. This allows to execute SQL commands without the need for an entire project setup.
JDK version - 18 or above
java -cp path/to/jar SQLExecuter.java
Inclusion in a Project:
In this use case, the SQLExecutor class is included as part of a larger Java project. we can leverage the class's functionality when executing SQL commands related to that specific project.
Conclusion
By utilizing the power of Java's IO streams and JDBC connectivity, we can now efficiently execute a bunch of SQL commands in just seconds. This approach proves to be very helpful for tasks like creating tables, inserting values, and executing some predefined definitions.
However, it's essential to keep in mind any syntax differences between databases, such as date and time value formats or varying support for certain databases. Happy coding!
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read