Thursday, July 22, 2010

Writing Java Object To Database

it is too easy to write java object to database with the use of following code




import java.io.Serializable;
public class Employee implements Serializable {
    private int Id; 
    private String firstName;
}
 

Employee object = new Employee();
PreparedStatement pstmt =conn.prepareStatement(SQL_INSERT_QUERY);
  
pstmt.setString(1, "Employee");
pstmt.setObject(2, object);
pstmt.executeUpdate();

here pstmt.setObject(2, object); statment will write java object to  database as one record record.











implements Serializable is important to serialization of java object to database.

No comments:

Post a Comment