Performing CRUD Operations - Java JDBC

Java examples for JDBC:SQL Statement

Description

Performing CRUD Operations

Demo Code

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

public class Main {

  public static void main(String[] args) {
    String sql = "INSERT INTO MyTable VALUES(" 
        + "next value for MyTable_seq, " //
        + "'13', " // 
        + "'Performing CRUD Operations', " //
        + "'How to perform create, read, update, delete functions', " //
        + "'Recipe Text')";

    try (Connection conn = getConnection();
        Statement stmt = conn.createStatement();) {
      // Returns row-count or 0 if not successful
      int result = stmt.executeUpdate(sql);
      if (result == 1) {
        System.out.println("-- Record created --");
      } else {//from ww w  .  ja v  a  2  s.co  m
        System.err.println("!! Record NOT Created !!");
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

  }

  private static void performRead() {
    String qry = "select id, recipe_name, description from MyTable";

    try (Connection conn = getConnection();
        Statement stmt = conn.createStatement();) {
      ResultSet rs = stmt.executeQuery(qry);
      while (rs.next()) {
        String recipe = rs.getString("id");
        String name = rs.getString("RECIPE_NAME");
        String desc = rs.getString("DESCRIPTION");

        System.out.println(recipe + "\t" + name + "\t" + desc);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

  }

  private static void performUpdate() {
    String sql = "UPDATE MyTable " + "SET id = '3' "
        + "WHERE id = '1'";

    try (Connection conn = getConnection();
        Statement stmt = conn.createStatement();) {
      int result = stmt.executeUpdate(sql);
      if (result == 1) {
        System.out.println("-- Record Updated --");
      } else {
        System.err.println("!! Record NOT Updated !!");
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

  }

  private static void performDelete() {
    String sql = "DELETE FROM MyTable WHERE id = '1'";

    try (Connection conn = getConnection();
        Statement stmt = conn.createStatement();) {
      int result = stmt.executeUpdate(sql);
      if (result == 1) {
        System.out.println("-- Record Deleted --");
      } else {
        System.err.println("!! Record NOT Deleted!!");
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static Connection getConnection() throws SQLException {
    Connection conn = null;

    String hostname = null;
    String port = null;
    String database = null;
    String username = null;
    String password = null;
    String driver = null;
    String jndi = null;
    String jdbcUrl;
    if (driver.equals("derby")) {
      jdbcUrl = "jdbc:derby://" + hostname + ":" + port + "/" + database;
    } else {
      jdbcUrl = "jdbc:oracle:thin:@" + hostname + ":" + port + ":" + database;
    }
    conn = DriverManager.getConnection(jdbcUrl, username, password);
    System.out.println("Successfully connected");
    return conn;
  }
}

Related Tutorials