Database:
Program Prepared Statement:
DynamicUpdateApp.java :
SqlUtils.java:
Output:
Program Prepared Statement:
DynamicUpdateApp.java :
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Scanner; public class DynamicUpdateApp { public static void main(String args[]) { Connection connection = null; PreparedStatement psmt = null; try { Class.forName("oracle.jdbc.OracleDriver"); connection = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE", "System", "Atharva007"); psmt = connection.prepareStatement(SqlUtils.DYNC_UPDATE_QUERY); // Update Student set address= ? where name = ? Scanner scan = new Scanner(System.in); System.out.println("Enter Address:"); String address = scan.next(); System.out.println("Enter Name:"); String name = scan.next(); psmt.setString(2, name); psmt.setString(1, address); int count = psmt.executeUpdate(); System.out.println(count + " Record(s) Updated"); } catch(SQLException e){ e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (psmt != null) psmt.close(); if (connection != null) connection.close(); } catch (Exception e) { e.printStackTrace(); } } } }
SqlUtils.java:
public interface SqlUtils { public static String DYNC_INSERT_QUERY = "Insert into Student values(?,?,?)"; }
Output: