- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to run .SQL script using JDBC?
A database script file is a file that contains multiple SQL quries separated from each other. Usually, these files have the .sql extention.
Running .sql script files in Java
You can execute .sql script files in Java using the runScript() method of the ScriptRunner class of Apache iBatis. To this method you need to pass a connection object.
Therefore to run a script file −
- Register the MySQL JDBC Driver using the registerDriver() method of the DriverManager class.
- Create a connection object to establish connection with the MySQL database using the getConnection() method.
- Initialize the ScriptRunner class of the package org.apache.ibatis.jdbc.
- Create a Reader object to read the script file.
- Finally, execute the script using the runScript(reader) method.
Example
Let us create a script file with name sampleScript.sql copy the following contents init. This script creates a table with name cricketers_data in MySQL database an populates it with five records.
CREATE DATABASE exampleDB; use exampleDB; CREATE TABLE exampleDB.cricketers_data( First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); insert into cricketers_data values('Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into cricketers_data values('Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into cricketers_data values('Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into cricketers_data values('Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into cricketers_data values('Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); select * from mydatabase.cricketers_data;
Add the following maven dependency (for the jar file mybatis-3.4.1.jar) to your pom.xml file −
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency>
Example
Following JDBC program executes the sampleScript.sql file.
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.Reader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import org.apache.ibatis.jdbc.ScriptRunner; public class RunningScripts { public static void main(String args[]) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/talakai_noppi"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Initialize the script runner ScriptRunner sr = new ScriptRunner(con); //Creating a reader object Reader reader = new BufferedReader(new FileReader("E:\sampleScript.sql")); //Running the script sr.runScript(reader); } }
Output
Connection established...... CREATE DATABASE exampleDB use exampleDB CREATE TABLE exampleDB.cricketers_data( First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ) insert into cricketers_data values('Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India') insert into cricketers_data values('Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica') insert into cricketers_data values('Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka') insert into cricketers_data values('Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India') insert into cricketers_data values('Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India') select * from mydatabase.cricketers_data First_Name Last_Name Year_Of_Birth Place_Of_Birth Country Shikhar Dhawan 1981-12-05 Delhi India Jonathan Trott 1981-04-22 CapeTown SouthAfrica Lumara Sangakkara 1977-10-27 Matale Srilanka Virat Kohli 1988-11-05 Delhi India Rohit Sharma 1987-04-30 Nagpur India
- Related Articles
- How to run SQL script in MySQL?
- Performing Null check using HANA SQL Script
- How to sql insert items from a list or collection in to table using JDBC?
- How to run a PowerShell script from the command prompt?
- Run Python script from Node.js using child process spawn() method?
- How to echo print statements while executing an SQL script?
- How to process SQL statements with JDBC explain with an example?
- Run a Script on Startup in Linux
- What is JDBC SQL Escape Syntax Explain?
- Run SQL file in MySQL database from terminal?
- How to run splash using Docker toolbox?
- SQL Script vs Graphical Calcualtion views in SAP HANA
- How to make loops run faster using Python?
- How to insert images in Database using JDBC?
- How to drop a database using JDBC API?

Advertisements