Jdbc Program to Update Database records based on the given input by End-User.

3:36:00 PM Rohit Kumar Verma 0 Comments

Oracle: Any version , Tested on Oracle 12c.
Java 64 bit: Tested on version 7.0, from 8.0 JDBC Driver removed.

/* Taking the input from the user and updating the                                        
 * record based on the input                                                              
package com.rkv.jdbc;                                                                     
import java.sql.Connection;                                                               
import java.sql.DriverManager;                                                            
import java.sql.SQLException;                                                             
import java.sql.Statement;                                                                
import java.util.Scanner;                                                                 
public class UpdateQuery {                                                                
 public static void main(String[] args) {                                              
  // Declaring variables                                                            
  //Connection variable                                                             
  Connection con = null;                                                            
  //Statement Variable to pass the value                                            
  Statement st = null;                                                              
  //Scanner Variable to get input from the End-user                                 
  Scanner sc = null;                                                                
   sc = new Scanner(;                                                  
   String sno = null,name = null;                                                
   //Getting Input from the user                                                 
   System.out.println("Enter The Student Number in which you want to update :"); 
    sno =;                                                             
   System.out.println("Enter name to update ");                                  
    name =;                                                            
   //registering driver                                                          
   //Creating Connection with Database Server                                    
   con = DriverManager.getConnection("jdbc:odbc:oradsn","c##scott","tiger");     
   //Creating statement object                                                   
   st= con.createStatement();                                                    
   int s = st.executeUpdate("update student set name='"+name+"' where sno ="+sno);
    System.out.println("Records not updated");                                
    System.out.println("records Updated sucessfully");                        
  // Catching the Exception                                                         
  catch(ClassNotFoundException ce){                                                 
   System.out.println("Class Not Found or Missing");                             
   catch(SQLException ce1)                                                       
    System.out.println("Sql Exception occured contact Rohit"+ce1);            
   catch(Exception ce2)                                                          
   //Closing the connections                                                     
    catch(SQLException se){                                                   
    catch(SQLException se){