JDBC technology is an API that provides cross-DBMS connectivity to a wide range of SQL databases and access to other tabular data sources, such as spreadsheets or flat files. With a JDBC technology-enabled driver, you can connect all corporate data even in a heterogeneous environment.

At times there is need of inserting row data in tables at runtime. JDBC provides facility of insertion of data by support of INSERT statement.

The example below inserts data into an SQL server's database tables.

 import java.sql.*;

public class insertTableData
{
    public static void main(String[] args) 
    {
        DB db = new DB();
        Connection conn=db.dbConnect(
    "jdbc:jtds:sqlserver://localhost:1433/tempdb","sa","");
        db.insertData(conn);
    }
}

class DB
{
    public DB() {}

    public Connection dbConnect(String db_connect_string,
  String db_userid,String db_password)
    {
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
    db_connect_string,db_userid,db_password);
            System.out.println("connected");
            return conn;
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public void insertData(Connection conn)
    {
        Statement stmt;
        try
        {
            stmt = conn.createStatement();
            stmt.executeUpdate("insert into cust_profile " +
                 "values('name1', 'add1','city1','state1','country1')");
    
            stmt.executeUpdate("insert into cust_profile " +
                 "values('name2', 'add2','city2','state2','country2')");

            stmt.executeUpdate("insert into cust_profile " +
                 "values('name3', 'add3','city3','state3','country3')");
    
            stmt.close();
            conn.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
};