Wednesday, 11 December 2013

Scrollable ResultSet:jdbc program for adding a new record

//Scrollable ResultSet:jdbc program for adding a new record 

import java.sql.*;
import java.util.*;

public class AddEmployee {
public static void main(String s[]) throws Exception {

Driver d= (Driver) ( Class.forName("com.mysql.jdbc.Driver").newInstance());

Properties p=new Properties ();
p.put("user","root");
p.put("password","admin");

Connection con=d.connect(
"jdbc:mysql://localhost:3306/test",p);

Statement st=con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

ResultSet rs= st.executeQuery("select empno, ename, sal, deptno from emp");

rs.moveToInsertRow();
rs.updateInt(1,102);
rs.updateString(2,"kalyan");
rs.updateDouble(3,950);
//rs.updateInt(1,20);//dont give any duplicate prime no:Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '20' for key 'PRIMARY'
rs.updateInt(1,40);
rs.insertRow();
System.out.print("Employee Inserted");
con.close();
}//main
}//class

No comments:

Post a Comment