Java JDBC Statement run insert statement

Description

Java JDBC Statement run insert statement

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {

   static void performCreate(Connection conn) {
      String sql = "INSERT INTO RECIPES VALUES(" + 
                   "RECIPES_SEQ.NEXTVAL, " + 
                    "'11-4', " + 
                    "'String value 1', " + 
                    "'String value 2', " + 
                    "'String value 3')";
      Statement stmt = null;//from w  ww  .j  a  va2  s  .  c  o  m

      try {
         stmt = conn.createStatement();
         // Returns row-count or 0 if not successful
         int result = stmt.executeUpdate(sql);
         if (result > 0) {
            System.out.println("-- Record created --");
         } else {
            System.out.println("!! Record NOT Created !!");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if (stmt != null) {
            try {
               stmt.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }

      }

   }
}



PreviousNext

Related