Updated April 18, 2023
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.
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 import.java.sql.* 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.
Example
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 –
This table is present in our My SQL database and we want to insert some rows in this table by using our java application. We will try to use the JDBC for doing insert operation and will follow all the steps that are specified above. Our program becomes as shown here –
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 = "payal@123";
public static void main(String[] args) {
// Creating a new connection to database
try(Connection objectOfCon = DriverManager.getConnection(Url_of_database., Name_of_user, Associated_password);
Statement query_statement = objectOfCon.createStatement();
) {
// Query execution
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/2016')";
query_statement.executeUpdate(statement );
statement = "INSERT INTO educba_writers VALUES (201, 'Mehek', 2600, '25/03/2016')";
query_statement.executeUpdate(statement );
statement = "INSERT INTO educba_writers VALUES (202, 'Sahil', 1500, '30/03/2016')";
query_statement.executeUpdate(statement );
statement = "INSERT INTO educba_writers VALUES(203, 'Karishma', 1600, '28/03/2016')";
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 payal@123. 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 JDBCInsertExample.java using the below command –
javac JDBCInsertExample.java
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
The output of the above command will give the following output –
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 –
Conclusion
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 –