JDBC EXAMPLE FOR UPDATE | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Tuesday, May 20, 2014

JDBC EXAMPLE FOR UPDATE


PROGRAM:
import java.sql.*;
import java.io.*;
class EmpUpdate
{
public static void main(String a[])throws Exception
{
try
{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection c=DriverManager.getConnection("jdbc:odbc:Empsn");
            Statement s=c.createStatement();
            int n=102;
            String str1 = "update emp set name=” + ",\'" +  "kelvin" + ",\'"+ “where id=”+ n;
            int count=s.executeUpdate(str1);
            if(count==1)
            System.out.println("Record updated!!");
            else
            System.out.println("Record not updated");
String str="select * from emp";
            ResultSet r=s.executeQuery(str);
                        while(r.next())
                        {
                        int id=r.getInt(1);
                        String name=r.getString(2);
                        String de=r.getString(3);
                        double sal=r.getDouble(4);
                        double hr=r.getDouble(5);
                        double da=r.getDouble(6);
                        double loan=r.getDouble(7);
                        double n=(sal+hr+da)-loan;
                        System.out.println("Empid:" +id);
                        System.out.println("Name: "+name);
                        System.out.println("Designation: "+de);
                        System.out.println("Salary: "+n);
                        }
}
catch(Exception e)
{
System.out.println(e);
}
      }
}



OUTPUT:

TABLE CREATION IN MS-ACCESS
id
Name
De
Sal
Hr
Da
Loan
101
Martin
manager
10000
5000
750
750
102
Smith
asstmanager
7500
2500
1000
1000
103
William
Clerk
8000
1500
500
2000

Record updated!!

Empid: 101
Name: Martin
Designation: Manager
Salary: 15000

Empid: 102
Name: Kelvin
Designation: AsstManager
Salary: 10000


Empid: 103
Name: William
Designation: clerk

Salary: 8000

No comments:

Post a Comment

Thanks for your valuable comments

Followers