Trending September 2023 # How Jdbc Insert Works With Examples? # Suggested October 2023 # Top 18 Popular |

Trending September 2023 # How Jdbc Insert Works With Examples? # Suggested October 2023 # Top 18 Popular

You are reading the article How Jdbc Insert Works With Examples? updated in September 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Jdbc Insert Works With Examples?

Introduction to JDBC Insert

Java Database Connectivity insert statement is used for inserting the rows inside the tables of relational databases such as MySQL, Oracle, etc. In this article, we will have a look at all the steps needed to execute the insert statement, its syntax, and the implementation of the same along with the help of an example. The prerequisites for using the JDBC insert statement is that you should have your database which is using such as MySQL or oracle in the running state and on and the other one is that you should make use of your own username and password while establishing a new connection with JDBC.

Start Your Free Software Development Course

Syntax and steps for JDBC insert:

The syntax used for the insert statement in JDBC is as shown below –

INSERT INTO name of table VALUES (comma separated column values to be inserted);

After running the INSERT command, we have to prepare the statement in order to run the same in JDBC. The complete steps that need to be executed in JDBC insert are as specified below –

Package Import – The importing of all the required packages that contain the classes of JDBC which are necessary for programming the database should be included at the beginning of the program itself. The basic package for JDBC that is required for the proper working is the* which will mostly provide all the basic requirements.

The JDBC driver registration – In order to open all the channels for the establishment of the connection, we will need to register the driver and initialize the same in the JDBC program.

Open the database connection – We will make use of the method DriverManager.getConnection() which will create the object of the connection. This object will represent the connection with the database from your application physically.

Query execution – The submission and building of the SQL statement needs an object which is of type Statement that will help us to insert the rows inside the relational database to which the application is connected to by using JDBC.

The environment cleans up – The resources that were consumed and used for the JDBC operations should be freed up so that the environment will be cleaned.

How JDBC insert works?

The Java Database connectivity is the API that is Application Database Connectivity in Java which helps in doing all kinds of operations and manipulations in the relational database from the application program by using the particular steps. The query statement used is similar to the database that you are using such as MySQL or Oracle. But you need to prepare the statement and follow the steps mentioned above in the sequential order to execute any of the query in JDBC.

The URL that you mention while establishing the connection which includes the uniform resource locator, the database used, the Ip address, and the host address that will be used for connecting the to specific database. The username and password should be valid and should belong to user who has the privilege to perform the operations that you are trying to do by using JDBC, in our case, INSERT statement.


Let us try to consider one example where we have one table named educba_writers that includes the information of all the writers for a particular organization. The contents of this table can be seen in the output of the below query statement –

SELECT * FROM [educba_writers];

The output is –

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class JDBCExample { static final String Url_of_database. = "jdbc:mysql://localhost/EDUCBA"; static final String Name_of_user = "payal"; static final String Associated_password = "[email protected]"; public static void main(String[] args) { try(Connection objectOfCon = DriverManager.getConnection(Url_of_database., Name_of_user, Associated_password); Statement query_statement = objectOfCon.createStatement(); ) { System.out.println("This will insert 4 rows into the educba_writers table"); String statement = "INSERT INTO educba_writers VALUES (200, 'Khushboo', 5460, '18/03/2023')"; query_statement.executeUpdate(statement ); statement = "INSERT INTO educba_writers VALUES (201, 'Mehek', 2600, '25/03/2023')"; query_statement.executeUpdate(statement ); statement = "INSERT INTO educba_writers VALUES (202, 'Sahil', 1500, '30/03/2023')"; query_statement.executeUpdate(statement ); statement = "INSERT INTO educba_writers VALUES(203, 'Karishma', 1600, '28/03/2023')"; query_statement.executeUpdate(statement ); System.out.println("Records have been inserted successfully!"); } catch (SQLException sample) { sample.printStackTrace(); } } }

The user’s name of the user that we used in the above code is payal and the password associated with the user is [email protected] Further, the name of the database that we used here is EDUCBA and we will perform all our operations on the localhost itself. All these things are necessary to specify while building the connection that is why we have stored all of them inside the variables initially and then used in our connection establishment statement.

After we try to compile the code shown above inside the file named chúng tôi using the below command –


We will get the output as shown below after the records are successfully inserted into the table.

In order to run the above program, we will have to run the following query –

java JDBCInsertExample

Which shows that our program worked as per our expectations. In order to confirm if the rows that we tried to inserted got inserted or not, we can check the contents of the educba_writers table by using the below query statement –

SELECT * FROM [educba_writers]

The output of the above query statement gives the following result that shows our inserted records present in the table –


The JDBC that is java database connectivity provides us with the INSERT command which can be used to add new rows in our database that we might be using from our java application program. In order to do so, we will have to follow all the above-mentioned steps in sequential format and as per mentioned syntax.

Recommended Articles

This is a guide to JDBC Insert. Here we discuss Introduction, syntax, parameters, How JDBC insert works, examples with code implementation respectively. You may also have a look at the following articles to learn more –

You're reading How Jdbc Insert Works With Examples?

Update the detailed information about How Jdbc Insert Works With Examples? on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!