Following method adds a record to the database table:
public void addPerson(Person p) {
if (p == null || p.getName().equals("")) {
return;
}
String name = p.getName();
String address = p.getAddress();
String phone = p.getPhone();
Connection con = null;
try {
con = getConnection();
if (con != null) {
String sql = "INSERT INTO APP.ADDRESS_BOOK("
+ "NAME,"
+ "ADDRESS,"
+ "PHONE) "
+ "VALUES(?,?,?)";
PreparedStatement pStmt = con.prepareStatement(sql);
// Set the values
pStmt.setString(1, name);
pStmt.setString(2, address);
pStmt.setString(3, phone);
// Insert
pStmt.executeUpdate();
}
} catch (SQLException ex) {
Logger.getLogger(PersonDAO.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.getMessage());
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(PersonDAO.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.getMessage());
}
}
}
}
No comments:
Post a Comment