ResultSet: getBigDecimal(int columnIndex) : ResultSet « java.sql « Java by API






ResultSet: getBigDecimal(int columnIndex)

 

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();

    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");

    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM Employees");

    printColumnInfo(rs);

    printColumnNames(rs);
    processRs(rs);

    rs = stmt.executeQuery("SELECT * FROM Location");
    printColumnInfo(rs);

    printColumnNames(rs);
    processRs(rs);
    conn.close();
  }

  public static void processRs(ResultSet rs) throws SQLException {
    ResultSetMetaData rmd = rs.getMetaData();
    while (rs.next()) {
      for (int col = 1; col <= rmd.getColumnCount(); col++)
        getData(rs, rmd.getColumnType(col), col);
    }
  }

  public static void printColumnNames(ResultSet rs) throws SQLException {
    ResultSetMetaData rmd = rs.getMetaData();
    for (int col = 1; col <= rmd.getColumnCount(); col++)
      System.out.println(rmd.getColumnName(col) + " ");
  }

  public static void getData(ResultSet rs, int type, int colIdx) throws SQLException {
    switch (type) {
    case java.sql.Types.CHAR:
    case java.sql.Types.VARCHAR:
      System.out.println(rs.getString(colIdx));
      break;

    case java.sql.Types.INTEGER:
      int i = rs.getInt(colIdx);
      System.out.println(i);
      break;

    case java.sql.Types.NUMERIC:
      BigDecimal bd = rs.getBigDecimal(colIdx);
      System.out.println(bd.toString());
      break;

    case java.sql.Types.TIMESTAMP:
    case java.sql.Types.DATE:
      java.sql.Date d = rs.getDate(colIdx);
      System.out.println(d.toString());
      break;

    }
  }

  public static void printColumnInfo(ResultSet rs) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int cols = rsmd.getColumnCount();
    for (int colIdx = 1; colIdx <= cols; colIdx++) {
      String name = rsmd.getColumnName(colIdx);
      int type = rsmd.getColumnType(colIdx);
      String typeName = rsmd.getColumnTypeName(colIdx);
      System.out.println(name + ", " + type + ", " + typeName);
    }
  }
}

   
  








Related examples in the same category

1.ResultSet.CLOSE_CURSORS_AT_COMMIT
2.ResultSet.CONCUR_READ_ONLY
3.ResultSet.CONCUR_UPDATABLE
4.ResultSet.HOLD_CURSORS_OVER_COMMIT
5.ResultSet.TYPE_SCROLL_INSENSITIVE
6.ResultSet.TYPE_SCROLL_SENSITIVE
7.ResultSet: TYPE_FORWARD_ONLY
8.ResultSet: absolute(int row)
9.ResultSet: beforeFirst()
10.ResultSet: cancelRowUpdates()
11.ResultSet: close()
12.ResultSet: deleteRow()
13.ResultSet: first()
14.ResultSet.getAsciiStream(int columnIndex)
15.ResultSet: getBinaryStream(int columnIndex)
16.ResultSet: getBlob(int columnIndex)
17.ResultSet: getBlob(String colName)
18.ResultSet: getBytes(int columnIndex)
19.ResultSet: getClob(int columnIndex)
20.ResultSet: getConcurrency()
21.ResultSet: getDate(int columnIndex)
22.ResultSet: getDouble(String columnLabel)
23.ResultSet: getInt(int columnIndex)
24.ResultSet: getInt(String columnName)
25.ResultSet: getMetaData()
26.ResultSet: getObject(int columnIndex)
27.ResultSet: getObject(String columnLabel)
28.ResultSet: getRef(int columnIndex)
29.ResultSet: getRow()
30.ResultSet: getShort(String columnLabel)
31.ResultSet: getString(int columnIndex)
32.ResultSet: getString(String columnLabel)
33.ResultSet: getTime(int columnIndex)
34.ResultSet: getTimestamp(int columnIndex)
35.ResultSet: getType()
36.ResultSet: getWarnings()
37.ResultSet: insertRow()
38.ResultSet: isAfterLast()
39.ResultSet: isBeforeFirst()
40.ResultSet: isFirst()
41.ResultSet: last()
42.ResultSet: moveToInsertRow()
43.ResultSet: next()
44.ResultSet: previous()
45.ResultSet: refreshRow()
46.ResultSet: relative(int rows)
47.ResultSet: setFetchSize(int rows)
48.ResultSet: updateInt(String columnLabel, int x)
49.ResultSet: updateRow()
50.ResultSet: updateDouble(int columnIndex, double x)
51.ResultSet: updateString(String columnName, String x)
52.ResultSet: wasNull()