ResultSet: getObject(String columnLabel) : ResultSet « java.sql « Java by API






ResultSet: getObject(String columnLabel)

 
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;
import java.sql.Statement;
import java.text.NumberFormat;
import java.util.Map;

class Employee implements SQLData {
  public BigDecimal SSN;

  public String FirstName;

  public String LastName;

  public BigDecimal Salary;

  private String sqlUdt;

  public void writeSQL(SQLOutput stream) throws SQLException {
    stream.writeBigDecimal(SSN);
    stream.writeString(FirstName);
    stream.writeString(LastName);
    stream.writeBigDecimal(Salary);
  }

  public String getSQLTypeName() throws SQLException {
    return sqlUdt;
  }

  public void readSQL(SQLInput stream, String typeName) throws SQLException {
    sqlUdt = typeName;
    SSN = stream.readBigDecimal();
    FirstName = stream.readString();
    LastName = stream.readString();
    Salary = stream.readBigDecimal();
  }

  public String calcMonthlySalary() {
    double monthlySalary = Salary.doubleValue() / 12;
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    String str = nf.format(monthlySalary);
    return str;
  }

}

public class Main {

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

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

    Statement stmt = conn.createStatement();

    Map map = conn.getTypeMap();
    map.put("EMP_DATA", Class.forName("Employee"));
    conn.setTypeMap(map);

    ResultSet rs = stmt.executeQuery("SELECT * from Emp");

    Employee employee;

    while (rs.next()) {
      int empId = rs.getInt("EmpId");
      employee = (Employee) rs.getObject("Emp_Info");

      System.out.print("Employee Id: " + empId + ", SSN: " + employee.SSN);
      System.out.print(", Name: " + employee.FirstName + " " + employee.LastName);
      System.out.println(", Yearly Salary: $" + employee.Salary + " Monthly Salary: "
          + employee.calcMonthlySalary());
    }
    conn.close();
  }
}

   
  








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: getBigDecimal(int columnIndex)
17.ResultSet: getBlob(int columnIndex)
18.ResultSet: getBlob(String colName)
19.ResultSet: getBytes(int columnIndex)
20.ResultSet: getClob(int columnIndex)
21.ResultSet: getConcurrency()
22.ResultSet: getDate(int columnIndex)
23.ResultSet: getDouble(String columnLabel)
24.ResultSet: getInt(int columnIndex)
25.ResultSet: getInt(String columnName)
26.ResultSet: getMetaData()
27.ResultSet: getObject(int columnIndex)
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()