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 = getMySqlConnection();
    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 rs = meta.getExportedKeys(conn.getCatalog(), null, "survey");
    while (rs.next()) {
        String fkTableName = rs.getString("FKTABLE_NAME");
        String fkColumnName = rs.getString("FKCOLUMN_NAME");
        int fkSequence = rs.getInt("KEY_SEQ");
        System.out.println("getExportedKeys(): fkTableName=" + fkTableName);
        System.out.println("getExportedKeys(): fkColumnName=" + fkColumnName);
        System.out.println("getExportedKeys(): fkSequence=" + fkSequence);
    }/* w  ww  . ja  v  a2  s . c  o  m*/

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

From source file:Main.java

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

    conn.setAutoCommit(false);//from w ww  .  j  a  v a  2 s . com
    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");

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

From source file:CallableStmt.java

public static void main(String args[]) throws Exception {
    String storedProc = "create procedure SHOW_ORDERS_BY_STATE @State CHAR (2) as "
            + "select c.Last_Name+', '+c.First_Name AS Name,o.Order_Number "
            + "from CUSTOMERS c, ORDERS o where c.Customer_Number = o.Customer_Number "
            + "AND c.State = @State order by c.Last_Name;";

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Customers");
    Statement stmt = con.createStatement();
    stmt.executeUpdate(storedProc);/*w  ww .j a  v  a  2  s  .c  om*/
    CallableStatement cs = con.prepareCall("{call SHOW_ORDERS_BY_STATE(?)}");
    cs.setString(1, "NJ");
    ResultSet rs = cs.executeQuery();
    while (rs.next()) {
        String name = rs.getString("Name");
        int orderNo = rs.getInt("Order_Number");
        System.out.println(name + ": " + orderNo);
    }
}

From source file:ConnPool.java

public static void main(String[] args) throws Exception {
    OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();
    ocpds.setURL("jdbc:oracle:thin:@localhost:1521:ORCL");
    ocpds.setUser("user");
    ocpds.setPassword("password");

    PooledConnection pc_1 = ocpds.getPooledConnection();

    Connection conn_1 = pc_1.getConnection();
    Statement stmt = conn_1.createStatement();

    ResultSet rs = stmt.executeQuery("SELECT count(*) FROM v$session WHERE username = 'SYS'");
    rs.next();/*  www.ja  v  a2s. c om*/
    String msg = "Total connections after ";
    System.out.println(msg + "conn_1: " + rs.getString(1));

    Connection conn_2 = pc_1.getConnection();
    stmt = conn_2.createStatement();
    rs = stmt.executeQuery("SELECT count(*) FROM v$session WHERE username = 'SYS'");
    rs.next();
    System.out.println(msg + "conn_2: " + rs.getString(1));

    PooledConnection pc_2 = ocpds.getPooledConnection();
    rs = stmt.executeQuery("SELECT count(*) FROM v$session WHERE username = 'SYS'");
    rs.next();
    System.out.println(msg + "pc_2: " + rs.getString(1));

    conn_1.close();
    conn_2.close();
    pc_1.close();
    pc_2.close();
}

From source file:Main.java

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

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

    String url = "jdbc:oracle:thin:@localhost:1521:javaDemo";
    String username = "username";
    String password = "welcome";

    String sql = "CREATE TABLE books (id NUMBER(11), title VARCHAR2(64))";
    Connection connection = DriverManager.getConnection(url, username, password);
    Statement statement = connection.createStatement();
    statement.execute(sql);//from ww  w.j ava 2s  .  c  o m
    connection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);//from   w w w  .  j  a  v  a2 s  .c o  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();

    String procedure = "CREATE OR REPLACE PROCEDURE myprocout(x OUT VARCHAR) IS BEGIN "
            + "INSERT INTO oracle_table VALUES('string 2'); x := 'outvalue'; END;";
    stmt.executeUpdate(procedure);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from  www  .j a va 2 s.  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);
    Statement stmt = connection.createStatement();
    String sql = "UPDATE my_table SET col_string='a new string' WHERE col_string = 'a string'";
    int updateCount = stmt.executeUpdate(sql);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*  ww w. jav  a 2s  . co  m*/

    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);
    Statement stmt = connection.createStatement();
    String sql = "INSERT INTO my_table (col_string) VALUES('a string')";

    stmt.executeUpdate(sql);
}

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

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

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    pstmt.executeUpdate();/*from  www .  j  a v a  2s.  c om*/

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

    pstmt.setString(1, "2");
    pstmt.executeUpdate();

    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 = getMySqlConnection();
    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 rs = meta.getImportedKeys(conn.getCatalog(), null, "survey");
    while (rs.next()) {
        String fkTableName = rs.getString("FKTABLE_NAME");
        String fkColumnName = rs.getString("FKCOLUMN_NAME");
        int fkSequence = rs.getInt("KEY_SEQ");
        System.out.println("getExportedKeys(): fkTableName=" + fkTableName);
        System.out.println("getExportedKeys(): fkColumnName=" + fkColumnName);
        System.out.println("getExportedKeys(): fkSequence=" + fkSequence);
    }// w ww.  j  a va  2  s .  co  m

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