top of page
Search
  • Writer's pictureNaresh Padiyar

Simple Java JDBC Program To Execute SQL Update Statement



Below is a sample Java JDBC program which takes database connection string as command line arguments and executes the SQL update statement.


Value to be updated in the table is passed as a command line argument.


Replace the SQL update statement in the below code with the desired update statement.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class Run_SQL_Update {
 
    static Connection sqlConn = null;
    static Statement sqlStmt = null;
    static ResultSet sqlResultset = null;
 
    public static void main(String[] args) throws SQLException {
 
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException exception) {
            System.out.println("Oracle Driver Class Not found Exception: " + exception.toString());
            return;
        }
 
        DriverManager.setLoginTimeout(5);
 
        try {

	    String db_host=args[0];
            String db_serviceName=args[1];
            String db_username=args[2];
            String db_password=args[3];
            String jdbc_conn_string="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST="+db_host+")(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME="+db_serviceName+")))";
	    sqlConn = DriverManager.getConnection(jdbc_conn_string, db_username, db_password);
        } 
        catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            System.exit(1);
            return;
        }
        
        sqlStmt = sqlConn.createStatement();
        String var1 = args[4];
	String var2 = args[5];
	sqlResultset = sqlStmt.executeQuery("update SAMPLE_TABLE SET SAMPLE_COLUMN1='"+var1+"' where SAMPLE_COLUMN2=='"+var2+"'");
        sqlResultset.close(); 
        
    }
 
}

Copy paste the above code in a file having same name as the class name.


Replace the SQL update statement as per your requirement.


Compile the code:

javac Run_SQL_Update.java

Execute the code with the class name:

java Run_SQL_Update ${DB_HOST} ${DB_SID} ${DB_USER} ${DB_PASSWORD} ${VALUE_TO_UPDATE} ${CONDITION_VALUE}

40 views0 comments

Recent Posts

See All

Cronitor API Examples

List All CronJobs and their code curl -X GET https://cronitor.io/v2/monitors -u <API Token>: | jq curl -X GET https://cronitor.io/v2/monitors -u <API Token>: | jq . | jq '.monitors' | jq -c --raw-outp

AWS CLI Cheatsheet

1. Command to export EC2 Details in CSV Format Create the Excel file with Headers echo -e "InstanceID\tInstanceType\tImageId\tCoreCount\tState\tLaunchTime\tAvailabilityZone\tPrivateIpAddress\tPrivateD

Docker Command Cheatsheet

To build a docker image from Dockerfile docker build To pull a docker image docker pull To run a docker image docker run Find docker version docker version docker -v Find docker installation details

bottom of page