Executing a SQL INSERT Statement Using a Statement Object - Java JDBC

Java examples for JDBC:SQL Statement

Description

Executing a SQL INSERT Statement Using a Statement Object

Demo Code

import java.sql.Connection;
import java.sql.Driver;
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) {
    Connection conn = null;//from   www.j ava 2  s. co  m
    try {
      conn = JDBCUtil.getConnection();
      insertPerson(conn, 101, "John", "Bates",
                   "M", "{d '1970-01-01'}", 60000);
      insertPerson(conn, 102, "D", "A",
                   "F", "{d '1960-01-01'}", 70000);
      insertPerson(conn, 103, "B", "R",
                   "M", "{d '1975-01-01'}", 45000);
      JDBCUtil.commit(conn);
      System.out.println("Inserted persons successfully.");
    }
    catch (SQLException e) {
      System.out.println(e.getMessage());
      JDBCUtil.rollback(conn);
    }
    finally {
      JDBCUtil.closeConnection(conn);
    }
  }

  public static void insertPerson(Connection conn, int personId,
    String firstName, String lastName, String gender, String dob,
    double income) throws SQLException {

    String SQL = "insert into person " +
                 "(person_id, first_name, last_name," +
                 " gender, dob, income) " +
                 "values " +
                 "(" + personId + ", " +
                  "'" + firstName + "'" + ", " +
                  "'" + lastName + "'" + ", " +
                  "'" + gender + "'" + ", " +
                  dob + ", " +
                  income + ")";

    Statement stmt = null;
    try {
      stmt = conn.createStatement();
      stmt.executeUpdate(SQL);
    }
    finally {
      JDBCUtil.closeStatement(stmt);
    }
  }
}


class JDBCUtil {
  public static Connection getConnection() throws SQLException {
    Driver derbyEmbeddedDriver = null;//new org.apache.derby.jdbc.EmbeddedDriver();
    DriverManager.registerDriver(derbyEmbeddedDriver);

    String dbURL = "jdbc:derby:beginningJavaDB;create=true;";
    String userId = "root";
    String password = "password";

    Connection conn = DriverManager.getConnection(dbURL, userId, password);
    conn.setAutoCommit(false);
    return conn;
  }

  public static void closeConnection(Connection conn) {
    try {
      if (conn != null) {
        conn.close();
      }
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void closeStatement(Statement stmt) {
    try {
      if (stmt != null) {
        stmt.close();
      }
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void closeResultSet(ResultSet rs) {
    try {
      if (rs != null) {
        rs.close();
      }
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void commit(Connection conn) {
    try {
      if (conn != null) {
        conn.commit();
      }
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void rollback(Connection conn) {
    try {
      if (conn != null) {
        conn.rollback();
      }
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    Connection conn = null;
    try {
      conn = getConnection();
      System.out.println("Connetced to the database.");
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
    finally {
      closeConnection(conn);
    }
  }
}

Related Tutorials