Search in sources :

Example 36 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project jodd by oblac.

the class DbOomQuery method resolveColumnDbSqlType.

/**
	 * Resolves column db sql type and populates it in column descriptor if missing.
	 */
protected void resolveColumnDbSqlType(Connection connection, DbEntityColumnDescriptor dec) {
    if (dec.dbSqlType != SqlType.DB_SQLTYPE_UNKNOWN) {
        return;
    }
    ResultSet rs = null;
    DbEntityDescriptor ded = dec.getDbEntityDescriptor();
    try {
        DatabaseMetaData dmd = connection.getMetaData();
        rs = dmd.getColumns(null, ded.getSchemaName(), ded.getTableName(), dec.getColumnName());
        if (rs.next()) {
            dec.dbSqlType = rs.getInt("DATA_TYPE");
        } else {
            dec.dbSqlType = SqlType.DB_SQLTYPE_NOT_AVAILABLE;
            if (log.isWarnEnabled()) {
                log.warn("Column SQL type not available: " + ded.toString() + '.' + dec.getColumnName());
            }
        }
    } catch (SQLException sex) {
        dec.dbSqlType = SqlType.DB_SQLTYPE_NOT_AVAILABLE;
        if (log.isWarnEnabled()) {
            log.warn("Column SQL type not resolved: " + ded.toString() + '.' + dec.getColumnName(), sex);
        }
    } finally {
        DbUtil.close(rs);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 37 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project L42 by ElvisResearchGroup.

the class DatabaseResource method tableExists.

/**
	 * Checks if the given tableName is in the database or not
	 * Thank you Stackoverflow!
	 * @param tableName name of the table to check for
	 * @return true if the table exists in the database or not
	 */
public boolean tableExists(String tableName) {
    if (!isConnected()) {
        throw new RuntimeException("Not connected");
    }
    try {
        DatabaseMetaData dbm = con.getMetaData();
        ResultSet tables = dbm.getTables(null, null, tableName, null);
        return tables.next();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 38 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project OpenAM by OpenRock.

the class JdbcSimpleUserDao method initialize.

/**
     * This class must be called before using the methods since the datasource
     * must be set up.
     * This version of initialize is used when connections are retreived using
     * a Java EE datasource resource which is configured through the application
     * server. For example if you use your server's ability to configure and
     * pool connections. This also requires a java:comp/env resource-ref
     * in web.xml
     *
     * @throws java.lang.InstantiationException when not able to instantiate
     *      this class, for example if parameters are bad or null or can not
     *      make a connection to datasource.
     */
public void initialize(String jndiName, String userDataBaseTableName, String membershipDataBaseTableName, Debug idRepoDebugLog) throws java.lang.InstantiationException {
    useJNDI = true;
    //validate input parameters
    if (jndiName == null || jndiName.trim().length() == 0 || userDataBaseTableName == null || userDataBaseTableName.trim().length() == 0 || idRepoDebugLog == null || membershipDataBaseTableName == null) {
        String msg = "JdbcSimpleUserDao.initialize" + " validation failed to make and make a new instance" + " with paramaters: jndiName=" + jndiName + " userDataBaseTableName=" + userDataBaseTableName + " membershipDataBaseTableName=" + membershipDataBaseTableName + " debug=" + idRepoDebugLog == null ? null : idRepoDebugLog.getName();
        if (idRepoDebugLog != null && idRepoDebugLog.messageEnabled()) {
            idRepoDebugLog.message(msg);
        }
        throw new java.lang.InstantiationException(msg);
    }
    //set class fields to input paramater
    debug = idRepoDebugLog;
    datasourceName = jndiName.trim();
    userTableName = userDataBaseTableName.trim();
    //input value for membership table can be empty, but null is not allowed
    if (membershipDataBaseTableName != null) {
        membershipTableName = membershipDataBaseTableName.trim();
    }
    //set the datasource class field
    try {
        Context ctx = new InitialContext();
        //java:comp/env requires a resource-ref in web.xml
        datasource = (DataSource) ctx.lookup(datasourceName);
    } catch (Exception ex) {
        String msg = "JdbcSimpleUserDao.getInstance:" + " Not able to initialize the datasource through JNDI" + " for datasourceName=" + datasourceName;
        if (debug.errorEnabled()) {
            debug.error(msg + " exception =" + ex);
        }
        //reset to un-initialized state
        datasourceName = null;
        datasource = null;
        userTableName = null;
        debug = null;
        throw new java.lang.InstantiationException(msg + ex.getMessage());
    }
    Connection con = null;
    try {
        //test out and log database info to debug log
        con = getConnection();
        DatabaseMetaData dbmd = con.getMetaData();
        if (debug.messageEnabled()) {
            debug.message("JdbcSimpleUserDao.initialize: DB Meta Data:" + " name=" + (dbmd == null ? "Not Available" : dbmd.getUserName()) + " url=" + (dbmd == null ? "Not Available" : dbmd.getURL()));
        }
        databaseURL = (dbmd == null ? null : dbmd.getURL());
        isMySQL = isMySQL(databaseURL);
    } catch (Exception ex) {
        String msg = "JdbcSimpleUserDao.getInstance:" + " Not able to connect the datasource and get the meta" + " data such as DB url";
        if (debug.errorEnabled()) {
            debug.error(msg + " exception =" + ex);
        }
        //reset to un-initialized state
        datasourceName = null;
        datasource = null;
        userTableName = null;
        membershipTableName = null;
        throw new java.lang.InstantiationException(msg + ex.getMessage());
    } finally {
        closeConnection(con);
        //reset to un-initialized state
        if (datasourceName == null) {
            debug = null;
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) InitialContext(javax.naming.InitialContext) IdRepoUnsupportedOpException(com.sun.identity.idm.IdRepoUnsupportedOpException) SQLException(java.sql.SQLException)

Example 39 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project tdi-studio-se by Talend.

the class MetadataColumnComparator method updatePackage.

private void updatePackage(IMetadataConnection metadataConnectionTemp) {
    if (metadataConnectionTemp == null) {
        return;
    }
    Driver derbyDriver = null;
    java.sql.Connection sqlConn = null;
    ExtractMetaDataUtils extractMeta = ExtractMetaDataUtils.getInstance();
    // get dbType before get connection so that the dbtype won't be null.TDI-18366
    String dbType = metadataConnectionTemp.getDbType();
    DatabaseConnection dbConn = (DatabaseConnection) metadataConnectionTemp.getCurrentConnection();
    List list = MetadataConnectionUtils.getConnection(metadataConnectionTemp);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i) instanceof Driver) {
            String driverClass = metadataConnectionTemp.getDriverClass();
            if (MetadataConnectionUtils.isDerbyRelatedDb(driverClass, dbType)) {
                derbyDriver = (Driver) list.get(i);
            }
        }
        if (list.get(i) instanceof java.sql.Connection) {
            sqlConn = (java.sql.Connection) list.get(i);
        }
    }
    try {
        if (sqlConn != null) {
            DatabaseMetaData dm = null;
            // Added by Marvin Wang on Mar. 13, 2013 for loading hive jars dynamically, refer to TDI-25072.
            if (EDatabaseTypeName.HIVE.getXmlName().equalsIgnoreCase(dbType)) {
                dm = HiveConnectionManager.getInstance().extractDatabaseMetaData(metadataConnectionTemp);
            } else {
                dm = extractMeta.getDatabaseMetaData(sqlConn, dbType, false, metadataConnectionTemp.getDatabase());
            }
            MetadataFillFactory.getDBInstance().fillCatalogs(dbConn, dm, MetadataConnectionUtils.getPackageFilter(dbConn, dm, true));
            MetadataFillFactory.getDBInstance().fillSchemas(dbConn, dm, MetadataConnectionUtils.getPackageFilter(dbConn, dm, false));
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        if (sqlConn != null) {
            ConnectionUtils.closeConnection(sqlConn);
        }
        if (derbyDriver != null) {
            try {
                //$NON-NLS-1$
                derbyDriver.connect("jdbc:derby:;shutdown=true", null);
            } catch (SQLException e) {
            // exception of shutdown success. no need to catch.
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) QueriesConnection(org.talend.core.model.metadata.builder.connection.QueriesConnection) ManagerConnection(org.talend.metadata.managment.repository.ManagerConnection) Driver(java.sql.Driver) ExtractMetaDataUtils(org.talend.core.model.metadata.builder.database.ExtractMetaDataUtils) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) PersistenceException(org.talend.commons.exception.PersistenceException) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 40 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project tdi-studio-se by Talend.

the class MetadataColumnComparator method getDatabaseMetaData.

/**
     * method "getDatabaseMetaData" get databaseMetaData.
     * 
     * @param iMetadataConnection contains connection
     * @return dbMetaData DatabaseMetaData .
     * @throws SQLException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws ClassNotFoundException
     */
public DatabaseMetaData getDatabaseMetaData(IMetadataConnection iMetadataConnection) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    ExtractMetaDataUtils extractMeta = ExtractMetaDataUtils.getInstance();
    extractMeta.getConnection(iMetadataConnection.getDbType(), iMetadataConnection.getUrl(), iMetadataConnection.getUsername(), iMetadataConnection.getPassword(), iMetadataConnection.getDatabase(), iMetadataConnection.getSchema(), iMetadataConnection.getDriverClass(), iMetadataConnection.getDriverJarPath(), iMetadataConnection.getDbVersionString(), iMetadataConnection.getAdditionalParams());
    String dbType = iMetadataConnection.getDbType();
    DatabaseMetaData dbMetaData = null;
    // Added by Marvin Wang on Mar. 13, 2013 for loading hive jars dynamically, refer to TDI-25072.
    if (EDatabaseTypeName.HIVE.getXmlName().equalsIgnoreCase(dbType)) {
        dbMetaData = HiveConnectionManager.getInstance().extractDatabaseMetaData(iMetadataConnection);
    } else {
        dbMetaData = extractMeta.getDatabaseMetaData(extractMeta.getConn(), dbType, iMetadataConnection.isSqlMode(), iMetadataConnection.getDatabase());
    }
    return dbMetaData;
}
Also used : ExtractMetaDataUtils(org.talend.core.model.metadata.builder.database.ExtractMetaDataUtils) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)361 ResultSet (java.sql.ResultSet)216 Connection (java.sql.Connection)169 SQLException (java.sql.SQLException)155 Test (org.junit.Test)87 Statement (java.sql.Statement)49 ResultSetMetaData (java.sql.ResultSetMetaData)42 ArrayList (java.util.ArrayList)40 PreparedStatement (java.sql.PreparedStatement)38 IOException (java.io.IOException)26 Properties (java.util.Properties)25 HashSet (java.util.HashSet)16 PhoenixDatabaseMetaData (org.apache.phoenix.jdbc.PhoenixDatabaseMetaData)16 HashMap (java.util.HashMap)15 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)15 PrintWriter (java.io.PrintWriter)10 Savepoint (java.sql.Savepoint)10 List (java.util.List)10 Map (java.util.Map)9 DataSource (javax.sql.DataSource)9