JDBC Using PreparedStatement example – Update a record
/* This program shows you how to update the data in
the database table using JDBC PreparedStatement .
In this you update the name on the giving id.*/
package r4r;
import java.sql.*;
import java.util.*;
public class UpdateData
{
public static void main(String[] args)
{
try
{
Scanner in=new Scanner(System.in);
System.out.println("enter the name:-");
String n=in.nextLine();
System.out.println("enter the id:-");
int i=in.nextInt();
in.nextLine();
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement stmt=con.prepareStatement("update user5
set name=? where id=?");
stmt.setString(1, n);
stmt.setInt(2, i);
stmt.executeQuery();
System.out.println("successfully updated");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
output:
enter the name:-
Ajeet
enter the id:-
12
successfully updated