// assumes... // import java.sql.*; Connection conn=DriverManager.getConnection( "jdbc:ucanaccess://C:/__tmp/test/zzz.accdb"); Statement s = conn.createStatement(); ResultSet rs = s.executeQuery("SELECT [LastName] FROM [Clients]"); while (rs.next()) { System.out.println(rs.getString(1));
Statement stmt = conn.createStatement(); try { ResultSet rs = stmt.executeQuery( "SELECT FULL_NAME FROM EMP" ); try { while ( rs.next() ) { System.out.println( "Name: " + rs.getString("FULL_NAME") ); } finally { try { rs.close(); } catch (Exception ignore) { } } finally { try { stmt.close(); } catch (Exception ignore) { }
Class.forName("com.mysql.jdbc.Driver") ; Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBNAME", "usrname", "pswd") ; Statement stmt = conn.createStatement() ; String query = "select columnname from tablename ;" ; ResultSet rs = stmt.executeQuery(query) ;
@Test public void testSelectCount() throws Exception final ResultSet resultSet = client.createStatement().executeQuery("SELECT COUNT(*) AS cnt FROM druid.foo"); final List<Map<String, Object>> rows = getRows(resultSet); Assert.assertEquals( ImmutableList.of( ImmutableMap.of("cnt", 6L)
/**
* @throws Exception If failed.
@Test
public void testFetchSize() throws Exception {
stmt.setFetchSize(1);
ResultSet rs = stmt.executeQuery("select * from TestObject where id > 0");
assertTrue(rs.next());
assertTrue(rs.next());
assertTrue(rs.next());
stmt.setFetchSize(0);
@Test(timeOut = 10000) public void testQueryPartialCancel() throws Exception try (Connection connection = createConnection("blackhole", "default"); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT count(*) FROM slow_test_table")) { statement.unwrap(PrestoStatement.class).partialCancel(); assertTrue(resultSet.next()); assertEquals(resultSet.getLong(1), 0);
protected QueryResult executeLdapQuery(String query, String name, String password)
throws SQLException
try (Connection connection = getLdapConnection(name, password)) {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(query);
return QueryResult.forResultSet(rs);
private void checkRepresentation(String expression, int expectedSqlType, ResultAssertion assertion)
throws Exception
try (ResultSet rs = statement.executeQuery("SELECT " + expression)) {
ResultSetMetaData metadata = rs.getMetaData();
assertEquals(metadata.getColumnCount(), 1);
assertEquals(metadata.getColumnType(1), expectedSqlType);
assertTrue(rs.next());
assertion.accept(rs, 1);
assertFalse(rs.next());
private static String getSessionProperty(Connection connection, String key, String valueType)
throws SQLException
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery("SHOW SESSION");
while (rs.next()) {
if (rs.getString("Name").equals(key)) {
return rs.getString(valueType);
return null;
@Test public void testSelectBoolean() throws Exception final ResultSet resultSet = client.createStatement().executeQuery( "SELECT dim2, dim2 IS NULL AS isnull FROM druid.foo LIMIT 1" Assert.assertEquals( ImmutableList.of( ImmutableMap.of("dim2", "a", "isnull", false) getRows(resultSet)
@Test(groups = {LDAP, PRESTO_JDBC, PROFILE_SPECIFIC_TESTS}, timeOut = TIMEOUT) public void shouldFailForIncorrectTrustStore() try { String url = format("jdbc:presto://%s?SSL=true&SSLTrustStorePath=%s&SSLTrustStorePassword=%s", prestoServer(), ldapTruststorePath, "wrong_password"); Connection connection = DriverManager.getConnection(url, ldapUserName, ldapUserPassword); Statement statement = connection.createStatement(); statement.executeQuery(NATION_SELECT_ALL_QUERY); fail(); catch (SQLException exception) { assertEquals(exception.getMessage(), "Error setting up SSL: Keystore was tampered with, or password was incorrect");
@Test(timeOut = 10_000) public void testPartialCancel() throws Exception try (Connection connection = createConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT count(*) FROM blackhole.test.slow")) { statement.unwrap(PrestoStatement.class).partialCancel(); assertTrue(resultSet.next()); assertEquals(resultSet.getLong(1), 0);
@Test(expectedExceptions = SQLException.class, expectedExceptionsMessageRegExp = ".* does not exist") public void testBadQuery() throws Exception try (Connection connection = createConnection("test", "tiny")) { try (Statement statement = connection.createStatement()) { try (ResultSet ignored = statement.executeQuery("SELECT * FROM bad_table")) { fail("expected exception");
/**
* Get the name of the current schema.
* This method is called by the database.
* @param conn the connection
* @return the schema name
public static String getCurrentSchema(Connection conn) throws SQLException {
ResultSet rs = conn.createStatement().executeQuery("call schema()");
rs.next();
return rs.getString(1);
@Test public void testFieldAliasingSelect() throws Exception final ResultSet resultSet = client.createStatement().executeQuery( "SELECT dim2 AS \"x\", dim2 AS \"y\" FROM druid.foo LIMIT 1" Assert.assertEquals( ImmutableList.of( ImmutableMap.of("x", "a", "y", "a") getRows(resultSet)
@Test(groups = {LDAP, SIMBA_JDBC, PROFILE_SPECIFIC_TESTS}, timeOut = TIMEOUT) public void shouldFailForIncorrectTrustStore() try { String url = String.format(JDBC_URL_FORMAT, prestoServer(), ldapTruststorePath, "wrong_password"); Connection connection = DriverManager.getConnection(url, ldapUserName, ldapUserPassword); Statement statement = connection.createStatement(); statement.executeQuery(NATION_SELECT_ALL_QUERY); fail(); catch (SQLException exception) { assertEquals(exception.getMessage(), SSL_CERTIFICATE_ERROR);
@Before public void setUp() throws Exception { this.connection = mock(Connection.class); this.dataSource = mock(DataSource.class); this.statement = mock(Statement.class); this.preparedStatement = mock(PreparedStatement.class); this.resultSet = mock(ResultSet.class); this.resultSetMetaData = mock(ResultSetMetaData.class); this.template = new JdbcTemplate(this.dataSource); given(this.dataSource.getConnection()).willReturn(this.connection); given(this.resultSet.getMetaData()).willReturn(this.resultSetMetaData); given(this.resultSetMetaData.getColumnCount()).willReturn(1); given(this.resultSetMetaData.getColumnLabel(1)).willReturn("age"); given(this.connection.createStatement()).willReturn(this.statement); given(this.connection.prepareStatement(anyString())).willReturn(this.preparedStatement); given(this.preparedStatement.executeQuery()).willReturn(this.resultSet); given(this.statement.executeQuery(anyString())).willReturn(this.resultSet);
/**
* Check whether the database is in multi-threaded mode.
* @param conn the connection
* @return true if the multi-threaded mode is used
static boolean isMultiThread(Connection conn)
throws SQLException {
try (Statement stat = conn.createStatement()) {
ResultSet rs = stat.executeQuery(
"SELECT value FROM information_schema.settings" +
" WHERE name = 'MULTI_THREADED'");
return rs.next() && !"0".equals(rs.getString(1));
@Test public void testTimestampsInResponse() throws Exception final ResultSet resultSet = client.createStatement().executeQuery( "SELECT __time, CAST(__time AS DATE) AS t2 FROM druid.foo LIMIT 1" Assert.assertEquals( ImmutableList.of( ImmutableMap.of( "__time", new Timestamp(DateTimes.of("2000-01-01T00:00:00.000Z").getMillis()), "t2", new Date(DateTimes.of("2000-01-01").getMillis()) getRows(resultSet)
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/database", "username", "password"); Statement stmt = conn.createStatement(); Scanner s = new Scanner(System.in); stmt.executeQuery("SELECT * FROM table WHERE name = '" + s.next() + "';"); conn.close();