Search in sources :

Example 91 with SQLException

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);
}
Also used : Path(org.apache.hadoop.fs.Path) Statement(java.sql.Statement) Connection(java.sql.Connection) HiveConf(org.apache.hadoop.hive.conf.HiveConf) SQLException(java.sql.SQLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 92 with SQLException

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();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 93 with SQLException

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;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 94 with SQLException

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);
}
Also used : SQLException(java.sql.SQLException) Properties(java.util.Properties)

Example 95 with SQLException

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;
}
Also used : SQLException(java.sql.SQLException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException) PrivilegedActionException(java.security.PrivilegedActionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MalformedObjectNameException(javax.management.MalformedObjectNameException) JMException(javax.management.JMException)

Aggregations

SQLException (java.sql.SQLException)6792 PreparedStatement (java.sql.PreparedStatement)3048 ResultSet (java.sql.ResultSet)2426 Connection (java.sql.Connection)1871 ArrayList (java.util.ArrayList)972 Test (org.junit.Test)873 Statement (java.sql.Statement)779 IOException (java.io.IOException)341 List (java.util.List)335 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)298 Properties (java.util.Properties)255 DatabaseException (net.jforum.exceptions.DatabaseException)249 HashMap (java.util.HashMap)232 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)184 Timestamp (java.sql.Timestamp)171 CallableStatement (java.sql.CallableStatement)165 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)160 DalHints (com.ctrip.platform.dal.dao.DalHints)159 Map (java.util.Map)125 Date (java.util.Date)123