import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdatableResultsetInsert {
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(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query = "select * from MYEMP";
rs = stmt.executeQuery(query);
// Check ResultSet's updatability
if (rs.getConcurrency() == ResultSet.CONCUR_READ_ONLY) {
System.out.println("ResultSet non-updatable.");
} else {
System.out.println("ResultSet updatable.");
}
boolean exist = false;
while(rs.next()){
if(rs.getInt(1) == 9999){
exist = true;
}
}
if(exist){
System.out.println("Record already exist...");
}else{
rs.moveToInsertRow();
rs.updateInt(1,9999);
rs.updateString(2, "Deepa");
rs.updateFloat(3, 20000);
rs.updateString(4, "Munnekolala");
rs.insertRow();
System.out.println("Row inserted ok.");
rs.close();
stmt.close();
con.close();
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}catch (SQLException ex1) {
ex1.printStackTrace();
}
}
}
No comments:
Post a Comment