Java jdbc Project Workshop
Java jdbc Project Workshop
(JDBC – Part 3)
Lecture 3
Executing Non Select Queries
Statement st=conn.createStatement();
String qry=“Insert into employees values(100, ‘Vishal’,4500)";
int res=st.executeUpdate(qry);
System.out.println("Record inserted:"+res);
Removing A Record
Statement st=conn.createStatement();
String qry=“Delete from employees where ename=‘Amit’;
int res=st.executeUpdate(qry);
System.out.println("Record deleted:"+res);
Updating A Record
Statement st=conn.createStatement();
String qry=“Update employees set sal=sal+1000 where empno=101’;
int res=st.executeUpdate(qry);
System.out.println("Record updated:"+res);
Dynamic SQL
• 2. Use PreparedStaement
Using String Concatenation
• The first placeholder represents position 1, the next position 2, and so forth.
pst.setInt(1, 101);
pst.setString(2,”Ravi”);
pst.setDouble(3,500.0);
Statement V/s PreparedStatement
Statement PreparedStatement
It is used to execute parameterized or
It is used to execute normal SQL queries.
dynamic SQL queries.
You cannot pass the parameters to SQL You can pass the parameters to SQL query at
query using this interface. run time using this interface.
This interface is mainly used for DDL It is used for any kind of SQL queries which
statements like CREATE, ALTER, DROP etc. are to be executed multiple times.