Package java.sql

Examples of java.sql.Connection


        return conn;
    }

    public static Connection getConnection(@Nonnull String connectUrl, @Nullable String userName, @Nullable String password)
            throws SQLException {
        final Connection conn;
        if(userName == null) {
            conn = DriverManager.getConnection(connectUrl);
        } else {
            conn = DriverManager.getConnection(connectUrl, userName, password);
        }
View Full Code Here


     */
    public static void closeAll(ResultSet rs) throws SQLException {
        if(rs != null) {
            Statement stmt = rs.getStatement();
            if(stmt != null) {
                Connection conn = stmt.getConnection();
                stmt.close();
                closeQuietly(conn);
            } else {
                rs.close();
            }
View Full Code Here

    /**
     * Close a <code>Statement</code>, avoid closing if null.
     */
    public static void closeAll(Statement stmt) throws SQLException {
        if(stmt != null) {
            Connection conn = stmt.getConnection();
            stmt.close();
            closeQuietly(conn);
        }
    }
View Full Code Here

        }
        if(driver == null) {//sanity check
            throw new SQLException("Driver is not registered: " + driverClass);
        }
        */
        final Connection conn;
        if(userName == null) {
            conn = DriverManager.getConnection(dbUrl);
        } else {
            conn = DriverManager.getConnection(dbUrl, userName, password);
        }
View Full Code Here

        TestBase.createCaller().init().test();
    }

    public void test() throws Exception {
        deleteDb("queryCache");
        Connection conn = getConnection("queryCache;QUERY_CACHE_SIZE=10");
        Statement stat = conn.createStatement();
        stat.execute("create table test(id int, name varchar) as select x, space(100) from system_range(1, 1000)");
        PreparedStatement prep;
        conn.prepareStatement("select count(*) from test t1, test t2");
        long time;
        ResultSet rs;
        long first = 0;
        for (int i = 0; i < 4; i++) {
            // this should both ensure results are not re-used
            // stat.execute("set mode regular");
            // stat.execute("create table x()");
            // stat.execute("drop table x");
            time = System.currentTimeMillis();
            prep = conn.prepareStatement("select count(*) from test t1, test t2");
            prep.executeQuery();
            rs = stat.executeQuery("select count(*) from test t1, test t2");
            rs.next();
            int c = rs.getInt(1);
            assertEquals(1000000, c);
            time = System.currentTimeMillis() - time;
            if (first == 0) {
                first = time;
            } else {
                assertSmaller(time, first);
            }
        }
        conn.close();
        deleteDb("queryCache");
    }
View Full Code Here

        deleteDb("sessionsLocks");
    }

    private void testLocks() throws SQLException {
        deleteDb("sessionsLocks");
        Connection conn = getConnection("sessionsLocks;MULTI_THREADED=1");
        Statement stat = conn.createStatement();
        ResultSet rs;
        rs = stat.executeQuery("select * from information_schema.locks order by session_id");
        assertFalse(rs.next());
        Connection conn2 = getConnection("sessionsLocks");
        Statement stat2 = conn2.createStatement();
        stat2.execute("create table test(id int primary key, name varchar)");
        conn2.setAutoCommit(false);
        stat2.execute("insert into test values(1, 'Hello')");
        rs = stat.executeQuery("select * from information_schema.locks order by session_id");
        rs.next();
        assertEquals("PUBLIC", rs.getString("TABLE_SCHEMA"));
        assertEquals("TEST", rs.getString("TABLE_NAME"));
        rs.getString("SESSION_ID");
        if (config.mvcc) {
            assertEquals("READ", rs.getString("LOCK_TYPE"));
        } else {
            assertEquals("WRITE", rs.getString("LOCK_TYPE"));
        }
        assertFalse(rs.next());
        conn2.commit();
        conn2.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        stat2.execute("SELECT * FROM TEST");
        rs = stat.executeQuery("select * from information_schema.locks order by session_id");
        if (!config.mvcc) {
            rs.next();
            assertEquals("PUBLIC", rs.getString("TABLE_SCHEMA"));
            assertEquals("TEST", rs.getString("TABLE_NAME"));
            rs.getString("SESSION_ID");
            assertEquals("READ", rs.getString("LOCK_TYPE"));
        }
        assertFalse(rs.next());
        conn2.commit();
        rs = stat.executeQuery("select * from information_schema.locks order by session_id");
        assertFalse(rs.next());
        conn.close();
        conn2.close();
    }
View Full Code Here

public class TestJDBCProcedureExecution {
 
  @Test public void testProcedureExecution() throws Exception {
    Command command = TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "exec pm2.spTest8a()"); //$NON-NLS-1$
    Connection connection = Mockito.mock(Connection.class);
    CallableStatement cs = Mockito.mock(CallableStatement.class);
    Mockito.stub(cs.getUpdateCount()).toReturn(-1);
    Mockito.stub(cs.getInt(1)).toReturn(5);
    Mockito.stub(connection.prepareCall("{  call spTest8a(?)}")).toReturn(cs); //$NON-NLS-1$
    JDBCExecutionFactory ef = new JDBCExecutionFactory();
   
    JDBCProcedureExecution procedureExecution = new JDBCProcedureExecution(command, connection, Mockito.mock(ExecutionContext.class),  ef);
    procedureExecution.execute();
    assertEquals(Arrays.asList(5), procedureExecution.getOutputParameterValues());
View Full Code Here

        conn2.close();
    }

    private void testCancelStatement() throws Exception {
        deleteDb("sessionsLocks");
        Connection conn = getConnection("sessionsLocks;MULTI_THREADED=1");
        Statement stat = conn.createStatement();
        ResultSet rs;
        rs = stat.executeQuery("select * from information_schema.sessions order by SESSION_START, ID");
        rs.next();
        int sessionId = rs.getInt("ID");
        rs.getString("USER_NAME");
        rs.getTimestamp("SESSION_START");
        rs.getString("STATEMENT");
        rs.getTimestamp("STATEMENT_START");
        assertFalse(rs.next());
        Connection conn2 = getConnection("sessionsLocks");
        final Statement stat2 = conn2.createStatement();
        rs = stat.executeQuery("select * from information_schema.sessions order by SESSION_START, ID");
        assertTrue(rs.next());
        assertEquals(sessionId, rs.getInt("ID"));
        assertTrue(rs.next());
        int otherId = rs.getInt("ID");
        assertTrue(otherId != sessionId);
        assertFalse(rs.next());
        stat2.execute("set throttle 1");
        final boolean[] done = { false };
        Runnable runnable = new Runnable() {
            public void run() {
                try {
                    stat2.execute("select count(*) from system_range(1, 10000000) t1, system_range(1, 10000000) t2");
                    new Error("Unexpected success").printStackTrace();
                } catch (SQLException e) {
                    done[0] = true;
                }
            }
        };
        new Thread(runnable).start();
        while (true) {
            Thread.sleep(100);
            rs = stat.executeQuery("CALL CANCEL_SESSION(" + otherId + ")");
            rs.next();
            if (rs.getBoolean(1)) {
                for (int i = 0; i < 20; i++) {
                    Thread.sleep(100);
                    if (done[0]) {
                        break;
                    }
                }
                assertTrue(done[0]);
                break;
            }
        }
        conn2.close();
        conn.close();
    }
View Full Code Here

    assertEquals(Arrays.asList(5), procedureExecution.getOutputParameterValues());
    Mockito.verify(cs, Mockito.times(1)).registerOutParameter(1, Types.INTEGER);
  }
  @Test public void testProcedureExecution1() throws Exception {
    Command command = TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "exec pm2.spTest8(1)"); //$NON-NLS-1$
    Connection connection = Mockito.mock(Connection.class);
    CallableStatement cs = Mockito.mock(CallableStatement.class);
    Mockito.stub(cs.getUpdateCount()).toReturn(-1);
    Mockito.stub(cs.getInt(2)).toReturn(5);
    Mockito.stub(connection.prepareCall("{  call spTest8(?,?)}")).toReturn(cs); //$NON-NLS-1$
    JDBCExecutionFactory config = new JDBCExecutionFactory();

    JDBCProcedureExecution procedureExecution = new JDBCProcedureExecution(command, connection, Mockito.mock(ExecutionContext.class), config);
    procedureExecution.execute();
    assertEquals(Arrays.asList(5), procedureExecution.getOutputParameterValues());
View Full Code Here

    value.setBindValue(true);
    value.setValue(Arrays.asList(1, 2));
    value1.setMultiValued(true);
    value1.setBindValue(true);
    value1.setValue(Arrays.asList(2, 3));
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement p = Mockito.mock(PreparedStatement.class);
    Mockito.stub(p.executeBatch()).toReturn(new int [] {1, 1});
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey, IntNum) VALUES (?, ?)")).toReturn(p); //$NON-NLS-1$
   
    JDBCExecutionFactory config = new JDBCExecutionFactory();
   
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, Mockito.mock(ExecutionContext.class), config);
    updateExecution.execute();
View Full Code Here

TOP

Related Classes of java.sql.Connection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.