Example usage for java.sql Statement execute

List of usage examples for java.sql Statement execute

Introduction

In this page you can find the example usage for java.sql Statement execute.

Prototype

boolean execute(String sql) throws SQLException;

Source Link

Document

Executes the given SQL statement, which may return multiple results.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
            + "Dbq=C:\\Users\\Public\\uls\\ulsTest.mdb;" + "SystemDB=C:\\Users\\Public\\uls\\Security.mdw;"
            + "Uid=Gord;" + "Pwd=obfuscated;" + "ExtendedAnsiSQL=1;");
    Statement s = conn.createStatement();
    s.execute("CREATE USER Tim pwd");
    System.out.println("User 'Tim' created.");
    s.execute("DROP USER Tim");
    System.out.println("User 'Tim' dropped.");
    s.close();/*w  w w.  ja va2 s  . com*/
    conn.close();
}

From source file:DataInserter.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    DriverManager.registerDriver(new JdbcOdbcDriver());
    String SQLCommand = "INSERT INTO CONTACT_INFO " + "(First_Name,MI,Last_Name,Street,City,State,Zip) "
            + "VALUES " + "('Michael','J','Corleone','86 Horsehead Blvd','NY','NY','12345');";
    String url = "jdbc:odbc:Contacts";
    Connection con = DriverManager.getConnection(url);
    Statement stmt = con.createStatement();
    stmt.execute(SQLCommand);
    con.close();//from www .  j a va  2s  .  co  m

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
            + "DBQ=C:\\Users\\Public\\uls\\ulsTest.mdb;" + "SystemDB=C:\\Users\\Public\\uls\\Security.mdw;"
            + "Uid=Gord;" + "Pwd=obfuscated;" + "ExtendedAnsiSQL=1;");

    String UID = "Tim";
    String oldPWD = "oldpassword";
    String newPWD = "I like Java";

    Statement s = conn.createStatement();
    s.execute("ALTER USER " + UID + " PASSWORD \"" + newPWD + "\" \"" + oldPWD + "\"");

    s.close();//from  w  w w .ja v a 2  s . co  m
    conn.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String connectionURL = "jdbc:mysql://localhost:3306/test";
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(connectionURL, "login", "password");
    Statement stmt = con.createStatement();
    stmt.execute("CREATE TRIGGER obs_update BEFORE UPDATE ON obs " //
            + "FOR EACH ROW "//
            + "BEGIN "//
            + "IF OLD.voided = 0 AND NEW.voided = 1 THEN "//
            + "   DELETE FROM emp WHERE id = OLD.obs_id; "//
            + "ELSE "//
            + "   UPDATE emp SET emp.revision_token = NOW() "//
            + "   WHERE NEW.id = emp.id; "//
            + "END IF; "//
            + "END;");
    con.close();//  ww w . j  a  v  a2s  .  co m
}

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);
    connection.close();/* ww w  .  j a v  a2 s.  c o m*/
}

From source file:ExecuteExample.java

public static void main(String args[]) {
    try {/*w  ww. ja  v a2 s . c o m*/
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    } catch (Exception e) {
        System.out.println("JDBC-ODBC driver failed to load.");
        return;
    }

    try {
        Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", "");
        Statement stmt = con.createStatement();
        stmt.execute("CREATE TABLE SalesHistory(ProductID NUMBER,Price CURRENCY, TrnsDate DATE)");
        stmt.close();
        con.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);// www  . j a  va2s. c  om

    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 object2 AS OBJECT(col_string2 VARCHAR(30), col_integer2 NUMBER)");
    stmt.execute("CREATE TYPE object1 AS OBJECT(col_string1 VARCHAR(30), col_integer2 object2)");
    stmt.execute("CREATE TABLE object1_table(col_integer NUMBER, col_object1 object1)");
    stmt.execute("INSERT INTO object1_table VALUES(1, object1('str1', object2('obj2str1', 123)))");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection con = DriverManager.getConnection("jdbc:h2:mem:");
    Statement s = con.createStatement();
    s.execute("CREATE TABLE Table1 (Column1 CLOB)");

    InputStream is = new FileInputStream("data.txt");
    Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1);
    PreparedStatement ps = con.prepareStatement("INSERT INTO Table1 (Column1) VALUES (?)");
    ps.setCharacterStream(1, rdr);/*from  w ww .  j  a  va 2s .c o  m*/
    ps.executeUpdate();

    ResultSet rs = s.executeQuery("SELECT Column1 FROM Table1");
    int rowNumber = 0;
    while (rs.next()) {
        String str = rs.getString("Column1");
        System.out.println(String.format("Row %d: CLOB is %d character(s) long.", ++rowNumber, str.length()));
    }
    rs.close();
    con.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  a 2 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();

    stmt.execute("CREATE TYPE object2 AS OBJECT(col_string2 VARCHAR(30), col_integer2 NUMBER)");

    stmt.execute("CREATE TYPE object1 AS OBJECT(col_string1 VARCHAR(30), col_integer2 object2)");

    stmt.execute("CREATE TABLE object1_table(col_integer NUMBER, col_object1 object1)");
}

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  ava  2s  .com

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

}