Example usage for java.sql Connection setAutoCommit

List of usage examples for java.sql Connection setAutoCommit

Introduction

In this page you can find the example usage for java.sql Connection setAutoCommit.

Prototype

void setAutoCommit(boolean autoCommit) throws SQLException;

Source Link

Document

Sets this connection's auto-commit mode to the given state.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int, name VARCHAR(30) );");

    String INSERT_RECORD = "select * from survey where id < ?";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);

    pstmt.setInt(1, 1);/*from w w  w  .j  a  v  a 2  s  . co  m*/

    pstmt.setFetchSize(200);

    ResultSet rs = pstmt.executeQuery();

    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int, name VARCHAR(30) );");

    String INSERT_RECORD = "insert into survey(id, name) values(?,?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);

    for (int i = 0; i < 10; i++) {
        pstmt.setInt(1, i);/*ww  w .j a  v a2s . c o m*/
        pstmt.setString(2, "name" + i);
        pstmt.executeUpdate();
    }

    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);
    conn.setAutoCommit(false);

    String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "java.gif");
    stmt.setString(2, "Java Official Logo");

    File image = new File("D:\\a.gif");
    FileInputStream fis = new FileInputStream(image);
    stmt.setBinaryStream(3, fis, (int) image.length());
    stmt.execute();//from   ww w .j  a v  a  2 s.  com

    conn.commit();
    fis.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int, name VARCHAR(30) );");
    String INSERT_RECORD = "insert into survey(id, name) values(?,?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    pstmt.setString(2, "name1");
    pstmt.addBatch();/*from  www. jav  a2 s.  com*/

    pstmt.setString(1, "2");
    pstmt.setString(2, "name2");
    pstmt.addBatch();
    int[] updateCounts = pstmt.executeBatch();
    checkUpdateCounts(updateCounts);
    conn.commit();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    outputResultSet(rs);
    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Connection conn = getHSQLConnection();

    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st = conn.createStatement();//ww  w  . j  a v  a  2 s .  c o  m
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    outputResultSet(rs);

    checkForWarning(rs.getWarnings());

    rs.close();
    st.close();
    conn.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);
    conn.setAutoCommit(false);

    String sql = "INSERT INTO documents (name, description, data) VALUES (?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "a.txt");
    stmt.setString(2, "b");

    File data = new File("C:\\a.txt");
    FileReader reader = new FileReader(data);
    stmt.setCharacterStream(3, reader, (int) data.length());
    stmt.execute();/*  w w  w  .ja va 2s .  co  m*/

    conn.commit();
    reader.close();
    conn.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    try {//from  ww  w  . java2s.  com
        st.executeUpdate("create table survey (id int,name varchar(30));");
        st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
        st.executeUpdate("insert into survey (id,name ) values (2,'nameValue')");
        // commits all the transactions
        conn.commit();
    } catch (Exception e) {
        //cancel (roll back) all the transactions
        conn.rollback();
        // to see what went wrong
        e.printStackTrace();
    }

    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*w  w  w . j  a v  a 2  s.co m*/
        Connection conn = getHSQLConnection();

        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        st.executeUpdate("create table survey (id int,name varchar(30));");
        st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
        st = conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM survey");

        outputResultSet(rs);

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException sqle) {
        do { // loop through each exception
             // do something with each exception
            System.err.println("Exception occurred:\nMessage: " + sqle.getMessage());
            System.err.println("SQL state: " + sqle.getSQLState());
            System.err.println("Vendor code: " + sqle.getErrorCode() + "\n----------------");
        } while ((sqle = sqle.getNextException()) != null);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    try {//from ww w  .  j  a  v a 2 s  .  com
        st.executeUpdate("create table survey (id int,name varchar(30));");
        st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
        st.executeUpdate("insert into survey (id,name ) values (2,'nameValue')");
        // commits all the transactions
        conn.commit();
    } catch (Exception e) {
        // cancel (roll back) all the transactions
        conn.rollback();
        // to see what went wrong
        e.printStackTrace();
    }

    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*  w  w w .  j a  v a  2  s. com*/
        Connection conn = getHSQLConnection();

        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        st.executeUpdate("create table survey (id int,name varchar(30));");
        st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
        st = conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM survey");

        outputResultSet(rs);

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException sqle) {
        String sqlMessage = sqle.getMessage();
        String sqlState = sqle.getSQLState();
        int vendorCode = sqle.getErrorCode();
        System.err.println("Exception occurred:");
        System.err.println("Message: " + sqlMessage);
        System.err.println("SQL state: " + sqlState);
        System.err.println("Vendor code: " + vendorCode + "\n----------------");
    }
}