Package java.sql

Examples of java.sql.ResultSetMetaData


     */
    protected String nextLine() throws IOException{       
        try {
            if (firstTime) {
                firstTime = false;
                ResultSetMetaData metadata = source.getMetaData();
                columnCount = metadata.getColumnCount();
                columnTypes  = new int[columnCount];
                for (int i = 0; i < columnCount; i++) {
                    columnTypes[i] = metadata.getColumnType(i+1);
                }
                return resultSetMetaDataToString(metadata, delimiter);
            }
           
            // if you get here then we are ready to read the results.
View Full Code Here


    try {
      if (nextFuture != null) {
        sendErrorResponse(new IllegalStateException("Pending results have not been sent")); //$NON-NLS-1$
      }
         
      ResultSetMetaData meta = rs.getMetaData();
        List<PgColInfo> cols = getPgColInfo(meta, rs.getStatement());
          if (describeRows) {
            sendRowDescription(cols);
          }
          Runnable r = new ResultsWorkItem(cols, sql, rs, result);
View Full Code Here

      }
    }
    ResultSet rs = getResultSet();
    if (m_Debug)
      System.err.println("Getting metadata...");
    ResultSetMetaData md = rs.getMetaData();
    if (m_Debug)
      System.err.println("Completed getting metadata...");
   
   
    // Determine structure of the instances
    int numAttributes = md.getColumnCount();
    int [] attributeTypes = new int [numAttributes];
    Hashtable [] nominalIndexes = new Hashtable [numAttributes];
    FastVector [] nominalStrings = new FastVector [numAttributes];
    for (int i = 1; i <= numAttributes; i++) {
      /* switch (md.getColumnType(i)) {
      case Types.CHAR:
      case Types.VARCHAR:
      case Types.LONGVARCHAR:
      case Types.BINARY:
      case Types.VARBINARY:
      case Types.LONGVARBINARY:*/
     
      switch (translateDBColumnType(md.getColumnTypeName(i))) {
 
      case STRING :
  //System.err.println("String --> nominal");
  attributeTypes[i - 1] = Attribute.NOMINAL;
  nominalIndexes[i - 1] = new Hashtable();
  nominalStrings[i - 1] = new FastVector();
  break;
      case TEXT:
  //System.err.println("Text --> string");
  attributeTypes[i - 1] = Attribute.STRING;
  nominalIndexes[i - 1] = new Hashtable();
  nominalStrings[i - 1] = new FastVector();
  break;
      case BOOL:
  //System.err.println("boolean --> nominal");
  attributeTypes[i - 1] = Attribute.NOMINAL;
  nominalIndexes[i - 1] = new Hashtable();
  nominalIndexes[i - 1].put("false", new Double(0));
  nominalIndexes[i - 1].put("true", new Double(1));
  nominalStrings[i - 1] = new FastVector();
  nominalStrings[i - 1].addElement("false");
  nominalStrings[i - 1].addElement("true");
  break;
      case DOUBLE:
  //System.err.println("BigDecimal --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case BYTE:
  //System.err.println("byte --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case SHORT:
  //System.err.println("short --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case INTEGER:
  //System.err.println("int --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case LONG:
  //System.err.println("long --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case FLOAT:
  //System.err.println("float --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case DATE:
  attributeTypes[i - 1] = Attribute.DATE;
  break;
      case TIME:
  attributeTypes[i - 1] = Attribute.DATE;
  break;
      default:
  //System.err.println("Unknown column type");
  attributeTypes[i - 1] = Attribute.STRING;
      }
    }

    // For sqlite
    // cache column names because the last while(rs.next()) { iteration for
    // the tuples below will close the md object: 
    Vector<String> columnNames = new Vector<String>();
    for (int i = 0; i < numAttributes; i++) {
      columnNames.add(md.getColumnLabel(i + 1));
    }

    // Step through the tuples
    if (m_Debug)
      System.err.println("Creating instances...");
    FastVector instances = new FastVector();
    int rowCount = 0;
    while(rs.next()) {
      if (rowCount % 100 == 0) {
        if (m_Debug)  {
    System.err.print("read " + rowCount + " instances \r");
    System.err.flush();
        }
      }
      double[] vals = new double[numAttributes];
      for(int i = 1; i <= numAttributes; i++) {
  /*switch (md.getColumnType(i)) {
  case Types.CHAR:
  case Types.VARCHAR:
  case Types.LONGVARCHAR:
  case Types.BINARY:
  case Types.VARBINARY:
  case Types.LONGVARBINARY:*/
  switch (translateDBColumnType(md.getColumnTypeName(i))) {
  case STRING :
    String str = rs.getString(i);
   
    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
View Full Code Here

    query += key[i].toString();
  }
      }
    }
    ResultSet rs = select(query);
    ResultSetMetaData md = rs.getMetaData();
    int numAttributes = md.getColumnCount();
    if (!rs.next()) {
      throw new Exception("No result for query: " + query);
    }
    // Extract the columns for the result
    Object [] result = new Object [numAttributes];
    for(int i = 1; i <= numAttributes; i++) {
      switch (translateDBColumnType(md.getColumnTypeName(i))) {
  case STRING :
    result[i - 1] = rs.getString(i);
    if (rs.wasNull()) {
      result[i - 1] = null;
    }
    break;
  case FLOAT:
  case DOUBLE:
    result[i - 1] = new Double(rs.getDouble(i));
    if (rs.wasNull()) {
      result[i - 1] = null;
    }
    break;
  default:
    throw new Exception("Unhandled SQL result type (field " + (i + 1)
        + "): "
        + DatabaseUtils.typeName(md.getColumnType(i)));
      }
    }
    if (rs.next()) {
      throw new Exception("More than one result entry "
        + "for result key: " + query);
View Full Code Here

     * @param rs0 the first result set
     * @param rs1 the second result set
     * @throws AssertionError if the values are not equal
     */
    protected void assertEquals(String message, ResultSet rs0, ResultSet rs1) throws SQLException {
        ResultSetMetaData meta = rs0.getMetaData();
        int columns = meta.getColumnCount();
        assertEquals(columns, rs1.getMetaData().getColumnCount());
        while (rs0.next()) {
            assertTrue(message, rs1.next());
            for (int i = 0; i < columns; i++) {
                assertEquals(message, rs0.getString(i + 1), rs1.getString(i + 1));
View Full Code Here

     * @param precision the expected precisions
     * @param scale the expected scales
     */
    protected void assertResultSetMeta(ResultSet rs, int columnCount, String[] labels, int[] datatypes, int[] precision,
            int[] scale) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        int cc = meta.getColumnCount();
        if (cc != columnCount) {
            fail("result set contains " + cc + " columns not " + columnCount);
        }
        for (int i = 0; i < columnCount; i++) {
            if (labels != null) {
                String l = meta.getColumnLabel(i + 1);
                if (!labels[i].equals(l)) {
                    fail("column label " + i + " is " + l + " not " + labels[i]);
                }
            }
            if (datatypes != null) {
                int t = meta.getColumnType(i + 1);
                if (datatypes[i] != t) {
                    fail("column datatype " + i + " is " + t + " not " + datatypes[i] + " (prec="
                            + meta.getPrecision(i + 1) + " scale=" + meta.getScale(i + 1) + ")");
                }
                String typeName = meta.getColumnTypeName(i + 1);
                String className = meta.getColumnClassName(i + 1);
                switch (t) {
                case Types.INTEGER:
                    assertEquals("INTEGER", typeName);
                    assertEquals("java.lang.Integer", className);
                    break;
                case Types.VARCHAR:
                    assertEquals("VARCHAR", typeName);
                    assertEquals("java.lang.String", className);
                    break;
                case Types.SMALLINT:
                    assertEquals("SMALLINT", typeName);
                    assertEquals("java.lang.Short", className);
                    break;
                case Types.TIMESTAMP:
                    assertEquals("TIMESTAMP", typeName);
                    assertEquals("java.sql.Timestamp", className);
                    break;
                case Types.DECIMAL:
                    assertEquals("DECIMAL", typeName);
                    assertEquals("java.math.BigDecimal", className);
                    break;
                default:
                }
            }
            if (precision != null) {
                int p = meta.getPrecision(i + 1);
                if (precision[i] != p) {
                    fail("column precision " + i + " is " + p + " not " + precision[i]);
                }
            }
            if (scale != null) {
                int s = meta.getScale(i + 1);
                if (scale[i] != s) {
                    fail("column scale " + i + " is " + s + " not " + scale[i]);
                }
            }

View Full Code Here

        prep.setString(2, "Hello");
        prep.execute();
        rs = stat.executeQuery("select * from test");
        rs.next();

        ResultSetMetaData rsMeta = rs.getMetaData();
        assertEquals(Types.INTEGER, rsMeta.getColumnType(1));
        assertEquals(Types.VARCHAR, rsMeta.getColumnType(2));

        prep.close();
        assertEquals(1, rs.getInt(1));
        assertEquals("Hello", rs.getString(2));
        assertFalse(rs.next());
View Full Code Here

        }
    return createResultSet(records, metadata);
  }
 
    ResultSetImpl createResultSet(List records, Map[] columnMetadata) throws SQLException {
        ResultSetMetaData rsmd = new ResultSetMetaDataImpl(new MetadataProvider(columnMetadata));

        return createResultSet(records, rsmd);
    }
View Full Code Here

        columnAlias = "MySQL,Regular";
        stat.execute("CREATE TABLE TEST(ID INT)");
        for (String mode : modes) {
            stat.execute("SET MODE " + mode);
            ResultSet rs = stat.executeQuery("SELECT ID I FROM TEST");
            ResultSetMetaData meta = rs.getMetaData();
            String columnName = meta.getColumnName(1);
            String tableName = meta.getTableName(1);
            if ("ID".equals(columnName) && "TEST".equals(tableName)) {
                assertTrue(mode + " mode should not support columnAlias", columnAlias.indexOf(mode) >= 0);
            } else if ("I".equals(columnName) && tableName == null) {
                assertTrue(mode + " mode should support columnAlias", columnAlias.indexOf(mode) < 0);
            } else {
View Full Code Here

    try {
      Statement statement = connection.createStatement();
     
      ResultSet results = statement.executeQuery(sql);
     
      ResultSetMetaData metadata = results.getMetaData();
      int columns = metadata.getColumnCount();
      System.out.println("Results");
      for (int row = 1; results.next(); row++) {
        System.out.print(row + ": ");
        for (int i = 0; i < columns; i++) {
          if (i > 0) {
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.