Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Update Contents of a Table using JDBC Connection?

  • Last Updated : 08 Dec, 2020

JDBC (Java Database Connectivity) is basically a standard API(application interface) between the java programming language and various databases like Oracle, SQL, PostgreSQL, etc. It connects the front end(for interacting with the users) with the backend( for storing data).

Steps to Update the contents of a table using JDBC

1. Create Database: You can create a database using SQLyog and create some tables in it and fill data inside it in order to update the contents of a table. Here, for example, the name of my database is hotelman, and table names are cuslogin and adminlogin. We will be taking cuslogin table as an example.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Create a DatabaseTable Data

2. Create Connection: Open Netbeans and create a new package. Inside the package, open a new java file and type the below code for JDBC connectivity and save the filename with connection.java.



Java




// Java program to create a connection to a database
  
import java.sql.*;
  
public class connection {
  
    // Connection instance
    Connection con = null;
  
    public static Connection connectDB()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
  
            Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/hotelman",
                "root", "1234");
            
            // here,root is the username and 1234 is the
            // password,you can set your own username and
            // password.
            return con;
        }
        catch (SQLException e) {
  
            System.out.println(e);
        }
    }
}

3. Update Contents In a Table: Let’s suppose we want to update the customer name  from cuslogin table whose id is 2.

Initialize a string with the SQL query as follows

String sql="update  cuslogin set name='GFG' where id=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();

Now, add the SQL query given above, inside prepareStatement and execute it as follows

p =con.prepareStatement(sql);
p.execute();

Open a new java file (here, its result.java) inside the same package and type the full code (shown below) for updating the name of the customer whose id is 2, from 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!!

Java




// Java Program to Update contents in a table
  
import java.sql.*;
public class result {
  
    public static void main(String[] args)
    {
        Connection con = null;
        PreparedStatement p = null;
        con = connection.connectDB();
        try {
            String sql
                = "update cuslogin set name='GFG' where id=2";
            p = con.prepareStatement(sql);
            p.execute();
        }
        catch (SQLException e) {
            System.out.println(e);
        }
    }
}

After running the result.java, the output can be seen in SQLyog as follows:

Updated table

We can see, the customer whose id is 2, now has the name GFG.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!