Java Program to Insert Details in a Table using JDBC
Last Updated :
21 Feb, 2023
Java Database Connectivity is basically a standard API(application interface) between the java programming language and various databases like Oracle, SQL, Postgres, SQL, etc. It connects the front end(for interacting with the users ) with the backend( for storing data).
Algorithm: Search/ Insert/ Delete/ Update in JDBC
In order to deal with JDBC standard 7 steps are supposed to be followed:
- Import the database
- Load and register drivers
- Create a connection
- Create a statement
- Execute the query
- Process the results
- Close the connection
Procedure:
- Creating a database irrespective of SQL or NoSQL. Creating a database using sqlyog and creating some tables in it and fill data inside it in order to search for the contents of a table. For example, the database is named “hotelman” and table names are “cuslogin” & “adminlogin”.
- Create a connection: Open any IDE where the java executable file can be generated following the standard methods. Creating a package further creating a class. Inside the package, open a new java file and type the below code for JDBC connectivity and save the filename with connection.java.
- Inserting details in a table using JDBC in the input sample image with parameters as follows
- “cuslogin” table has columns namely –
- name
- password
- email
- address
- phone
- id
- Need is to insert new details inside the “cuslogin” table.
Input sample Image:
3.1: Initialize a string with the SQL query as follows
String sql=”insert into cuslogin values(‘geeksforgeeks’,’gfg’,’geeks@email.com’,’flat 1′,’1239087474′,10)”;
3.2: Initialize the below objects of Connection class, PreparedStatement class(needed for JDBC ) and connect with the database as follows
Connection con=null;
PreparedStatement p=null;
con=connection.connectDB();
3.3: Now, add the SQL query of step 3.1 inside PrepareStatement and execute it as follows
p =con.prepareStatement(sql);
p.execute();
3.4: Open a new java file (here, its result.java) inside the same package and type the full code (shown below) for inserting the details of the customer in table “cuslogin”.
Note: Both the file’s viz result.java and connection.java should be inside the same package, else the program won’t give the desired output.
Implementation :
- Example 1 is Connection class of JDBC
- Example 2 is App(Main) class where connection class is used as calling object of Connection class in main class.
Example 1: Connection class
Java
import java.sql.*;
public class connection {
Connection con = null ;
public static Connection connectDB()
{
try {
Class.forName( "com.mysql.jdbc.Driver" );
Connection con = DriverManager.getConnection(
"root" , "1234" );
return con;
}
catch (SQLException | ClassNotFoundException e) {
System.out.println(e);
return null ;
}
}
}
|
Example 2: App/Main Class where the program is compiled and run calling the above connection class object
Java
import java.sql.*;
public class GFG {
public static void main(String[] args)
{
Connection con = null ;
PreparedStatement ps = null ;
con = connection.connectDB();
try {
String sql = "insert into cuslogin values('geeksforgeeks','gfg','geeks@email.com','flat 1','1239087474',10)" ;
ps = con.prepareStatement(sql);
ps.execute();
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
Details added here: “geeksforgeeks” named customer details have been added.
Share your thoughts in the comments
Please Login to comment...