List of usage examples for java.sql PreparedStatement getMetaData
ResultSetMetaData getMetaData() throws SQLException;
ResultSetMetaData
object that contains information about the columns of the ResultSet
object that will be returned when this PreparedStatement
object is executed. From source file:org.pentaho.di.core.database.Database.java
public RowMetaInterface getQueryFields(String sql, boolean param, RowMetaInterface inform, Object[] data) throws KettleDatabaseException { RowMetaInterface fields;// w w w .j a v a 2 s .c o m DBCache dbcache = DBCache.getInstance(); DBCacheEntry entry = null; // Check the cache first! // if (dbcache != null) { entry = new DBCacheEntry(databaseMeta.getName(), sql); fields = dbcache.get(entry); if (fields != null) { return fields; } } if (connection == null) { return null; // Cache test without connect. } // No cache entry found // The new method of retrieving the query fields fails on Oracle because // they failed to implement the getMetaData method on a prepared statement. // (!!!) // Even recent drivers like 10.2 fail because of it. // // There might be other databases that don't support it (we have no // knowledge of this at the time of writing). // If we discover other RDBMSs, we will create an interface for it. // For now, we just try to get the field layout on the re-bound in the // exception block below. // if (databaseMeta.supportsPreparedStatementMetadataRetrieval()) { // On with the regular program. // PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(databaseMeta.stripCR(sql), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); preparedStatement.setMaxRows(1); ResultSetMetaData rsmd = preparedStatement.getMetaData(); fields = getRowInfo(rsmd, false, false); } catch (Exception e) { fields = getQueryFieldsFallback(sql, param, inform, data); } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { throw new KettleDatabaseException( "Unable to close prepared statement after determining SQL layout", e); } } } } else { /* * databaseMeta.getDatabaseType()==DatabaseMeta.TYPE_DATABASE_SYBASEIQ ) { */ fields = getQueryFieldsFallback(sql, param, inform, data); } // Store in cache!! if (dbcache != null && entry != null) { if (fields != null) { dbcache.put(entry, fields); } } return fields; }
From source file:org.pentaho.di.core.database.Database.java
private RowMetaInterface getQueryFieldsFallback(String sql, boolean param, RowMetaInterface inform, Object[] data) throws KettleDatabaseException { RowMetaInterface fields;/*from w ww. java 2 s. com*/ try { if ((inform == null // Hack for MSSQL jtds 1.2 when using xxx NOT IN yyy we have to use a // prepared statement (see BugID 3214) && (databaseMeta.getDatabaseInterface() instanceof MSSQLServerDatabaseMeta || databaseMeta.getDatabaseInterface() instanceof KettleDatabaseMeta)) || databaseMeta.getDatabaseInterface().supportsResultSetMetadataRetrievalOnly()) { sel_stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); if (databaseMeta.isFetchSizeSupported() && sel_stmt.getMaxRows() >= 1) { if (databaseMeta.getDatabaseInterface() instanceof MySQLDatabaseMeta) { sel_stmt.setFetchSize(Integer.MIN_VALUE); } else { sel_stmt.setFetchSize(1); } } if (databaseMeta.supportsSetMaxRows()) { sel_stmt.setMaxRows(1); } ResultSet r = sel_stmt.executeQuery(databaseMeta.stripCR(sql)); fields = getRowInfo(r.getMetaData(), false, false); r.close(); sel_stmt.close(); sel_stmt = null; } else { PreparedStatement ps = connection.prepareStatement(databaseMeta.stripCR(sql)); if (param) { RowMetaInterface par = inform; if (par == null || par.isEmpty()) { par = getParameterMetaData(ps); } if (par == null || par.isEmpty()) { par = getParameterMetaData(sql, inform, data); } setValues(par, data, ps); } ResultSet r = ps.executeQuery(); fields = getRowInfo(ps.getMetaData(), false, false); r.close(); ps.close(); } } catch (Exception ex) { throw new KettleDatabaseException("Couldn't get field info from [" + sql + "]" + Const.CR, ex); } return fields; }