Sajmon

Example of insert

May 8th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. private String sqlStatement = "INSERT INTO Person VALUES(?,?)";
  2.  
  3. public boolean insertMember(Object newMember) {
  4.     Connection con = null;
  5.     PreparedStatement ps = null;
  6.  
  7.     try {
  8.         con = OracleDAOFactory.getConnection();
  9.         ps = con.prepareStatement(sqlStatement);
  10.         ps.setInt(1, newMember.getAttribute());
  11.         ps.setString(2, newMember.getAnotherAttribute());
  12.         ps.executeUpdate();
  13.         System.out.println("Member inserted successfully.");
  14.         return true;
  15.     }
  16.     catch (SQLException ex) {
  17.         Logger.getLogger(NameOfClass.class.getName()).log(Level.SEVERE, null, ex);
  18.         return false;  
  19.     }
  20.     finally {
  21.         if (con != null) {
  22.             try {
  23.                 con.close();
  24.             }
  25.             catch (SQLException ex) {
  26.                 Logger.getLogger(NameOfClass.class.getName()).log(Level.SEVERE, null, ex); 
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment