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[] argv) throws Exception {

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from   w w  w .  j  ava 2s.c  om

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);

    // Get the fetch size of a statement
    Statement stmt = connection.createStatement();
    int fetchSize = stmt.getFetchSize();

    // Set the fetch size on the statement
    stmt.setFetchSize(100);

    // Create a result set
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Change the fetch size on the result set
    resultSet.setFetchSize(100);

}

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);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    JdbcRowSet jdbcRS = new JdbcRowSetImpl(conn);
    jdbcRS.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
    String sql = "SELECT * FROM survey";
    jdbcRS.setCommand(sql);//  w  w w  .  j  ava 2s  .  c  o  m
    jdbcRS.execute();
    jdbcRS.addRowSetListener(new ExampleListener());

    while (jdbcRS.next()) {
        System.out.println("id=" + jdbcRS.getString(1));
        System.out.println("name=" + jdbcRS.getString(2));
    }
    conn.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();

    String streamingDataSql = "CREATE TABLE XML_Data (id INTEGER, Data LONG)";
    try {/*from  w w w.j a va 2 s. c  om*/
        stmt.executeUpdate("DROP TABLE XML_Data");
    } catch (SQLException se) {
        if (se.getErrorCode() == 942)
            System.out.println("Error dropping XML_Data table:" + se.getMessage());
    }
    stmt.executeUpdate(streamingDataSql);

    File f = new File("employee.xml");
    long fileLength = f.length();
    FileInputStream fis = new FileInputStream(f);
    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO XML_Data VALUES (?,?)");
    pstmt.setInt(1, 100);
    pstmt.setAsciiStream(2, fis, (int) fileLength);
    pstmt.execute();
    fis.close();
    ResultSet rset = stmt.executeQuery("SELECT Data FROM XML_Data WHERE id=100");
    if (rset.next()) {
        InputStream xmlInputStream = rset.getAsciiStream(1);
        int c;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((c = xmlInputStream.read()) != -1)
            bos.write(c);
        System.out.println(bos.toString());
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;
    Statement stmt = null;//from  w ww.j a  v a  2s .  c o  m

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    stmt = conn.createStatement();

    String sql = "CREATE DATABASE STUDENTS";
    stmt.executeUpdate(sql);
    System.out.println("Database created successfully...");

    stmt.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')");

    DatabaseMetaData meta = conn.getMetaData();
    // Oracle (and some other vendors) do not support
    // some the following methods; therefore, we need
    // to use try-catch block.
    try {/*from www  . ja v a 2  s.  com*/
        int jdbcMajorVersion = meta.getJDBCMajorVersion();
        System.out.println("jdbcMajorVersion:" + jdbcMajorVersion);
    } catch (Exception e) {
        System.out.println("jdbcMajorVersion unsupported feature");
    }

    try {
        int jdbcMinorVersion = meta.getJDBCMinorVersion();
        System.out.println("jdbcMinorVersion:" + jdbcMinorVersion);
    } catch (Exception e) {
        System.out.println("jdbcMinorVersion unsupported feature");
    }

    String driverName = meta.getDriverName();
    String driverVersion = meta.getDriverVersion();
    System.out.println("driverName=" + driverName);
    System.out.println("driverVersion=" + driverVersion);

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

From source file:GetDateFromMySql.java

public static void main(String args[]) {
    ResultSet rs = null;//from  w  ww. j a v  a2  s  .  co m
    Connection conn = null;
    Statement stmt = null;
    try {
        conn = getMySQLConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery("select timeCol, dateCol, dateTimeCol from dateTimeTable");
        while (rs.next()) {
            java.sql.Time dbSqlTime = rs.getTime(1);
            java.sql.Date dbSqlDate = rs.getDate(2);
            java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);
            System.out.println("dbSqlTime=" + dbSqlTime);
            System.out.println("dbSqlDate=" + dbSqlDate);
            System.out.println("dbSqlTimestamp=" + dbSqlTimestamp);

            java.util.Date dbSqlTimeConverted = new java.util.Date(dbSqlTime.getTime());
            java.util.Date dbSqlDateConverted = new java.util.Date(dbSqlDate.getTime());
            System.out.println("in standard date");
            System.out.println(dbSqlTimeConverted);
            System.out.println(dbSqlDateConverted);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

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);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    JdbcRowSet jdbcRS;/*from   w  w  w .java2s .  c  o  m*/
    jdbcRS = new JdbcRowSetImpl(conn);
    jdbcRS.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
    String sql = "SELECT * FROM survey";
    jdbcRS.setCommand(sql);
    jdbcRS.execute();
    jdbcRS.addRowSetListener(new ExampleListener());

    while (jdbcRS.next()) {
        // each call to next, generates a cursorMoved event
        System.out.println("id=" + jdbcRS.getString(1));
        System.out.println("name=" + jdbcRS.getString(2));
    }
    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 indexInformation = null;
    DatabaseMetaData meta = conn.getMetaData();

    // The '_' character represents any single character.
    // The '%' character represents any sequence of zero
    // or more characters.
    indexInformation = meta.getIndexInfo(conn.getCatalog(), null, "survey", true, true);
    while (indexInformation.next()) {
        short type = indexInformation.getShort("TYPE");
        switch (type) {
        case DatabaseMetaData.tableIndexClustered:
            System.out.println("tableIndexClustered");
        case DatabaseMetaData.tableIndexHashed:
            System.out.println("tableIndexHashed");
        case DatabaseMetaData.tableIndexOther:
            System.out.println("tableIndexOther");
        case DatabaseMetaData.tableIndexStatistic:
            System.out.println("tableIndexStatistic");
        default:/*from w ww  .j av  a  2s.  c o m*/
            System.out.println("tableIndexOther");
        }

    }

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

From source file:Main.java

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

    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myDate DATE);");
    String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
    pstmt.setDate(2, sqlDate);//from   ww w. ja  v a 2 s .  co  m

    pstmt.executeUpdate();

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

    ResultSetMetaData rsmd = rs.getMetaData();

    int numCols = rsmd.getColumnCount();

    System.out.print("\t");
    for (int i = 1; i <= numCols; i++) {
        System.out.println(rsmd.getColumnLabel(i));
    }

    System.out.print("\nAuto Increment\t");
    for (int i = 1; i <= numCols; i++) {
        System.out.println(rsmd.isAutoIncrement(i));
    }
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isCaseSensitive(i));
    }
    System.out.print("\nSearchable\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isSearchable(i));
    }
    System.out.print("\nCurrency\t");
    for (int i = 1; i <= numCols; i++) {
        System.out.println(rsmd.isCurrency(i));
    }
    System.out.print("\nAllows nulls\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isNullable(i));
    }
    System.out.print("\nSigned\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isSigned(i));
    }
    System.out.print("\nRead only\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isReadOnly(i));
    }
    System.out.print("\nWritable\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.print(rsmd.isWritable(i));
    }
    System.out.print("\nDefinitely Writable\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isDefinitelyWritable(i));
    }

    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    System.setProperty("oracle.net.tns_admin", "C:/app/product/11.2.0/client_1/NETWORK/ADMIN");
    String dbURL = "jdbc:oracle:thin:@ENTRY_FROM_TNSNAMES";

    Class.forName("oracle.jdbc.OracleDriver");

    Connection conn = DriverManager.getConnection(dbURL, "your_user_name", "your_password");

    System.out.println("Connection established");

    Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery("SELECT dummy FROM dual");

    if (rs.next()) {
        System.out.println(rs.getString(1));
    }/*from w  ww . j  a  v  a2  s  .co m*/
    stmt.close();
    conn.close();
}