简体   繁体   中英

Comparing values from resultset in java

I have created a method which produces a ResultSet with 1 row and 31 columns with with some column value being 1,2 or 0.I want to create a method which gives only those column name and values whose value is 1.

I tried in this way

int status[] = {0, 2};
public int[][] beam_CurrentStatus() {
  int arr[][] = new int[1][31];
  int i = 0;
  try

  {
    con = getConnection();
    stmt = con.createStatement();

    String sql = "SELECT TOP 1 c.logtime, a.BL1_data_SS_ST,a.BL2_data_SS_ST,a.BL3_data_SS_ST,a.BL4_data_SS_ST,a.BL5_data_SS_ST,a.BL6_data_SS_ST,a.BL7_data_SS_ST,a.BL8_data_SS_ST,a.BL9_data_SS_ST,a.BL10_data_SS_ST,a.BL11_data_SS_ST, a.BL12_data_SS_ST,a.BL13_data_SS_ST,a.BL14_data_SS_ST,a.BL15_data_SS_ST,a.BL16_data_SS_ST,a.BL17_data_SS_ST,a.BL18_data_SS_ST,a.BL19_data_SS_ST,a.BL20_data_SS_ST,a.BL21_data_SS_ST,a.BL22_data_SS_ST,a.BL23_data_SS_ST,a.BL24_data_SS_ST,a.BL25_data_SS_ST,a.BL26_data_SS_ST,a.BL27_data_SS_ST,b.st1_prmt_status_p45,c.beam_current,c.beam_energy from INDUS2_BLFE.dbo.main_BLFE_status a inner join INDUS2_MSIS.dbo.main_MSIS_status b on a.logtime=b.logtime inner join INDUS2_BDS.dbo.DCCT c on b.logtime=c.logtime ORDER BY c.logtime DESC ";

    stmt.executeQuery(sql);
    rs = stmt.getResultSet();

    while (rs.next()) {
      for (int j = 2; j < 29; j++) {
        if (!status.equals(rs.getInt(j)))
          arr[i][j] = rs.getInt(j);
      }
    }
  } catch (Exception e) {
    System.out.println("\nException in  Bean " + e);
  } finally {
    closeConnection(stmt, rs, con);
  }

  return arr;
}

But the output is coming as

[[I@553763.

How to resolve it??

The print statement is printing the string representation of the exception. To print a meaningful (human meaningful that is) string, you will need to replace this: System.out.println("\\nException in Bean " + e); with this: System.out.println("\\nException in Bean " + e.getMessage()); .

For more information, you can you e.printStackTrace() to print the stack trace for the exception.

Your logic is wrong. You are comparing an array to an integer.

Change it to :

  for (int j = 2; j < 29; j++) {
    if (rs.getInt(j)==1)
      arr[i][j] = rs.getInt(j);
  }

As to the output you see, you should print Arrays.deepToString(arr) . Printing the array as you currently do calls Object 's default toString() implementation, which doesn't display the content of the array.

This code can help you to achieve what you want. You will need to check for columnTypes in if block.

// Get resultset metadata
ResultSetMetaData meta = rs.getMetaData();

// Get count of columns
int columnCount = meta.getColumnCount();

// Iterate over all columns
for (int i = 0; i < columnCount; i++) { 
    // Get name of column
    String columnName = meta.getColumnName(i + 1);
    if (meta.getColumnType(i+1) == Types.INTEGER && rs.getInt(i+1)==1) {
        // if columnt type is integer and value is 1 print name and value
        System.out.println("Columnname:" + columnName);
        System.out.println("Columnvalue:" + rs.getInt(i+1)); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM