Write a Java program to accept the empno from user and update the salary of employee and display the updated record on the screen. Employee having fields empno, ename & salary.
Write a Java program to accept the empno from user and update the salary of employee and display the updated record on the screen. Employee having fields empno, ename & salary.
Program:
import java.sql.*;
import java.io.*;
class Employee {
public static void main(String args[]) {
Connection con;
Statement stmt;
ResultSet rs;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:employeeDSN");
stmt = con.createStatement();
System.out.println("Enter Employee No:");
int empno = Integer.parseInt(br.readLine());
System.out.println("Enter new Salary of Employee:");
int salary = Integer.parseInt(br.readLine());
stmt.executeUpdate("update employee set salary = "+salary+" where empno="+empno);
System.out.println("Record Updated.");
rs = stmt.executeQuery("select * from employee where empno="+empno);
while(rs.next()){
System.out.println("Employee=["+rs.getInt(1)+",\t"+rs.getString(2)+",\t"+rs.getInt(3)+"]");
}
} catch(Exception e) {
System.out.println(e);
}
}
}
Comments
Post a Comment