Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

JDBC PreparedStatement With MySQL

//JDBC program to insert a record into customer table

Here we are using MySQL database environment. In MySQL we have created a customer table in customerdb database.

To create a database in MySQL;

Create database customerdb

To create a table:

create table customerdb.customer
(
cid varchar(200) primary key,
cname varchar(200) not null,
caddress varchar(200) not null
)


JdbcExample1.java
package com.silan;
import java.sql.*;
public class JdbcExample1 {

    public static void main(String[] args) throws Exception{

        //To load and register the Type-4 driver
        Class.forName("com.mysql.jdbc.Driver");

        //Establish the connection
        Connection
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/customerdb","root","silan");

        //Create the statement object
        PreparedStatement ps=con.prepareStatement("insert into customer values(?,?,?)");
        ps.setInt(1, 101);
        ps.setString(2,"John De");
        ps.setString(3, "BBSR");

        //Execute sql statement
        int k=ps.executeUpdate();

        System.out.println(k+"row inserted");

        //Close the connection
        con.close();
    }
}

Output:

1row inserted

We will go to MySQL environment and execute select * from customerdb.customer Then we will see the following output:

img

Explanation:

Step-1 : To load and register the driver : Class.forName(com.mysql.jdbc.Driver);

When there is a static method named as forName( ) invoking by a class named as Class and forName( ) taking an argument that is Type-4 driver class name for MySQL database then Type-4 driver is loaded and registered.


Step-2 : Establish the connection

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/customerdb","root","silan");

The Connection object is representing establishment of Connection. Then question is how to get Connection object. The answer is when getConnection( ) is invoking by DriverManager class, it is returning Connection object. Here getConnection( ) is a static method and it takes three arguments. First argument is url that is
jdbc:mysql://localhost:3306/customerdb
Here jdbc : protocol
mysql: subprotocol our database name
localhost: database server name
3303: port no
customerdb : database name
second argument is username of MySQL that is root
third argument is password value that you have given at the time of MySQL installation. Here my password value is silan.


Step-3 : Create the statement object

PreparedStatement ps=con.prepareStatement("insert into customer values(?,?,?)");


When prepareStatement( ) is invoking by Connection object then it is returning PreparedStatement object.

PreparedStatement interface is used to execute parameterized SQL statement. So here prepareStatement( ) taking an argument that is required sql statement for our problem statement.

Then to assign value we have invoked setXXX( ) method.


Step-4: Execute SQL statement

Since here problem task is inserting a record into a table that is DML operation so executeUpdate( ) we have to call. This method returning an int value.

int k=ps.executeUpdate( );


Step-5: Close the connection

con.close( );

when we establish the connection , many resources are allocating by the operating system. After execution we have to release the resources by invoking close( ), otherwise memory leaking may arise.


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc







 PreviousNext