Example usage for java.sql PreparedStatement executeUpdate

List of usage examples for java.sql PreparedStatement executeUpdate

Introduction

In this page you can find the example usage for java.sql PreparedStatement executeUpdate.

Prototype

int executeUpdate() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Usage

From source file:DemoPreparedStatementSetDate.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/*w  ww  .  ja va 2  s. c om*/
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into date_table(id, date_column) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        java.sql.Date date = getCurrentJavaSqlDate();
        pstmt.setDate(2, date);

        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:DemoPreparedStatementSetBigDecimal.java

public static void main(String[] args) throws Exception {
    Connection conn = null;//from w w w  .j  a v a  2s .c o  m
    PreparedStatement pstmt = null;
    String query = null;
    try {
        conn = getConnection();
        query = "insert into  BIG_DECIMAL_TABLE(id, big_decimal) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "001");
        pstmt.setBigDecimal(2, new java.math.BigDecimal("123456789"));
        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    float floatValue = 0001f;
    double doubleValue = 1.0001d;
    Connection conn = null;/*from  w  w w .  j  a  va2  s.co m*/
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into double_table(id, float_column, double_column) values(?, ?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        pstmt.setFloat(2, floatValue);
        pstmt.setDouble(3, doubleValue);
        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/old", "user", "pass");
    Connection con1 = DriverManager.getConnection("jdbc:postgresql://localhost:5432/new", "user", "pass");

    String sql = "INSERT INTO users(" + "name," + "active," + "login," + "password)" + "VALUES(?,?,?,?)";

    Statement statement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

    PreparedStatement pstmt = con1.prepareStatement(sql);

    ResultSet rs = statement.executeQuery("SELECT * FROM users");
    while (rs.next()) {
        String nm = rs.getString(2);
        Boolean ac = rs.getBoolean(3);
        String log = rs.getString(4);
        String pass = rs.getString(5);
        pstmt.setString(1, nm);// w  ww.  j a  va2  s  . co  m
        pstmt.setBoolean(2, ac);
        pstmt.setString(3, log);
        pstmt.setString(4, pass);
        pstmt.executeUpdate();
    }
    con.close();
    con1.close();
}

From source file:InsertCustomType_Oracle.java

public static void main(String[] args) {
    String id = "001";
    String isbn = "1234567890";
    String title = "java demo";
    String author = "java2s";
    int edition = 1;

    Connection conn = null;/*from w  ww .  j a  v  a2s  .  co  m*/
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String insert = "insert into book_table values(?, BOOK(?, ?, ?, ?))";
        pstmt = conn.prepareStatement(insert);
        pstmt.setString(1, id);
        pstmt.setString(2, isbn);
        pstmt.setString(3, title);
        pstmt.setString(4, author);
        pstmt.setInt(5, edition);
        pstmt.executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    } finally {
        try {
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:InsertTextFileToOracle.java

public static void main(String[] args) throws Exception {
    String id = "001";
    String fileName = "fileName.txt";

    FileInputStream fis = null;/*from w w w.  jav a2 s  .  co  m*/
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
        conn = getConnection();
        conn.setAutoCommit(false);
        File file = new File(fileName);
        fis = new FileInputStream(file);
        pstmt = conn.prepareStatement("insert into DataFiles(id, fileName, fileBody) values (?, ?, ?)");
        pstmt.setString(1, id);
        pstmt.setString(2, fileName);
        pstmt.setAsciiStream(3, fis, (int) file.length());
        pstmt.executeUpdate();
        conn.commit();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        pstmt.close();
        fis.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    Connection conn = null;/*from   w  w w  . j  a  va  2  s .c  o  m*/
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into nullable_table(id,string_column, int_column) values(?, ?, ?)";

        // create PrepareStatement object
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        pstmt.setNull(2, java.sql.Types.VARCHAR);
        pstmt.setNull(3, java.sql.Types.INTEGER);

        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection dbConnection = null;
    PreparedStatement preparedStatement = null;

    String createTableSQL = "CREATE TABLE Person(" + "USER_ID NUMBER(5) NOT NULL, "
            + "USERNAME VARCHAR(20) NOT NULL, " + "CREATED_BY VARCHAR(20) NOT NULL, "
            + "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) " + ")";

    Class.forName(DB_DRIVER);/*from  w  ww  .  jav  a  2 s.co m*/
    dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

    preparedStatement = dbConnection.prepareStatement(createTableSQL);

    System.out.println(createTableSQL);

    preparedStatement.executeUpdate();

    preparedStatement.close();
    dbConnection.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/*  ww  w .jav  a 2  s  .  co m*/
    PreparedStatement pstmt = null;
    java.sql.Array sqlArray = null;
    conn = getOracleConnection();
    //  ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("CHAR_ARRAY", conn);
    String[] content = { "v1", "v2", "v3", "v4" };
    // sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content);

    String query = "insert into CHAR_ARRAY_TABLE(id, array) values(?, ?)";

    pstmt = conn.prepareStatement(query);
    pstmt.setString(1, "0001");
    pstmt.setArray(2, sqlArray);

    int rowCount = pstmt.executeUpdate();
    System.out.println("rowCount=" + rowCount);
    System.out.println("--Demo_PreparedStatement_SetArray end--");
    pstmt.close();
    conn.close();
}

From source file:DemoPreparedStatementSetAsciiStream.java

public static void main(String[] args) {
    Connection conn = null;//from   w  ww .j  a  v  a2  s  .  c o  m
    PreparedStatement pstmt = null;
    String query = null;
    try {
        conn = getConnection();
        String fileName = "fileName.txt";
        File file = new File(fileName);
        int fileLength = (int) file.length();
        InputStream stream = (InputStream) new FileInputStream(file);

        query = "insert into  LONG_VARCHAR_TABLE(id, stream) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, fileName);
        pstmt.setAsciiStream(2, stream, fileLength);

        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}