Followers

JDBC : Updation using Statement Interface

package com.pack1;

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

public class UpdateQuery1 {
     public static void main(String[] args){
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            String url = "jdbc:derby://localhost:1527/hari";
            con = DriverManager.getConnection(url, "app","app");
            stmt = con.createStatement();
            Scanner sc=new Scanner(System.in); 
            System.out.println("Please enter the employee number to be updated");
            int id = sc.nextInt();
            System.out.println("Please enter the employee name to update");
            String sname = sc.next();     
            
            
            String query = "update MYEMP set name='"+sname+"' where ENO = "+id;
            int count = stmt.executeUpdate(query);                       
                System.out.println(count + "Records updated.");                       
            
        } catch (ClassNotFoundException | SQLException ex) {
            ex.printStackTrace();
        }
    }
}

No comments:

Post a Comment