Package java.sql

Examples of java.sql.ResultSetMetaData


  }


  protected int toColumnIndex(String name) throws SQLException
  {
    ResultSetMetaData meta = getMetaData();
    int n = meta.getColumnCount();
    for(int i=1; i<=n; i++) {
      if (meta.getColumnName(i).equals(name)) {
        return i;
      }
    }
    return 0;
  }
View Full Code Here


        }
       
        try {
            String sql = "select top 1 * from " + DcModules.get(moduleIdx).getTableName();
            ResultSet result = DatabaseManager.executeSQL(sql);
            ResultSetMetaData meta = result.getMetaData();
           
            if (getNewFieldType() == ComponentFactory._REFERENCESFIELD) {
                boolean exists = false;
                for (int i = 1; i < meta.getColumnCount() + 1; i++)
                    exists |= meta.getColumnName(i).equalsIgnoreCase(columnName);
               
                // column should no longer be there after a successful conversion..
                // else the conversion still needs to (re-) occur.
                needed = exists;
            } else if (getNewFieldType() == ComponentFactory._REFERENCEFIELD) {
                // Check if there are items stored in the targeted module and if it exists.

                sql = "select top 1 * from " + refMod.getTableName();
               
                try {
                    ResultSet rs = DatabaseManager.executeSQL(sql);
                    rs.close();
                   
                    int pos = -1;
                    for (int idx = 1; idx < meta.getColumnCount(); idx ++) {
                        if (meta.getColumnName(idx).equalsIgnoreCase(columnName))
                            pos = idx;
                    }
                   
                   
                    // check the column type.. if not BIGINT a conversion is still needed.
                    needed = pos > -1 && meta.getColumnType(pos) != Types.BIGINT;
                   
                    if (!needed) {
                        // Check if each of the values actually exists in the reference module!!!!!!
                        sql = "select distinct " + columnName + " from " + DcModules.get(getModuleIdx()).getTableName() + " where " + columnName + " is not null " +
                              "and " + columnName + " not in (select " + refMod.getField(DcProperty._A_NAME).getDatabaseFieldName() + " from " + refMod.getTableName() + ") " +
                              "and " + columnName + " not in (select ID from " + refMod.getTableName() + ")";
                       
                        rs = DatabaseManager.executeSQL(sql);
                       
                        while (rs.next()) {
                            needed = true;
                            break;
                        }
                       
                        rs.close();
                    }
                   
                } catch (Exception ignore) {
                    needed = true;
                }
            } else {
                sql = "select top 1 " + columnName + " from " + DcModules.get(moduleIdx);
               
                try {
                    ResultSet rs = DatabaseManager.executeSQL(sql);
                    rs.close();
                   
                    int pos = -1;
                    for (int idx = 1; idx < meta.getColumnCount(); idx ++) {
                        if (meta.getColumnName(idx).equalsIgnoreCase(columnName))
                            pos = idx;
                    }
                   
                    needed = !isCorrectColumnType(DcModules.get(moduleIdx).getField(columnName).getDataBaseFieldType(), meta.getColumnType(pos));
                } catch (Exception ignore) {
                    needed = true;
                }
            }
           
View Full Code Here

        } catch (Exception exp) {
            return objects;
        }

        try {
          ResultSetMetaData md = rs.getMetaData();
         
          int fieldStart = 1;
            int[] fields = null;
            DcObject dco;
            DcModule module = null;
            while (rs.next()) {
              try {
                int moduleIdx = rs.getInt("MODULEIDX");
              module = DcModules.get(moduleIdx);
              fieldStart = 2;
              } catch (Exception e) {
                module = DcModules.get(md.getTableName(1));
              }
             
              if (fields == null) {
                  fields = new int[md.getColumnCount() - (fieldStart - 1)];
                  int fieldIdx = 0;
                  for (int i = fieldStart; i < fields.length + fieldStart; i++) {
                    String column = md.getColumnName(i);
                      fields[fieldIdx++] = module.getField(column).getIndex();
                  }
              }
             
              dco = module.getItem();
View Full Code Here

    int lenPK;
    int iCurr;
    int cCols;
    Statement oStmt = null;
    ResultSet oRSet = null;
    ResultSetMetaData oMDat = null;

    if (DebugFile.trace) {
      DebugFile.writeln("Begin DataTblDef.readMetaData([Connection], \"" + sTable + "\",\"" + sPK + "\")");
      DebugFile.incIdent();
    }

    BaseTable = sTable;

    // **********************************
    // * Leer los valores de metadatos

    // Lanza una SELECT que no devuelve ningún registro y luego
    // hace una llamada a getMetaData para guardar en memoria
    // la definición de los campos leidos.
    // La clave primaria no se extrae de la SELECT sino que debe
    // pasarse externamente como parametro.

    try {
      oStmt = oConn.createStatement();

      if (DebugFile.trace) DebugFile.writeln ("Statement.executeQuery(SELECT * FROM " + sTable + " WHERE 1=0)");

      oRSet = oStmt.executeQuery("SELECT * FROM " + sTable + " WHERE 1=0");

      oMDat = oRSet.getMetaData();

      cCols = oMDat.getColumnCount();

      alloc(cCols); // Función interna de soporte

      for (int c=0; c<cCols; c++) {
        ColNames[c] = oMDat.getColumnName(c+1);
        ColTypes[c] = oMDat.getColumnType(c+1);
        ColSizes[c] = oMDat.getPrecision(c+1);

        if (DebugFile.trace) DebugFile.writeln(ColNames[c] + " SQLType " + String.valueOf(ColTypes[c]) + " precision "  + ColSizes[c]);
      // next (c)

      oMDat = null;
View Full Code Here

   */
  public boolean load(JDCConnection oConn, Object[] PKVals) throws SQLException {
    boolean bRetVal;
    PreparedStatement oStmt;
    ResultSet oRSet;
    ResultSetMetaData oMDat;
    int iColCount;
    String sColName;
    String sSQL;

    if (DebugFile.trace) {
      DebugFile.writeln("Begin NewsGroup.load([Connection], ...)");
      DebugFile.incIdent();
      DebugFile.writeln("gu_newsgrp=" + (String) PKVals[0]);
    }

  getTable(oConn);
 
    clear();

    sSQL = "SELECT * FROM " + DB.k_categories + " WHERE " + DB.gu_category + "=?";

    if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")");

    oStmt = oConn.prepareStatement(sSQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

    oStmt.setString(1, (String) PKVals[0]);

    oRSet = oStmt.executeQuery();

    bRetVal = oRSet.next();

    if (bRetVal) {
      oMDat = oRSet.getMetaData();
      iColCount = oMDat.getColumnCount();

      for (int c=1; c<=iColCount; c++) {
        sColName = oMDat.getColumnName(c).toLowerCase();
        if (!sColName.equalsIgnoreCase(DB.dt_created));
          put(sColName, oRSet.getObject(c));
      } // next
      oMDat = null;
    } // fi (bRetVal)

    oRSet.close();
    oStmt.close();

    if (bRetVal) {
      sSQL = "SELECT * FROM " + DB.k_newsgroups + " WHERE " + DB.gu_newsgrp + "=?";
      if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")");
      oStmt = oConn.prepareStatement(sSQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
      oStmt.setString(1, (String) PKVals[0]);
      oRSet = oStmt.executeQuery();
      bRetVal = oRSet.next();
      if (bRetVal) {
        oMDat = oRSet.getMetaData();
        iColCount = oMDat.getColumnCount();

        for (int c=1; c<=iColCount; c++) {
          sColName = oMDat.getColumnName(c).toLowerCase();
          if (sColName.equalsIgnoreCase(DB.tx_journal)) {
        try {
        Reader oRdr = oRSet.getCharacterStream(c);
              if (!oRSet.wasNull()) {
            IBindingFactory bfact = BindingDirectory.getFactory(NewsGroupJournal.class);
View Full Code Here

         throw new IllegalArgumentException("ResultSetToXmlConverter: Please call setOutputStream() first");

      if (this.maxRows > 0 && this.rowCounter > this.maxRows)
         return;

      ResultSetMetaData meta = rs.getMetaData();
      int numberOfColumns = meta.getColumnCount();
      StringBuffer buf = new StringBuffer(4096);
    
      if (this.rowCounter == 0L) {
         // Create the header meta information
         buf.append("\n <desc>");
         //buf.append("\n  <statement><![CDATA[");
         //buf.append(rs.getStatement().toString()); // is not available!
         //buf.append("]]></statement>");
         if (this.command != null) {
            buf.append("\n  <command>").append(this.command).append("</command>");
         }
         if (this.ident != null) {
            buf.append("\n  <ident>").append(this.ident).append("</ident>");
         }

         // The CREATE command will allways have meta informations added
         if ((what == ALL && this.addMeta == true) || what == META_ONLY) { // rs.isFirst()) {
            for (int i=1; i<=numberOfColumns; i++) {
               buf.append("\n  <colname");
               String tn = meta.getTableName(i);
               if (tn != null && tn.length() > 0)
                  buf.append(" table='").append(meta.getTableName(i)).append("'");
               String schema = meta.getSchemaName(i);
               if (schema != null && schema.length() > 0)
                  buf.append(" schema='").append(schema).append("'");
               String catalog = meta.getCatalogName(i);
               if (catalog != null && catalog.length() > 0)
                  buf.append(" catalog='").append(catalog).append("'");
               buf.append(" type='").append(meta.getColumnTypeName(i)).append("'");
               if (meta.getPrecision(i) > 0)
                  buf.append(" precision='").append(meta.getPrecision(i)).append("'");
               if (meta.getScale(i) > 0)
                  buf.append(" scale='").append(meta.getScale(i)).append("'");
               // always write this since it is not a boolean and it has no default ...
               buf.append(" nullable='").append(meta.isNullable(i)).append("'");
               if (meta.isSigned(i)==false)
                  buf.append(" signed='").append(meta.isSigned(i)).append("'");
               if (meta.isReadOnly(i)==true)
                  buf.append(" readOnly='").append(meta.isReadOnly(i)).append("'");
               buf.append(">");
               buf.append(meta.getColumnName(i));
               buf.append("</colname>");
            }
         }
         if (this.transformer != null) {
            Map attr = this.transformer.transform(rs, -1);
            if (attr != null) {
               this.out.write(buf.toString().getBytes(this.charSet));
               buf.setLength(0);
               addInfo(attr);
            }
         }
         buf.append("\n </desc>");
         this.commandIsAdded = true;
      }

      if (what == ALL || what == ROW_ONLY) {
         buf.append("\n <row num='").append(""+this.rowCounter).append("'>");
         this.rowCounter++;

         for (int i=1; i<=numberOfColumns; i++) {
            // Possible attributes are 'name', 'size', 'type', 'encoding', see org.xmlBlaster.util.EncodableData
            buf.append("\n  <col name='").append(meta.getColumnName(i)).append("'");
            String value = rs.getString(i);
            int need = protectionNeeded(value);
            if (need == 0)
               buf.append(">").append(value==null?"":value);
            else if (need == 1)
View Full Code Here

      int rowCount = 0;
      String resultXml = "";
      ByteArrayOutputStream bout = null;
      BufferedOutputStream out = null;
      String command = "UPDATE";
      ResultSetMetaData rsmd = rs.getMetaData();
      int cols = rsmd.getColumnCount();
      StringBuffer buf = new StringBuffer(2048);

      // default if no grouping is configured
      String groupColValue = "${"+groupColName+"}";
      String newGroupColValue = null;
View Full Code Here

    String data = null;
    Text columnData = null;

    try
    {
      ResultSetMetaData metadata = results.getMetaData();
      while (results.next())
      {
        record = doc.createElement("Record");
        for (int i = 1; i <= metadata.getColumnCount(); i++)
        {
          String tableName = metadata.getTableName(i);
          if (tableName == null)
          {
            tableName = "";
          }

          if (tableName.equals(""))
          {
            column = doc.createElement(metadata.getColumnName(i));
          }
          else
          {
            column = doc.createElement(new StringBuffer(tableName)
                .append(".").append(metadata.getColumnName(i))
                .toString());
          }

          data = results.getString(i);
          if (data != null)
View Full Code Here

            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;
            }
View Full Code Here

    */
   public void fillFromTableSelectDELETED(ResultSet rs, boolean fillData, I_AttributeTransformer transformer) throws Exception {
      SqlDescription description = new SqlDescription(this.info);
      setDescription(description);
     
      ResultSetMetaData meta = rs.getMetaData();
      int numberOfColumns = meta.getColumnCount();
      String tableName = null;
      String schema = null;
      String catalog = null;
      for (int i=1; i <= numberOfColumns; i++) {
         SqlColumn col = new SqlColumn(this.info);
         description.addColumn(col);
         tableName = meta.getTableName(i);
         if (tableName != null && tableName.length() > 0)
            col.setTable(meta.getTableName(i));
         schema = meta.getSchemaName(i);
         if (schema != null && schema.length() > 0)
            col.setSchema(schema);
         catalog = meta.getCatalogName(i);
         if (catalog != null && catalog.length() > 0)
            col.setCatalog(catalog);
         col.setType(meta.getColumnTypeName(i));
         if (meta.getPrecision(i) > 0)
            col.setPrecision(meta.getPrecision(i));
         if (meta.getScale(i) > 0)
            col.setScale(meta.getScale(i));
         // always write this since it is not a boolean and it has no default ...
         col.setNullable(meta.isNullable(i));
         if (meta.isSigned(i)==false)
            col.setSigned(meta.isSigned(i));
         if (meta.isReadOnly(i)==true)
            col.setReadOnly(meta.isReadOnly(i));
      }
      // add PK and FK stuff 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));
                  col.setFkSchema(fkRs.getString(2));
                  col.setFkTable(fkRs.getString(3));
                  col.setFkCol(fkRs.getString(4));
                  col.setFkSeq(fkRs.getString(9));
                  col.setFkUpdRule(fkRs.getString(10));
                  col.setFkDelRule(fkRs.getString(11));
                  col.setFkDef(fkRs.getString(14));
               }
            }
         }
      }

      if (transformer != null) {
         Map attr = transformer.transform(rs, -1);
         if (attr != null) {
            Iterator iter = attr.entrySet().iterator();
            while (iter.hasNext()) {
               Map.Entry entry = (Map.Entry)iter.next();
               ClientProperty prop = new ClientProperty((String)entry.getKey(), null, null, entry.getValue().toString());
               description.setAttribute(prop);
            }
         }
      }
     
      if (fillData) {
         int count = 0;
         while (rs.next()) {
            SqlRow row = new SqlRow(this.info, count);
            count++;
            getRows().add(row);
            for (int i=1; i<=numberOfColumns; i++) {
               String value = rs.getString(i);
               ClientProperty prop = new ClientProperty(meta.getColumnName(i), null, null, value);
               row.setColumn(prop);
            }
            if (transformer != null) {
               Map attr = transformer.transform(rs, count);
               if (attr != null) {
View Full Code Here

TOP

Related Classes of java.sql.ResultSetMetaData

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.