This is my Model class.
//Model public class CustomerData { private String locomotive_id; private String customer_name; private String road_number; private String locomotive_type_code; private String in_service_date; private String part_number; private String emission_tier_type; private String airbrake_type_code; private String lms_fleet; private String aar_road; private String locomotive_status_code; // Getters and Setters
Here is my implementation of RowMapper
//RowMapper public class CustomerDataResponseMapper implements RowMapper { @Override public Object mapRow(ResultSet rs, int count) throws SQLException { CustomerData customerData = new CustomerData(); customerData.setLocomotive_id(rs.getString("locomotive_id")); customerData.setCustomer_name(rs.getString("customer_name")); customerData.setRoad_number(rs.getString("road_number")); customerData.setLocomotive_type_code(rs.getString("locomotive_type_code")); customerData.setIn_service_date(rs.getString("in_service_date")); customerData.setPart_number(rs.getString("part_number")); customerData.setEmission_tier_type(rs.getString("emission_tier_type")); customerData.setAirbrake_type_code(rs.getString("airbrake_type_code")); customerData.setLms_fleet(rs.getString("lms_fleet")); customerData.setAar_road(rs.getString("aar_road")); customerData.setLocomotive_status_code(rs.getString("locomotive_status_code")); return customerData; } }
And finally, I got here the DaoImpl class
//DaoImpl public String getCustomersData(String locoId, String custName, String roadNumber) { CustomerData resultSet = null; String str = ""; if (locoId != null && locoId.length() > 0 && !(locoId.equals("0"))) { str = "select locomotive_id,customer_name,road_number,model_type as locomotive_type_code,to_char(in_service_date,'yyyy-mm-dd') as in_service_date,loco_part_number as part_number, emission_tier_type as emission_tier_type, " + "air_brake_type as airbrake_type_code,lms_fleet,aar_road,locomotive_status_code from get_rdf_explorer.get_rdf_locomotive_detail where locomotive_id = ?"; resultSet = (CustomerData) jdbcTemplate.queryForObject(str, new CustomerDataResponseMapper(), locoId); } else if ((custName != null && custName.length() > 0) && (roadNumber != null && roadNumber.length() > 0 && roadNumber != "0")) { str = "select locomotive_id,customer_name,road_number,model_type as locomotive_type_code,to_char(in_service_date,'yyyy-mm-dd') as in_service_date,loco_part_number as part_number, emission_tier_type as emission_tier_type, " + "air_brake_type as airbrake_type_code,lms_fleet,aar_road,locomotive_status_code from get_rdf_explorer.get_rdf_locomotive_detail where customer_name = ? and road_number= ?"; resultSet = (CustomerData) jdbcTemplate.queryForObject(str, new CustomerDataResponseMapper(), custName, roadNumber); } else { str = "select distinct customer_name from get_rdf_explorer.get_rdf_locomotive_detail order by customer_name asc"; resultSet = (CustomerData) jdbcTemplate.queryForObject(str, new CustomerDataResponseMapper()); } return resultSet.toString(); }
How can I conditionally get values ​​from a result set based on whether a particular column is present in the result set or not. Since I do not get all the columns all the time for my queries.
I get a SQL error with a grammar error when a particular column is missing in the resultSet. For example, when a third query is executed to obtain individual customer names, the result set will contain only the customer name and not the other columns.
That would be really great help. Thank you very much in advance.