Example usage for java.sql Statement executeQuery

List of usage examples for java.sql Statement executeQuery

Introduction

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

Prototype

ResultSet executeQuery(String sql) throws SQLException;

Source Link

Document

Executes the given SQL statement, which returns a single ResultSet object.

Usage

From source file:TestSSL.java

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

    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    Properties prop = new Properties();
    prop.setProperty("user", "scott");
    prop.setProperty("password", "tiger");
    // THIS DOES NOT WORK YET
    prop.setProperty("oracle.net.ssl_cipher_suites",
            "(ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha)");
    prop.setProperty("oracle.net.ssl_client_authentication", "false");
    prop.setProperty("oracle.net.ssl_version", "3.0");
    prop.setProperty("oracle.net.encryption_client", "REJECTED");
    prop.setProperty("oracle.net.crypto_checksum_client", "REJECTED");
    Connection conn = DriverManager.getConnection(
            "jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCPS)(HOST = dssw2k01)(PORT = 2484))) (CONNECT_DATA = (SERVICE_NAME = DSSW2K01)))",
            prop);/*from   ww w .java 2 s. c om*/
    Statement stmt = conn.createStatement();
    ResultSet rset = stmt
            .executeQuery("select 'Hello Thin driver SSL " + "tester '||USER||'!' result from dual");
    while (rset.next())
        System.out.println(rset.getString(1));
    rset.close();
    stmt.close();
    conn.close();
}

From source file:TestDSLookUp.java

public static void main(String[] args) throws SQLException, NamingException {

    Context ctx = null;//from  ww w .  jav a2 s .com
    try {
        Properties prop = new Properties();
        prop.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        prop.setProperty(Context.PROVIDER_URL, "file:/JNDI/JDBC");
        ctx = new InitialContext(prop);
    } catch (NamingException ne) {
        System.err.println(ne.getMessage());
    }

    DataSource ds = (DataSource) ctx.lookup("joe");
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery(
            "select 'Hello Thin driver data source tester '||" + "initcap(USER)||'!' result from dual");
    if (rset.next())
        System.out.println(rset.getString(1));
    rset.close();
    stmt.close();
    conn.close();
}

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  a v  a  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);
    String sql = "INSERT INTO mysql_all_table (col_binarystream) VALUES(?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    byte[] buffer = "some data".getBytes();
    pstmt.setBytes(1, buffer);
    pstmt.executeUpdate();
    pstmt.close();

    Statement stmt = connection.createStatement();
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM mysql_all_table");
    while (resultSet.next()) {
        byte[] bytes = resultSet.getBytes("col_binarystream");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Statement stmt = DriverManager.getConnection("jdbc:odbc:employee").createStatement();
    ResultSet rs = stmt.executeQuery("select lastname, firstname, id from [Sheet1$]");
    while (rs.next()) {
        String lname = rs.getString(1);
        String fname = rs.getString(2);
        int id = rs.getInt(3);
        System.out.println(fname + " " + lname + "  id : " + id);
    }/*from   w  w  w .j  a  v a2 s. c o m*/
    rs.close();
    stmt.close();
}

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();/*from  w w  w.  j ava  2 s . c  o  m*/
    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:MainClass.java

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.out.println("Usage: java JavaDBDemo <Name> <Address>");
        System.exit(1);//from w  w w  .j  av a 2s .c o  m
    }
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    String dbName = "AddressBookDB";
    String connectionURL = "jdbc:derby:" + dbName + ";create=true";
    String createString = "CREATE TABLE ADDRESSBOOKTbl (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)";
    Class.forName(driver);

    conn = DriverManager.getConnection(connectionURL);

    Statement stmt = conn.createStatement();
    stmt.executeUpdate(createString);

    PreparedStatement psInsert = conn.prepareStatement("insert into ADDRESSBOOKTbl values (?,?)");

    psInsert.setString(1, args[0]);
    psInsert.setString(2, args[1]);

    psInsert.executeUpdate();

    Statement stmt2 = conn.createStatement();
    ResultSet rs = stmt2.executeQuery("select * from ADDRESSBOOKTbl");
    System.out.println("Addressed present in your Address Book\n\n");
    int num = 0;

    while (rs.next()) {
        System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2));
    }
    rs.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection conn = null;//  w w  w.  j a v  a 2s.  co  m
    Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet r = s.executeQuery("SELECT * FROM employee");
    r.last();
    int count = r.getRow();
    r.beforeFirst();
}

From source file:JavaDBDemo.java

public static void main(String[] args) {
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    String connectionURL = "jdbc:derby:myDatabase;create=true";
    String createString = "CREATE TABLE Employee (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)";
    try {// w w w. j a  v  a2  s.co m
        Class.forName(driver);
    } catch (java.lang.ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        conn = DriverManager.getConnection(connectionURL);
        Statement stmt = conn.createStatement();
        stmt.executeUpdate(createString);

        PreparedStatement psInsert = conn.prepareStatement("insert into Employee values (?,?)");

        psInsert.setString(1, args[0]);
        psInsert.setString(2, args[1]);

        psInsert.executeUpdate();

        Statement stmt2 = conn.createStatement();
        ResultSet rs = stmt2.executeQuery("select * from Employee");
        int num = 0;
        while (rs.next()) {
            System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2));
        }
        rs.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbname";
    Connection dbConn = DriverManager.getConnection(URL, "user", "passw");
    Employee employee = new Employee(42, "AA", 9);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(employee);//w w w .  j a  v a  2 s.co  m
    byte[] employeeAsBytes = baos.toByteArray();
    PreparedStatement pstmt = dbConn.prepareStatement("INSERT INTO EMPLOYEE (emp) VALUES(?)");
    ByteArrayInputStream bais = new ByteArrayInputStream(employeeAsBytes);
    pstmt.setBinaryStream(1, bais, employeeAsBytes.length);
    pstmt.executeUpdate();
    pstmt.close();
    Statement stmt = dbConn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT emp FROM Employee");
    while (rs.next()) {
        byte[] st = (byte[]) rs.getObject(1);
        ByteArrayInputStream baip = new ByteArrayInputStream(st);
        ObjectInputStream ois = new ObjectInputStream(baip);
        Employee emp = (Employee) ois.readObject();
    }
    stmt.close();
    rs.close();
    dbConn.close();
}

From source file:Logging.java

public static void main(String args[]) throws Exception {
    FileOutputStream errors = new FileOutputStream("StdErr.txt", true);
    PrintStream stderr = new PrintStream(errors);
    PrintWriter errLog = new PrintWriter(errors, true);
    System.setErr(stderr);//from   www  .  j a v  a2s.c om

    String query = "SELECT Name,Description,Qty,Cost,Sell_Price FROM Stock";

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String name = rs.getString("Name");
            String desc = rs.getString("Description");
            int qty = rs.getInt("Qty");
            float cost = rs.getFloat("Cost");
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace(errLog);
    } catch (SQLException e) {
        System.err.println((new GregorianCalendar()).getTime());
        System.err.println("Thread: " + Thread.currentThread());
        System.err.println("ErrorCode: " + e.getErrorCode());
        System.err.println("SQLState:  " + e.getSQLState());
        System.err.println("Message:   " + e.getMessage());
        System.err.println("NextException: " + e.getNextException());
        e.printStackTrace(errLog);
        System.err.println();
    }
    stderr.close();
}