Java JDBC PreparedStatement run insert statement

Description

Java JDBC PreparedStatement run insert statement

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Main {

   public static void insertRecord(Connection conn) {
      String sql = "INSERT INTO RECIPES VALUES(" + "RECIPES_SEQ.NEXTVAL, ?,?,?,?)";
      PreparedStatement pstmt = null;
      try {//  ww w  .j ava 2 s  . c  o m
         pstmt = conn.prepareStatement(sql);
         pstmt.setString(1, "1001");
         pstmt.setString(2, "Java Book");
         pstmt.setString(3, "demo from demo2s.com");
         pstmt.setString(4, "comment");
         pstmt.executeUpdate();
         System.out.println("Record successfully inserted.");
      } catch (SQLException ex) {
         ex.printStackTrace();
      } finally {
         if (pstmt != null) {
            try {
               pstmt.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }
      }

   }
}



PreviousNext

Related