Example usage for java.sql Connection createStatement

List of usage examples for java.sql Connection createStatement

Introduction

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

Prototype

Statement createStatement() throws SQLException;

Source Link

Document

Creates a Statement object for sending SQL statements to the database.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);//from   w ww  . j av  a2 s  .com
    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();

    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[] argv) throws Exception {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);//  w  ww.j  a v a 2  s  .co m

    String serverName = "127.0.0.1";
    String portNumber = "1521";
    String sid = "mydatabase";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);

    Statement stmt = connection.createStatement();

    stmt.execute("CREATE TYPE number_varray AS VARRAY(10) OF NUMBER(12, 2)");

    stmt.execute("CREATE TYPE my_object AS OBJECT(col_string2 VARCHAR(30), col_int2 INTEGER)");

    //    Column Name          Oracle Type             Java Type
    String sql = "CREATE TABLE oracle_all_table(" + "col_short           SMALLINT, " // short
            + "col_int             INTEGER, " // int
            + "col_float           REAL, " // float; can also be NUMBER
            + "col_double          DOUBLE PRECISION, " // double; can also be FLOAT or NUMBER
            + "col_bigdecimal      DECIMAL(13,0), " // BigDecimal
            + "col_string          VARCHAR2(254), " // String; can also be CHAR(n)
            + "col_characterstream LONG, " // CharacterStream or AsciiStream
            + "col_bytes           RAW(2000), " // byte[]; can also be LONG RAW(n)
            + "col_binarystream    RAW(2000), " // BinaryStream; can also be LONG RAW(n)
            + "col_timestamp       DATE, " // Timestamp
            + "col_clob            CLOB, " // Clob
            + "col_blob            BLOB, " // Blob; can also be BFILE
            + "col_array           number_varray, " // oracle.sql.ARRAY
            + "col_object          my_object)"; // oracle.sql.OBJECT

    stmt.executeUpdate(sql);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();/*from w w  w .jav  a 2s  .co  m*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    ResultSet rsColumns = null;//from   www . j av a 2 s  .  c  om
    DatabaseMetaData meta = conn.getMetaData();
    rsColumns = meta.getColumns(null, null, "survey", null);
    while (rsColumns.next()) {
        System.out.println(rsColumns.getString("TYPE_NAME"));
        System.out.println(rsColumns.getString("COLUMN_NAME"));
    }
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Connection conn = null;
    Statement stmt = null;//from  w w w  . j a  va  2s .  co  m
    ResultSet rs = null;

    conn = getConnection();
    stmt = conn.createStatement();
    String excelQuery = "select * from [Sheet1$]";
    rs = stmt.executeQuery(excelQuery);

    while (rs.next()) {
        System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName"));
    }

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

From source file:Main.java

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

    Connection conn = getHSQLConnection();

    conn.setAutoCommit(false);// w ww  .java2  s  .  c o m
    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);

    checkForWarning(rs.getWarnings());

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

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String hostname = "", dbname = "", username = "", password = "";
    Class.forName("org.postgresql.Driver");
    Connection connection = DriverManager.getConnection("jdbc:postgresql://" + hostname + "/" + dbname,
            username, password);//from   ww  w  . j av a  2s . c  o m
    ResultSet rs = connection.createStatement().executeQuery("select version() as version");
    while (rs.next()) {
        System.out.println(rs.getString("version"));
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar, PRIMARY KEY (id) );");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet rs = meta.getPrimaryKeys(null, null, "survey");

    java.util.List list = new java.util.ArrayList();
    while (rs.next()) {
        String columnName = rs.getString("COLUMN_NAME");
        System.out.println("getPrimaryKeys(): columnName=" + columnName);
    }//from  w w  w.ja  va 2 s.c o m

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getOracleConnection();
    Statement stmt = null;//  www  .j a  v  a2s.  c  o  m
    ResultSet rs = null;
    stmt = conn.createStatement();
    //only for Oracle
    rs = stmt.executeQuery("select object_name from user_objects where object_type = 'VIEW'");

    while (rs.next()) {
        String tableName = rs.getString(1);
        System.out.println("viewName=" + tableName);
    }

    stmt.close();
    conn.close();
}

From source file:Main.java

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

    conn.setAutoCommit(false);//from   w w  w .  j  a v a  2  s .  c o  m
    Statement st = conn.createStatement();
    try {
        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();
}