Skip to main content

Executing an Insert

Once you have a connection to Caché, again represented by either java.sql.Connection or com.intersys.objects.Database, you can update the database. The following method executes an SQL INSERT operation against the database. It adds a new row to the PhoneNumber table.

public class JDBCExamples {
   public static void insertPhoneNumber(String contactId,
   String number, String type)throws SQLException, ClassNotFoundException{
      Connection conn = JDBCExamples.createConnection();
      String sql =
      "INSERT INTO JavaTutorial.PhoneNumber (Contact, Number, PhoneNumberType)"+
      "VALUES (?,?,?)";
      PreparedStatement pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, contactId);
      pstmt.setString(2, number);
      pstmt.setString(3, type);
      pstmt.executeUpdate();
   }
}

This method uses a prepared statement, represented by a java.sql.PreparedStatement object, which it creates using the Connection class' prepareStatement method. Note that the Caché Database class also contains a prepareStatement method that returns a java.sql.PreparedStatement object.

FeedbackOpens in a new tab