Java Code Examples for org.apache.calcite.avatica.AvaticaConnection#createStatement()

The following examples show how to use org.apache.calcite.avatica.AvaticaConnection#createStatement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcRemoteTest.java    From Quicksql with MIT License 6 votes vote down vote up
@Test
public void testPrepareStatementQuery() {
    try {
        AvaticaConnection conn = getConnection();
        final AvaticaStatement statement = conn.createStatement();
        PreparedStatement preparedStatement = conn.prepareStatement("select * from (values (1, 'a'), (2, 'b'), "
            + "(3, 'c')) "
            + "where expr_col__0 = ? or expr_col__1 = ?");
        preparedStatement.setInt(1,1);
        preparedStatement.setString(2,"b");
        ResultSet rs = preparedStatement.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
        }
        close(rs, statement,conn);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: JdbcRemoteTest.java    From Quicksql with MIT License 6 votes vote down vote up
@Test
public void testQueryMetaData() {
    try {
        AvaticaConnection conn = getConnection();
        final AvaticaStatement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery("select * from (values (1, 'a'), (2, 'b'))");
        if (rs.getMetaData() != null) {
            System.out.println(rs.getMetaData().getColumnCount());
            System.out.println(rs.getMetaData().getColumnName(1));
            System.out.println(rs.getMetaData().getColumnName(2));
        }
        close(rs, statement,conn);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: RemoteMetaTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Test public void testRemoteStatementInsert() throws Exception {
  ConnectionSpec.getDatabaseLock().lock();
  try {
    final String t = AvaticaUtils.unique("TEST_TABLE2");
    AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
    Statement statement = conn.createStatement();
    final String create =
        String.format(Locale.ROOT, "create table if not exists %s ("
            + "  id int not null, msg varchar(255) not null)", t);
    int status = statement.executeUpdate(create);
    assertEquals(status, 0);

    statement = conn.createStatement();
    final String update =
        String.format(Locale.ROOT, "insert into %s values ('%d', '%s')",
            t, RANDOM.nextInt(Integer.MAX_VALUE), UUID.randomUUID());
    status = statement.executeUpdate(update);
    assertEquals(status, 1);
  } finally {
    ConnectionSpec.getDatabaseLock().unlock();
  }
}
 
Example 4
Source File: RemoteMetaTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Ignore("[CALCITE-942] AvaticaConnection should fail-fast when closed.")
@Test public void testRemoteConnectionClosing() throws Exception {
  AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
  // Verify connection is usable
  conn.createStatement();
  conn.close();

  // After closing the connection, it should not be usable anymore
  try {
    conn.createStatement();
    fail("expected exception");
  } catch (SQLException e) {
    assertThat(e.getMessage(),
        containsString("Connection is closed"));
  }
}
 
Example 5
Source File: JdbcRemoteTest.java    From Quicksql with MIT License 5 votes vote down vote up
@Test
public void testExecuteQuery() {
    try {
        AvaticaConnection conn = getConnection();
        final AvaticaStatement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery("select * from (values (1, 'a'), (2, 'b'))");
        while (rs.next()) {
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
        }
        close(rs, statement,conn);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: JdbcRemoteTest.java    From Quicksql with MIT License 5 votes vote down vote up
@Test
public void testInsertQuery() {
    try {
        AvaticaConnection conn = getConnection();
        final AvaticaStatement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery("INSERT INTO `hdfs://cluster:9000/` IN HDFS SELECT 1");
        while (rs.next()) {
            System.out.println(rs.getInt(1));
        }
        close(rs, statement,conn);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: ConnectionPropertiesTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectionPropertiesSync() throws Exception {
  ConnectionSpec.getDatabaseLock().lock();
  try {
    AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
    conn.setAutoCommit(false);
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

    // sync connection properties
    conn.createStatement();
    Connection remoteConn = getConnection(
            AvaticaServersForTest.PropertyRemoteJdbcMetaFactory.getInstance(PROPERTIES), conn.id);

    assertFalse(remoteConn.getAutoCommit());
    assertEquals(remoteConn.getTransactionIsolation(),
            Connection.TRANSACTION_REPEATABLE_READ);

    // after 1s, remote connection expired and reopen
    Thread.sleep(1000);

    conn.createStatement();
    Connection remoteConn1 = getConnection(
            AvaticaServersForTest.PropertyRemoteJdbcMetaFactory.getInstance(PROPERTIES), conn.id);

    assertFalse(remoteConn1.getAutoCommit());
    assertEquals(remoteConn1.getTransactionIsolation(),
            Connection.TRANSACTION_REPEATABLE_READ);
  } finally {
    ConnectionSpec.getDatabaseLock().unlock();
  }
}