Example usage for java.sql Struct getAttributes

List of usage examples for java.sql Struct getAttributes

Introduction

In this page you can find the example usage for java.sql Struct getAttributes.

Prototype

Object[] getAttributes() throws SQLException;

Source Link

Document

Produces the ordered values of the attributes of the SQL structured type that this Struct object represents.

Usage

From source file:InsertStores.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;/*from   w  w  w.  j  a v a 2  s .co m*/
    Statement stmt;
    try {
        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {

        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        stmt = con.createStatement();
        con.setAutoCommit(false);

        String insertStore1 = "INSERT INTO STORES VALUES (" + "100001, "
                + "ADDRESS(888, 'Main_Street', 'Rancho_Alegre', " + "'CA', '94049'), "
                + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', "
                + "'Colombian_Decaf', 'French_Roast_Decaf'), "
                + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000001))";

        stmt.addBatch(insertStore1);

        String insertStore2 = "INSERT INTO STORES VALUES (" + "100002, "
                + "ADDRESS(1560, 'Alder', 'Ochos_Pinos', " + "'CA', '94049'), "
                + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', "
                + "'Colombian_Decaf', 'French_Roast_Decaf', " + "'Kona', 'Kona_Decaf'), "
                + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000001))";

        stmt.addBatch(insertStore2);

        String insertStore3 = "INSERT INTO STORES VALUES (" + "100003, "
                + "ADDRESS(4344, 'First_Street', 'Verona', " + "'CA', '94545'), "
                + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', "
                + "'Colombian_Decaf', 'French_Roast_Decaf', " + "'Kona', 'Kona_Decaf'), "
                + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000002))";

        stmt.addBatch(insertStore3);

        String insertStore4 = "INSERT INTO STORES VALUES (" + "100004, "
                + "ADDRESS(321, 'Sandy_Way', 'La_Playa', " + "'CA', '94544'), "
                + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', "
                + "'Colombian_Decaf', 'French_Roast_Decaf', " + "'Kona', 'Kona_Decaf'), "
                + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000002))";

        stmt.addBatch(insertStore4);

        String insertStore5 = "INSERT INTO STORES VALUES (" + "100005, "
                + "ADDRESS(1000, 'Clover_Road', 'Happyville', " + "'CA', '90566'), "
                + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', "
                + "'Colombian_Decaf', 'French_Roast_Decaf'), "
                + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000003))";

        stmt.addBatch(insertStore5);

        int[] updateCounts = stmt.executeBatch();

        ResultSet rs = stmt.executeQuery("SELECT * FROM STORES");

        System.out.println("Table STORES after insertion:");
        System.out.println("STORE_NO  LOCATION          COF_TYPE     MGR");
        while (rs.next()) {
            int storeNo = rs.getInt("STORE_NO");
            Struct location = (Struct) rs.getObject("LOCATION");
            Object[] locAttrs = location.getAttributes();
            Array coffeeTypes = rs.getArray("COF_TYPE");
            String[] cofTypes = (String[]) coffeeTypes.getArray();

            Ref managerRef = rs.getRef("MGR");
            PreparedStatement pstmt = con.prepareStatement("SELECT MANAGER FROM MANAGERS WHERE OID = ?");
            pstmt.setRef(1, managerRef);
            ResultSet rs2 = pstmt.executeQuery();
            rs2.next();
            Struct manager = (Struct) rs2.getObject("MANAGER");
            Object[] manAttrs = manager.getAttributes();

            System.out.print(storeNo + "   ");
            System.out.print(locAttrs[0] + " " + locAttrs[1] + " " + locAttrs[2] + ", " + locAttrs[3] + "  "
                    + locAttrs[4] + " ");
            for (int i = 0; i < cofTypes.length; i++)
                System.out.print(cofTypes[i] + " ");
            System.out.println(manAttrs[1] + ", " + manAttrs[2]);

            rs2.close();
            pstmt.close();
        }

        rs.close();
        stmt.close();
        con.close();

    } catch (BatchUpdateException b) {
        System.err.println("-----BatchUpdateException-----");
        System.err.println("SQLState:  " + b.getSQLState());
        System.err.println("Message:  " + b.getMessage());
        System.err.println("Vendor:  " + b.getErrorCode());
        System.err.print("Update counts:  ");
        int[] updateCounts = b.getUpdateCounts();
        for (int i = 0; i < updateCounts.length; i++) {
            System.err.print(updateCounts[i] + "   ");
        }
        System.err.println("");

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        System.err.println("SQLState:  " + ex.getSQLState());
        System.err.println("Message:  " + ex.getMessage());
        System.err.println("Vendor:  " + ex.getErrorCode());
    }
}

From source file:com.nextep.designer.sqlgen.oracle.debug.ctrl.DebugMethod.java

@Override
public Object invokeMethod(Object... arg) {

    IConnection conn = (IConnection) arg[0];

    CallableStatement stmt = null;
    Thread debuggedThread = null;
    try {//from w ww.  j  a  v  a2s  . c o  m
        // Initializing our target connection
        targetConn = CorePlugin.getConnectionService().connect(conn);
        //
        stmt = targetConn.prepareCall("ALTER SESSION SET PLSQL_DEBUG=TRUE"); //$NON-NLS-1$
        try {
            stmt.execute();
        } finally {
            CaptureHelper.safeClose(null, stmt);
        }

        stmt = targetConn.prepareCall("{ ? = CALL DBMS_DEBUG.INITIALIZE() }"); //$NON-NLS-1$
        try {
            stmt.registerOutParameter(1, Types.VARCHAR);
            stmt.execute();
            debugSessionID = stmt.getString(1);
        } catch (SQLException e) {
            throw new ErrorException(e);
        } finally {
            CaptureHelper.safeClose(null, stmt);
        }

        // Switching to debug mode
        stmt = targetConn.prepareCall("{ CALL DBMS_DEBUG.DEBUG_ON() }"); //$NON-NLS-1$
        try {
            stmt.execute();
        } finally {
            CaptureHelper.safeClose(null, stmt);
        }

        // Starting our target code
        debuggedThread = new Thread(new TargetRunnable(targetConn));
        debuggedThread.start();

        // Now that we have our ID, we initialize debug connection
        debugConn = CorePlugin.getConnectionService().connect(conn);
        // new Thread(new DebugRunnable(debugConn,debugSessionID)).start();

        stmt = debugConn.prepareCall("{ CALL DBMS_DEBUG.ATTACH_SESSION(?) }"); //$NON-NLS-1$
        try {
            stmt.setString(1, debugSessionID);
            stmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            CaptureHelper.safeClose(null, stmt);
        }

        stmt = debugConn.prepareCall("{ ? = CALL DBMS_DEBUG.SYNCHRONIZE(?,0) }"); //$NON-NLS-1$
        try {
            stmt.registerOutParameter(1, Types.INTEGER);
            stmt.registerOutParameter(2, OracleTypes.OTHER, "DBMS_DEBUG.RUNTIME_INFO"); //$NON-NLS-1$
            stmt.execute();
            Object o = stmt.getObject(2);
            if (o != null) {

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            CaptureHelper.safeClose(null, stmt);
        }
        // // Setting breakpoints
        // stmt =
        // debugConn.prepareCall("{ call adp_debug.set_breakpoint(p_line=>?, p_name=>?, p_body=>true) }");
        // try {
        // for(IBreakpoint bp : SQLEditorUIServices.getInstance().getBreakpoints()) {
        // stmt.setInt(1, bp.getLine());
        // stmt.setString(2,bp.getTarget().getName());
        // stmt.execute();
        // }
        // } catch( Exception e) {
        // e.printStackTrace();
        // } finally {
        // stmt.close();
        // }
        stmt = debugConn.prepareCall("{ ? = CALL DBMS_DEBUG.CONTINUE(?,0,46) }"); //$NON-NLS-1$
        stmt.registerOutParameter(1, Types.INTEGER);
        stmt.registerOutParameter(2, OracleTypes.OTHER, "DBMS_DEBUG.RUNTIME_INFO"); //$NON-NLS-1$

        try {
            stmt.execute();
            Struct struct = (Struct) stmt.getObject(2);
            Object[] attrs = struct.getAttributes();
            int line = (Integer) attrs[0];
            int terminated = (Integer) attrs[1];
            int breakpoint = (Integer) attrs[2];
            LOGGER.debug(
                    "Continued to line " + line + ", terminated=" + terminated + ", breakpoint=" + breakpoint);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            CaptureHelper.safeClose(null, stmt);
        }

    } catch (SQLException e) {
        if (debuggedThread != null) {
            debuggedThread.interrupt();
        }
        throw new ErrorException(e);
    } finally {
        try {
            if (debugConn != null) {
                debugConn.close();
            }
        } catch (SQLException e) {
            throw new ErrorException("Unable to properly close connection: " + e.getMessage(), e);
        }
    }

    return null;
}