-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreparedStatementExample.java
29 lines (26 loc) · 1.06 KB
/
PreparedStatementExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;
import java.sql.PreparedStatement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PreparedStatementExample {
public static void main(String args[])
{
try{
Connection con=UtilityClass.connect();
Scanner sc=new Scanner(System.in);
System.out.println("Enter RollNo of student to promote ");
String n=sc.next();
String sql="update student set class=class+1 where rollno=?";
//to execute parametrized sql we need PreparedStatement object (instead of Statement)
PreparedStatement pt=(PreparedStatement) con.prepareStatement(sql);
pt.setString(1, n);
int n1=pt.executeUpdate();
System.out.println(n1+" rows Updated!");
con.close();
}catch (Exception ex) {
Logger.getLogger(PreparedStatementExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
}