Update records without using update command in java

Introduction 

 
In this blog we will know how to update records from the database without using update command.
 

Table creation with records

  1. create table employee(empno int,empname varchar(50),sal int)  
  2. insert into employee values(1,'raj',10000)  
  3. insert into employee values(2,'raju',20000)  
  4. insert into employee values(3,'rajesh',30000)   

Creation of dsn(database source name) for Oracle

 
Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system DSN tab-click add button-select a driver for which you want to set up a data source (for Oracle- Oracle in XE)-select it and click finish-give any name in the data source name textbox-then click ok button.
Note:- Here Username=system, Password=pintu and Dsn name=dsn1
 
/*updating record without using update command */
  1. import java.sql.*;  
  2. import java.util.*;  
  3. public class update   
  4. {  
  5.  public static void main(String args[]) throws Exception   
  6.  {  
  7.   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  8.   Connection con = DriverManager.getConnection("jdbc:odbc:dsn1""system""pintu");  
  9.   Statement stmt = con.createStatement(1004, 1008);  
  10.   Scanner sc = new Scanner(System.in);  
  11.   System.out.print("Provide employee no :");  
  12.   int no = sc.nextInt();  
  13.   ResultSet rs = stmt.executeQuery("select * from employee where empno=" + no);  
  14.   if (rs.next())   
  15.   {  
  16.    System.out.print("New Salary :");  
  17.    int s = sc.nextInt();  
  18.    rs.updateInt("sal", s);  
  19.    rs.updateRow();  
  20.    System.out.println("***Data changed****");  
  21.   }   
  22.   else  
  23.    System.out.println("No employee found ");  
  24.   con.close();  
  25.  }  
  26. }  

Compile

Javac update.java
Java update