Package java.sql

Examples of java.sql.DatabaseMetaData


        int state;
        Database.setInitialPowerOffCount(0);
        Connection conn = getConnection(url);
        assertEquals(0, ((JdbcConnection) conn).getPowerOffCount());
        Statement stat = conn.createStatement();
        DatabaseMetaData meta = conn.getMetaData();
        ResultSet rs = meta.getTables(null, null, "TEST", null);
        if (!rs.next()) {
            state = 0;
        } else {
            // table does not exist
            rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
View Full Code Here


            setting = FullTextSettings.getInstance(conn);
            if (!setting.isInitialized()) {
                FullText.init(conn);
            }
            ArrayList<String> keyList = New.arrayList();
            DatabaseMetaData meta = conn.getMetaData();
            ResultSet rs = meta.getColumns(null,
                    JdbcUtils.escapeMetaDataPattern(schemaName),
                    JdbcUtils.escapeMetaDataPattern(tableName),
                    null);
            ArrayList<String> columnList = New.arrayList();
            while (rs.next()) {
                columnList.add(rs.getString("COLUMN_NAME"));
            }
            columnTypes = new int[columnList.size()];
            index = new IndexInfo();
            index.schema = schemaName;
            index.table = tableName;
            index.columns = new String[columnList.size()];
            columnList.toArray(index.columns);
            rs = meta.getColumns(null,
                    JdbcUtils.escapeMetaDataPattern(schemaName),
                    JdbcUtils.escapeMetaDataPattern(tableName),
                    null);
            for (int i = 0; rs.next(); i++) {
                columnTypes[i] = rs.getInt("DATA_TYPE");
            }
            if (keyList.size() == 0) {
                rs = meta.getPrimaryKeys(null,
                        JdbcUtils.escapeMetaDataPattern(schemaName),
                        tableName);
                while (rs.next()) {
                    keyList.add(rs.getString("COLUMN_NAME"));
                }
View Full Code Here

            this.schema = schemaName;
            this.table = tableName;
            this.indexPath = getIndexPath(conn);
            this.indexAccess = getIndexAccess(conn);
            ArrayList<String> keyList = New.arrayList();
            DatabaseMetaData meta = conn.getMetaData();
            ResultSet rs = meta.getColumns(null,
                    JdbcUtils.escapeMetaDataPattern(schemaName),
                    JdbcUtils.escapeMetaDataPattern(tableName),
                    null);
            ArrayList<String> columnList = New.arrayList();
            while (rs.next()) {
                columnList.add(rs.getString("COLUMN_NAME"));
            }
            columnTypes = new int[columnList.size()];
            columns = new String[columnList.size()];
            columnList.toArray(columns);
            rs = meta.getColumns(null,
                    JdbcUtils.escapeMetaDataPattern(schemaName),
                    JdbcUtils.escapeMetaDataPattern(tableName),
                    null);
            for (int i = 0; rs.next(); i++) {
                columnTypes[i] = rs.getInt("DATA_TYPE");
            }
            if (keyList.size() == 0) {
                rs = meta.getPrimaryKeys(null,
                        JdbcUtils.escapeMetaDataPattern(schemaName),
                        tableName);
                while (rs.next()) {
                    keyList.add(rs.getString("COLUMN_NAME"));
                }
View Full Code Here

            throws SQLException {
        if (schema == null) {
            return treeIndex;
        }
        Connection conn = session.getConnection();
        DatabaseMetaData meta = session.getMetaData();
        int level = mainSchema ? 0 : 1;
        boolean showColumns = mainSchema || !schema.isSystem;
        String indentation = ", " + level + ", " + (showColumns ? "1" : "2") + ", ";
        String indentNode = ", " + (level + 1) + ", 2, ";
        DbTableOrView[] tables = schema.tables;
View Full Code Here

        boolean isH2 = false;
        try {
            contents.readContents(session.getMetaData());
            session.loadBnf();
            Connection conn = session.getConnection();
            DatabaseMetaData meta = session.getMetaData();
            isH2 = contents.isH2;

            StringBuilder buff = new StringBuilder();
            buff.append("setNode(0, 0, 0, 'database', '" + PageParser.escapeJavaScript((String) session.get("url"))
                    + "', null);\n");
            int treeIndex = 1;

            DbSchema defaultSchema = contents.defaultSchema;
            treeIndex = addTablesAndViews(defaultSchema, true, buff, treeIndex);
            DbSchema[] schemas = contents.schemas;
            for (DbSchema schema : schemas) {
                if (schema == defaultSchema || schema == null) {
                    continue;
                }
                buff.append("setNode(" + treeIndex + ", 0, 1, 'folder', '" + PageParser.escapeJavaScript(schema.name)
                        + "', null);\n");
                treeIndex++;
                treeIndex = addTablesAndViews(schema, false, buff, treeIndex);
            }
            if (isH2) {
                Statement stat = null;
                try {
                    stat = conn.createStatement();
                    ResultSet rs = stat
                            .executeQuery("SELECT * FROM INFORMATION_SCHEMA.SEQUENCES ORDER BY SEQUENCE_NAME");
                    for (int i = 0; rs.next(); i++) {
                        if (i == 0) {
                            buff.append("setNode(" + treeIndex
                                    + ", 0, 1, 'sequences', '${text.tree.sequences}', null);\n");
                            treeIndex++;
                        }
                        String name = rs.getString("SEQUENCE_NAME");
                        String current = rs.getString("CURRENT_VALUE");
                        String increment = rs.getString("INCREMENT");
                        buff.append("setNode(" + treeIndex + ", 1, 1, 'sequence', '"
                                + PageParser.escapeJavaScript(name) + "', null);\n");
                        treeIndex++;
                        buff.append("setNode(" + treeIndex + ", 2, 2, 'type', '${text.tree.current}: "
                                + PageParser.escapeJavaScript(current) + "', null);\n");
                        treeIndex++;
                        if (!"1".equals(increment)) {
                            buff.append("setNode(" + treeIndex + ", 2, 2, 'type', '${text.tree.increment}: "
                                    + PageParser.escapeJavaScript(increment) + "', null);\n");
                            treeIndex++;
                        }
                    }
                    rs.close();
                    rs = stat.executeQuery("SELECT * FROM INFORMATION_SCHEMA.USERS ORDER BY NAME");
                    for (int i = 0; rs.next(); i++) {
                        if (i == 0) {
                            buff.append("setNode(" + treeIndex + ", 0, 1, 'users', '${text.tree.users}', null);\n");
                            treeIndex++;
                        }
                        String name = rs.getString("NAME");
                        String admin = rs.getString("ADMIN");
                        buff.append("setNode(" + treeIndex + ", 1, 1, 'user', '" + PageParser.escapeJavaScript(name)
                                + "', null);\n");
                        treeIndex++;
                        if (admin.equalsIgnoreCase("TRUE")) {
                            buff.append("setNode(" + treeIndex + ", 2, 2, 'type', '${text.tree.admin}', null);\n");
                            treeIndex++;
                        }
                    }
                    rs.close();
                } finally {
                    JdbcUtils.closeSilently(stat);
                }
            }
            String version = meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion();
            buff.append("setNode(" + treeIndex + ", 0, 0, 'info', '" + PageParser.escapeJavaScript(version)
                    + "', null);\n");
            buff.append("refreshQueryTables();");
            session.put("tree", buff.toString());
        } catch (Exception e) {
View Full Code Here

        session.put("result", result);
        return "result.jsp";
    }

    private ResultSet getMetaResultSet(Connection conn, String sql) throws SQLException {
        DatabaseMetaData meta = conn.getMetaData();
        if (isBuiltIn(sql, "@best_row_identifier")) {
            String[] p = split(sql);
            int scale = p[4] == null ? 0 : Integer.parseInt(p[4]);
            boolean nullable = p[5] == null ? false : Boolean.valueOf(p[5]).booleanValue();
            return meta.getBestRowIdentifier(p[1], p[2], p[3], scale, nullable);
        } else if (isBuiltIn(sql, "@catalogs")) {
            return meta.getCatalogs();
        } else if (isBuiltIn(sql, "@columns")) {
            String[] p = split(sql);
            return meta.getColumns(p[1], p[2], p[3], p[4]);
        } else if (isBuiltIn(sql, "@column_privileges")) {
            String[] p = split(sql);
            return meta.getColumnPrivileges(p[1], p[2], p[3], p[4]);
        } else if (isBuiltIn(sql, "@cross_references")) {
            String[] p = split(sql);
            return meta.getCrossReference(p[1], p[2], p[3], p[4], p[5], p[6]);
        } else if (isBuiltIn(sql, "@exported_keys")) {
            String[] p = split(sql);
            return meta.getExportedKeys(p[1], p[2], p[3]);
        } else if (isBuiltIn(sql, "@imported_keys")) {
            String[] p = split(sql);
            return meta.getImportedKeys(p[1], p[2], p[3]);
        } else if (isBuiltIn(sql, "@index_info")) {
            String[] p = split(sql);
            boolean unique = p[4] == null ? false : Boolean.valueOf(p[4]).booleanValue();
            boolean approx = p[5] == null ? false : Boolean.valueOf(p[5]).booleanValue();
            return meta.getIndexInfo(p[1], p[2], p[3], unique, approx);
        } else if (isBuiltIn(sql, "@primary_keys")) {
            String[] p = split(sql);
            return meta.getPrimaryKeys(p[1], p[2], p[3]);
        } else if (isBuiltIn(sql, "@procedures")) {
            String[] p = split(sql);
            return meta.getProcedures(p[1], p[2], p[3]);
        } else if (isBuiltIn(sql, "@procedure_columns")) {
            String[] p = split(sql);
            return meta.getProcedureColumns(p[1], p[2], p[3], p[4]);
        } else if (isBuiltIn(sql, "@schemas")) {
            return meta.getSchemas();
        } else if (isBuiltIn(sql, "@tables")) {
            String[] p = split(sql);
            String[] types = p[4] == null ? null : StringUtils.arraySplit(p[4], ',', false);
            return meta.getTables(p[1], p[2], p[3], types);
        } else if (isBuiltIn(sql, "@table_privileges")) {
            String[] p = split(sql);
            return meta.getTablePrivileges(p[1], p[2], p[3]);
        } else if (isBuiltIn(sql, "@table_types")) {
            return meta.getTableTypes();
        } else if (isBuiltIn(sql, "@type_info")) {
            return meta.getTypeInfo();
        } else if (isBuiltIn(sql, "@udts")) {
            String[] p = split(sql);
            int[] types;
            if (p[4] == null) {
                types = null;
            } else {
                String[] t = StringUtils.arraySplit(p[4], ',', false);
                types = new int[t.length];
                for (int i = 0; i < t.length; i++) {
                    types[i] = Integer.parseInt(t[i]);
                }
            }
            return meta.getUDTs(p[1], p[2], p[3], types);
        } else if (isBuiltIn(sql, "@version_columns")) {
            String[] p = split(sql);
            return meta.getVersionColumns(p[1], p[2], p[3]);
        } else if (isBuiltIn(sql, "@memory")) {
            SimpleResultSet rs = new SimpleResultSet();
            rs.addColumn("Type", Types.VARCHAR, 0, 0);
            rs.addColumn("KB", Types.VARCHAR, 0, 0);
            rs.addRow("Used Memory", "" + Utils.getMemoryUsed());
            rs.addRow("Free Memory", "" + Utils.getMemoryFree());
            return rs;
        } else if (isBuiltIn(sql, "@info")) {
            SimpleResultSet rs = new SimpleResultSet();
            rs.addColumn("KEY", Types.VARCHAR, 0, 0);
            rs.addColumn("VALUE", Types.VARCHAR, 0, 0);
            rs.addRow("conn.getCatalog", conn.getCatalog());
            rs.addRow("conn.getAutoCommit", "" + conn.getAutoCommit());
            rs.addRow("conn.getTransactionIsolation", "" + conn.getTransactionIsolation());
            rs.addRow("conn.getWarnings", "" + conn.getWarnings());
            String map;
            try {
                map = "" + conn.getTypeMap();
            } catch (SQLException e) {
                map = e.toString();
            }
            rs.addRow("conn.getTypeMap", "" + map);
            rs.addRow("conn.isReadOnly", "" + conn.isReadOnly());
            rs.addRow("conn.getHoldability", "" + conn.getHoldability());
            addDatabaseMetaData(rs, meta);
            return rs;
        } else if (isBuiltIn(sql, "@attributes")) {
            String[] p = split(sql);
            return meta.getAttributes(p[1], p[2], p[3], p[4]);
        } else if (isBuiltIn(sql, "@super_tables")) {
            String[] p = split(sql);
            return meta.getSuperTables(p[1], p[2], p[3]);
        } else if (isBuiltIn(sql, "@super_types")) {
            String[] p = split(sql);
            return meta.getSuperTypes(p[1], p[2], p[3]);
        } else if (isBuiltIn(sql, "@prof_stop")) {
            if (profiler != null) {
                profiler.stopCollecting();
                SimpleResultSet rs = new SimpleResultSet();
                rs.addColumn("Top Stack Trace(s)", Types.VARCHAR, 0, 0);
View Full Code Here

  private Map<String, String> getDbInfo(String aDbName, ConnectionSource aConnSource) throws DAOException {
    Map<String, String> result = new HashMap<String, String>();
    Connection connection  = null;
    try {
      connection = aConnSource.getConnection(aDbName);
      DatabaseMetaData metaData = connection.getMetaData();
      addDatabaseURIs(result, metaData);
      addNamesAndVersions(result, metaData);
      addDriverNamesAndVersions(result, metaData);
    }
    catch (SQLException ex){
View Full Code Here

    * @return true if the entry has been found on the database, false otherwise
    * @throws Exception
    */
   public boolean fillMetadata(Connection conn, String catalog, String schema, String table, ResultSet queryRs, I_AttributeTransformer transformer) throws Exception {
      try {
         DatabaseMetaData meta = conn.getMetaData();
         ResultSet rs = null;
         boolean tableExists = false;
         try {
            rs = meta.getColumns(catalog, schema, table, null);
            if (this.description == null)
               this.description = new SqlDescription(this.info);
            this.description.setIdentity(table);
            this.description.setAttribute(ReplicationConstants.TABLE_NAME_ATTR, table);
            this.description.setAttribute(ReplicationConstants.CATALOG_ATTR, catalog);
            this.description.setAttribute(ReplicationConstants.SCHEMA_ATTR, schema);

            while (rs != null && rs.next()) {
               tableExists = true;
               String tmpCat = rs.getString(1);
               if (catalog == null)
                  catalog = tmpCat;
               String tmpSchema = rs.getString(2);
               if (schema == null)
                  schema = tmpSchema;
               String tmpTableName = rs.getString(3);
               String colName = rs.getString(4);
               SqlColumn colDescription = this.description.getColumn(colName);
               if (colDescription == null) {
                  colDescription = new SqlColumn(this.info);
                  this.description.addColumn(colDescription);
               }
               colDescription.setCatalog(tmpCat);
               colDescription.setSchema(tmpSchema);
               colDescription.setTable(tmpTableName);
               colDescription.setColName(colName);
               colDescription.setSqlType(rs.getInt(5));
               colDescription.setType(rs.getString(6));
               colDescription.setColSize(rs.getInt(7));
               colDescription.setPrecision(rs.getInt(9));
               colDescription.setRadix(rs.getInt(10));
               colDescription.setNullable(rs.getInt(11));
               colDescription.setRemarks(rs.getString(12));
               // TODO URL ENCODE THIS
               // colDescription.setColDefault(rs.getString(13));
               colDescription.setCharLength(rs.getInt(16));
               colDescription.setPos(rs.getInt(17));
            }
         }
         finally {
            if (rs != null)
               rs.close();
            rs = null;
         }
         if (!tableExists)
            return false;
           
         // retrieve additional information about columns
         Statement st = null;
         String completeTableName = null;
         String colName = null;
         SqlColumn colDesc = null;
         try {
            st = conn.createStatement();
            completeTableName = table;
            if (schema != null)
               completeTableName = schema + "." + table;
            rs = st.executeQuery("SELECT * from " + completeTableName);
            if (rs == null)
               return false;
            ResultSetMetaData rsMeta = rs.getMetaData();
            if (rsMeta == null)
               return false;
            int colCount = rsMeta.getColumnCount();
            if (colCount != this.description.getNumOfColumns()) {
               if (this.description.getNumOfColumns() != 0)
                  throw new Exception("SqlInfo.fillMetaData: wrong number of colums in the SELECT Statement. is '" + colCount + "' but should be '" + this.description.getNumOfColumns() + "'");
               // why does this happen ? Is it on old oracle ?
               for (int i=0; i < colCount; i++) {
                  SqlColumn colDescription = new SqlColumn(this.info);
                  colDescription.setPos(i+1);
                  this.description.addColumn(colDescription);
               }
            }
            for (int i=1; i <= colCount; i++) {
               colName = rsMeta.getColumnName(i);
               colDesc = this.description.getColumnAtPosition(i);
               if (colDesc.getColName() == null)
                  colDesc.setColName(colName);
               colDesc.setLabel(rsMeta.getColumnLabel(i));
               colDesc.setAutoInc(rsMeta.isAutoIncrement(i));
               colDesc.setCaseSens(rsMeta.isCaseSensitive(i));
               colDesc.setTypeName(rsMeta.getColumnTypeName(i));
               try { // there seems to be a bug in the oracle jdbc driver internally using long but giving
                     // back integer
                  colDesc.setPrecision(rsMeta.getPrecision(i));
               }
               catch (NumberFormatException e) {
                  log.warning(e.getMessage());
               }
               colDesc.setScale(rsMeta.getScale(i));
               colDesc.setSigned(rsMeta.isSigned(i));
               colDesc.setReadOnly(rsMeta.isReadOnly(i));
               colDesc.setNullable(rsMeta.isNullable(i));
               colDesc.setSearchable(rsMeta.isSearchable(i));
               // rsMeta.isWritable(i);
               // rsMeta.isDefinitelyWritable(i);
               colDesc = null; // to get correct info on exceptions
               colName = null;
            }
         }
         catch (SQLException ex) {
            // could still work but UROWID throws an exception here on ora8.1.6
            // "ORA-03115: unsupported network datatype or representation"
            // if (ex.getSQLState() != null && ex.getSQLState().indexOf("ORA-03115") > -1) { // does not work at least on ora8.1.6
            if ( (ex.getMessage() != null && ex.getMessage().indexOf("ORA-03115") > -1) ||
                  (ex.getSQLState() != null && ex.getSQLState().indexOf("ORA-03115") > -1)) {
               if (completeTableName == null)
                  completeTableName = "";
               if (colName == null)
                  colName = "";
               String colDescTxt = "";
               if (colDesc != null)
                  colDescTxt = colDesc.toXml("");
               log.warning("fillMetaData: could not build complete column information for " + completeTableName + "." + colName + ": " + colDescTxt + " " + ex.getMessage());
            }
            else
               throw ex;
         }
         finally {
            try {
               if (st != null)
                  st.close();
               st = null;
            }
            catch (SQLException ex) {
               ex.printStackTrace();
            }
            if (rs != null)
               rs.close();
            rs = null;
         }
        
         // add PK and FK stuff here ...
         rs = meta.getPrimaryKeys(catalog, schema, table);
         try {
            while (rs.next()) {
               colName = rs.getString(4);
               String pkName = rs.getString(6);
               SqlColumn col = this.description.getColumn(colName);
               if (col != null) {
                  col.setPrimaryKey(true);
                  col.setPkName(pkName);
               }
            }
         }
         finally {
            if (rs != null)
               rs.close();
            rs = null;
         }

         if (this.queryImportedKeys) {
            rs = meta.getImportedKeys(catalog, schema, table);
            try {
               while (rs.next()) {
                  colName = rs.getString(8);
                  SqlColumn col = description.getColumn(colName);
                  if (col != null) {
View Full Code Here

     
      Statement st = rs.getStatement();
      if (st != null) {
         Connection conn = st.getConnection();
         if (conn != null) {
            DatabaseMetaData dbMeta = conn.getMetaData();
            ResultSet pkRs = dbMeta.getPrimaryKeys(catalog, schema, tableName);
            while (pkRs.next()) {
               String colName = pkRs.getString(4);
               //String pkName = pkRs.getString(6);
               SqlColumn col = description.getColumn(colName);
               if (col != null)
                  col.setPrimaryKey(true);
            }
            ResultSet fkRs = dbMeta.getImportedKeys(catalog, schema, tableName);
            while (fkRs.next()) {
               String colName = fkRs.getString(8);
               SqlColumn col = description.getColumn(colName);
               if (col != null) {
                  col.setFkCatalog(fkRs.getString(1));
View Full Code Here

      // get the necessary metadata
      Connection conn = null;
      boolean success = true;
      try {
         conn = this.pool.getConnection();
         DatabaseMetaData dbmd = conn.getMetaData();
         this.maxStatementLength = dbmd.getMaxStatementLength();
         if (this.maxStatementLength < 1) {
            this.maxStatementLength = glob.getProperty().get("queue.persistent.maxStatementLength", 2048);
            if (first) {
               log.info("The maximum SQL statement length is not defined in JDBC meta data, we set it to " + this.maxStatementLength);
               first = false;
            }
         }

         if (!dbmd.supportsTransactions()) {
            String dbName = dbmd.getDatabaseProductName();
            log.severe("the database '" + dbName + "' does not support transactions, unpredicted results may happen");
         }

         if (!dbmd.supportsBatchUpdates()) {
            String dbName = dbmd.getDatabaseProductName();
            this.supportsBatch = false;
            log.fine("the database '" + dbName + "' does not support batch mode. No problem I will work whitout it");
         }
        
         // zero means not limit (to be sure we also check negative Values
         boolean logWarn = false;
         int defaultMaxNumStatements = dbmd.getMaxStatements();
         if (defaultMaxNumStatements < 1) {
            defaultMaxNumStatements = 50;
            logWarn = true;
         }
         // -queue.persistent.maxNumStatements 50
         this.maxNumStatements = this.pool.getProp("maxNumStatements", defaultMaxNumStatements);
         log.info("The maximum Number of statements for this database instance are '" + this.maxNumStatements + "'");
         if (logWarn && this.pool.getProp("maxNumStatements",-1)==-1)
            log.warning("The maxStatements returned fromt the database metadata is '" + defaultMaxNumStatements + "', will set the default to 50 unless you explicitly set '-maxNumStatements <num>'");

         // -queue.persistent.maxSelectLimit -1 (off)
         this.maxSelectLimit = this.pool.getProp("maxSelectLimit", -1);
         if (this.maxSelectLimit > 0)
            log.info("The maximum results returned by a select is set to '" + this.maxSelectLimit + "' (MSSQLerver only)");
        
         log.info("Using DB " + dbmd.getDatabaseProductName() + " " + dbmd.getDatabaseProductVersion() + " " + dbmd.getDriverName());
      }
      catch (XmlBlasterException ex) {
         success = false;
         throw ex;
      }
View Full Code Here

TOP

Related Classes of java.sql.DatabaseMetaData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.