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

    ResultSet rs = null;// w w w .ja  va2  s . c o m
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTableTypes();

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

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

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

    ResultSet rs = null;//from  w w  w.  java2s . c om
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTables(null, null, null, new String[] { "TABLE" });

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

    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 rs = null;//from   ww w  . ja v a  2  s .c om
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTables(null, null, null, new String[] { "TABLE", "VIEW" });
    while (rs.next()) {
        String tableOrViewName = rs.getString("TABLE_NAME");
        System.out.println("getTableNames(): tableOrViewName=" + tableOrViewName);
    }

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

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet schemas = meta.getSchemas();
    while (schemas.next()) {
        String tableSchema = schemas.getString(1); // "TABLE_SCHEM"
        String tableCatalog = schemas.getString(2); //"TABLE_CATALOG"
        System.out.println("tableSchema" + tableSchema);
    }/*from  w w  w  .ja va  2s.c  o m*/

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("Oracle driver registered");
    Connection conn = null;
    conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orclh", "scott", "tiger");

    Statement stmt = conn.createStatement();
    ResultSet r = stmt.executeQuery("Select * from emp");
    while (r.next()) {
        String str = r.getString("ename");
        System.out.println(str);// w  ww .j ava2 s  . c o m
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySQLConnection();
    Statement stmt = null;//from w w w  . jav  a 2 s .  c  o  m
    try {
        stmt = conn.createStatement();
        stmt.executeUpdate("DELETE FROM Mytable");
        displayError(stmt.getWarnings());
        stmt.executeUpdate(
                "INSERT INTO Mytable(id, name)" + "VALUES(1, 'NameLongerThanColumnLengthInDatabase')");
        displayError(stmt.getWarnings());
    } catch (DataTruncation dt) {
        displayError(dt);
        dt.printStackTrace();
    } catch (SQLException se) {
        System.out.println("Database error message: " + se.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        stmt.close();
        conn.close();
    }
}

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("create view surveyView as (select * from survey);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    ResultSet rs = null;//w  ww. ja v a  2s .  c  o  m
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTables(null, null, null, new String[] { "VIEW" });

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

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

From source file:DemoDataTruncation.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySQLConnection();
    Statement stmt = null;//  w  w w  .ja v  a 2 s  .  c  om
    try {
        stmt = conn.createStatement();
        stmt.executeUpdate("DELETE FROM animals_table");
        displayError(stmt.getWarnings());
        stmt.executeUpdate(
                "INSERT INTO animals_table(id, name)" + "VALUES(1, 'NameLongerThanColumnLengthInDatabase')");
        displayError(stmt.getWarnings());
    } catch (DataTruncation dt) {
        displayError(dt);
        dt.printStackTrace();
    } catch (SQLException se) {
        System.out.println("Database error message: " + se.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        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')");

    ResultSet catalogs = null;//  ww  w.ja v  a2  s.c  o m
    DatabaseMetaData meta = conn.getMetaData();
    catalogs = meta.getCatalogs();

    while (catalogs.next()) {
        String catalog = catalogs.getString(1); //"TABLE_CATALOG"
        System.out.println("catalog: " + catalog);
    }

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

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection c = DriverManager
            .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb");

    ResultSet rs = c.createStatement().executeQuery("select objet from Employee");

    while (rs.next()) {
        System.out.println(rs.getString("objet"));
    }/* w w w  .j  a  v  a 2  s. c  o  m*/
    rs.close();
    c.close();
}