Add

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

3:36:00 PM Rohit Kumar Verma 0 Comments

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


//UpdateQuery.java                                                                        
/* 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;                                                                
  try{                                                                              
   sc = new Scanner(System.in);                                                  
   String sno = null,name = null;                                                
   //Getting Input from the user                                                 
   if(sc!=null)                                                                  
   {                                                                             
   System.out.println("Enter The Student Number in which you want to update :"); 
    sno = sc.next();                                                             
   System.out.println("Enter name to update ");                                  
    name = sc.next();                                                            
    }//if                                                                        
   //registering driver                                                          
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");                                
   //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);
   if(s==0)                                                                      
   {                                                                             
    System.out.println("Records not updated");                                
   }                                                                             
   else{                                                                         
    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)                                                          
   {                                                                             
    System.out.println(ce2);                                                  
   }                                                                             
  finally{                                                                          
   //Closing the connections                                                     
   try{                                                                          
    if(st!=null){                                                             
     st.close();                                                           
    }                                                                         
   }                                                                             
    catch(SQLException se){                                                   
     se.printStackTrace();                                                 
    }                                                                         
   try{                                                                          
    if(con!=null){                                                            
     con.close();                                                          
    }                                                                         
   }                                                                             
    catch(SQLException se){                                                   
     se.printStackTrace();                                                 
    }                                                                         
  }//finally                                                                    
}//main                                                                                   
 }//class                                                                              

0 comments: