Followers

JDBC : Updation using PreParedStatement

package com.pack1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

public class UpdatePrePared11 {
   
    public static void main(String[] args) throws Exception
    {
        Connection con = null;
       
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            String url = "jdbc:derby://localhost:1527/hari";
            con = DriverManager.getConnection(url, "app","app");
            String query = "update MYEMP set SALARY=SALARY+? where ENO = ?";
            PreparedStatement pstmt = con.prepareStatement(query);
           Scanner sc = new Scanner(System.in);
           while(true){
           System.out.println("Please enter the number to be updated.");
           int no = sc.nextInt();
           System.out.println("By what amount.");
           float salary = sc.nextFloat();
           pstmt.setFloat(1, salary);
           pstmt.setInt(2,no );
           int count = pstmt.executeUpdate();
           if(count > 0)
           System.out.println("Record updated sucessfully..");
           else
           System.out.println("Record updation failed..");
           System.out.println("Updation of another record is required (yes/no)");
           String choice = sc.nextLine();
           if(!choice.equalsIgnoreCase("yes"))
               break;
         
           }
    }
}

No comments:

Post a Comment