Introduction
In
this blog we will know how to update records from the database without using
update command.
Table
creation with records
- create table employee(empno int,empname varchar(50),sal int)
- insert into employee values(1,'raj',10000)
- insert into employee values(2,'raju',20000)
- 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 */
- import java.sql.*;
- import java.util.*;
- public class update
- {
- public static void main(String args[]) throws Exception
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con = DriverManager.getConnection("jdbc:odbc:dsn1", "system", "pintu");
- Statement stmt = con.createStatement(1004, 1008);
- Scanner sc = new Scanner(System.in);
- System.out.print("Provide employee no :");
- int no = sc.nextInt();
- ResultSet rs = stmt.executeQuery("select * from employee where empno=" + no);
- if (rs.next())
- {
- System.out.print("New Salary :");
- int s = sc.nextInt();
- rs.updateInt("sal", s);
- rs.updateRow();
- System.out.println("***Data changed****");
- }
- else
- System.out.println("No employee found ");
- con.close();
- }
- }
Compile
Javac
update.java
Java
update