use of java.sql.SQLException in project hive by apache.
the class TestBeeLineWithArgs method createTable.
/**
* Create table for use by tests
* @throws ClassNotFoundException
* @throws SQLException
*/
private static void createTable() throws ClassNotFoundException, SQLException {
Class.forName(BeeLine.BEELINE_DEFAULT_JDBC_DRIVER);
Connection con = DriverManager.getConnection(miniHS2.getBaseJdbcURL(), userName, "");
assertNotNull("Connection is null", con);
assertFalse("Connection should not be closed", con.isClosed());
Statement stmt = con.createStatement();
assertNotNull("Statement is null", stmt);
stmt.execute("set hive.support.concurrency = false");
HiveConf conf = new HiveConf();
String dataFileDir = conf.get("test.data.files").replace('\\', '/').replace("c:", "");
Path dataFilePath = new Path(dataFileDir, "kv1.txt");
// drop table. ignore error.
try {
stmt.execute("drop table " + tableName);
} catch (Exception ex) {
fail(ex.toString() + " " + ExceptionUtils.getStackTrace(ex));
}
// create table
stmt.execute("create table " + tableName + " (under_col int comment 'the under column', value string) comment '" + tableComment + "'");
// load data
stmt.execute("load data local inpath '" + dataFilePath.toString() + "' into table " + tableName);
}
use of java.sql.SQLException in project hive by apache.
the class TestJdbcDriver2 method testOutOfBoundCols.
/**
* Test bad args to getXXX()
* @throws SQLException
*/
@Test
public void testOutOfBoundCols() throws SQLException {
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("select * from " + tableName);
// row 1
assertTrue(res.next());
try {
res.getInt(200);
} catch (SQLException e) {
}
try {
res.getInt("zzzz");
} catch (SQLException e) {
}
stmt.close();
}
use of java.sql.SQLException in project tomcat by apache.
the class JDBCRealm method getPassword.
/**
* Get the password for the specified user.
* @param username The user name
* @return the password associated with the given principal's user name.
*/
@Override
protected synchronized String getPassword(String username) {
// Look up the user's credentials
String dbCredentials = null;
// Number of tries is the number of attempts to connect to the database
// during this login attempt (if we need to open the database)
// This needs rewritten with better pooling support, the existing code
// needs signature changes since the Prepared statements needs cached
// with the connections.
// The code below will try twice if there is a SQLException so the
// connection may try to be opened again. On normal conditions (including
// invalid login - the above is only used once.
int numberOfTries = 2;
while (numberOfTries > 0) {
try {
// Ensure that we have an open database connection
open();
PreparedStatement stmt = credentials(dbConnection, username);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
dbCredentials = rs.getString(1);
}
dbConnection.commit();
if (dbCredentials != null) {
dbCredentials = dbCredentials.trim();
}
return dbCredentials;
}
} catch (SQLException e) {
// Log the problem for posterity
containerLog.error(sm.getString("jdbcRealm.exception"), e);
}
// Close the connection so that it gets reopened next time
if (dbConnection != null) {
close(dbConnection);
}
numberOfTries--;
}
return null;
}
use of java.sql.SQLException in project tomcat by apache.
the class JDBCRealm method open.
/**
* Open (if necessary) and return a database connection for use by
* this Realm.
* @return the opened connection
* @exception SQLException if a database error occurs
*/
protected Connection open() throws SQLException {
// Do nothing if there is a database connection already open
if (dbConnection != null)
return (dbConnection);
// Instantiate our database driver if necessary
if (driver == null) {
try {
Class<?> clazz = Class.forName(driverName);
driver = (Driver) clazz.newInstance();
} catch (Throwable e) {
ExceptionUtils.handleThrowable(e);
throw new SQLException(e.getMessage(), e);
}
}
// Open a new connection
Properties props = new Properties();
if (connectionName != null)
props.put("user", connectionName);
if (connectionPassword != null)
props.put("password", connectionPassword);
dbConnection = driver.connect(connectionURL, props);
if (dbConnection == null) {
throw new SQLException(sm.getString("jdbcRealm.open.invalidurl", driverName, connectionURL));
}
dbConnection.setAutoCommit(false);
return (dbConnection);
}
use of java.sql.SQLException in project tomcat by apache.
the class BasicDataSource method createPoolableConnectionFactory.
/**
* Creates the PoolableConnectionFactory and attaches it to the connection pool. This method only exists
* so subclasses can replace the default implementation.
*
* @param driverConnectionFactory JDBC connection factory
* @return the connection factory
* @throws SQLException if an error occurs creating the PoolableConnectionFactory
*/
protected PoolableConnectionFactory createPoolableConnectionFactory(final ConnectionFactory driverConnectionFactory) throws SQLException {
PoolableConnectionFactory connectionFactory = null;
try {
connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, registeredJmxName);
connectionFactory.setValidationQuery(validationQuery);
connectionFactory.setValidationQueryTimeout(validationQueryTimeout);
connectionFactory.setConnectionInitSql(connectionInitSqls);
connectionFactory.setDefaultReadOnly(defaultReadOnly);
connectionFactory.setDefaultAutoCommit(defaultAutoCommit);
connectionFactory.setDefaultTransactionIsolation(defaultTransactionIsolation);
connectionFactory.setDefaultCatalog(defaultCatalog);
connectionFactory.setCacheState(cacheState);
connectionFactory.setPoolStatements(poolPreparedStatements);
connectionFactory.setMaxOpenPrepatedStatements(maxOpenPreparedStatements);
connectionFactory.setMaxConnLifetimeMillis(maxConnLifetimeMillis);
connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
connectionFactory.setEnableAutoCommitOnReturn(getEnableAutoCommitOnReturn());
connectionFactory.setDefaultQueryTimeout(getDefaultQueryTimeout());
connectionFactory.setFastFailValidation(fastFailValidation);
connectionFactory.setDisconnectionSqlCodes(disconnectionSqlCodes);
validateConnectionFactory(connectionFactory);
} catch (final RuntimeException e) {
throw e;
} catch (final Exception e) {
throw new SQLException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
}
return connectionFactory;
}
Aggregations