I'm trying to populate Hashmap as following:
public static final String KEY_PROPNAME = "";
public static final String KEY_KEYWORDS = "";
public static final String KEY_THUMB_URI = "";
ArrayList<HashMap<String, String>> clipsList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < clipDetailsArr.length; i = i
+ constants.noOfColumns) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_PROPNAME, clipDetailsArr[i]);
map.put(KEY_PROPTYPE, clipDetailsArr[i + 1]);
map.put(KEY_PRICE, clipDetailsArr[i + 2]);
map.put(KEY_LOCATION, clipDetailsArr[i + 3]);
map.put(KEY_SQFT, clipDetailsArr[i + 4]);
map.put(KEY_SQFTTYPE, clipDetailsArr[i + 5]);
map.put(KEY_BED, clipDetailsArr[i + 6]);
map.put(KEY_BATH, clipDetailsArr[i + 7]);
map.put(KEY_KEYWORDS, clipDetailsArr[i + 8]);
map.put(KEY_THUMB_URI, clipDetailsArr[i + 9]);
Log.i("Main", "Clip clipDetailsArr[]: 0=" + clipDetailsArr[i] + " ,1=" + clipDetailsArr[i + 1]
+ " ,2=" + clipDetailsArr[i + 2] + " ,3=" + clipDetailsArr[i + 3] + " ,4="
+ clipDetailsArr[i + 4] + " ,5=" + clipDetailsArr[i + 5] + " ,6="
+ clipDetailsArr[i + 6] + " ,7=" + clipDetailsArr[i + 7] + " ,8="
+ clipDetailsArr[i + 8] + " ,9=" + clipDetailsArr[i + 9]);
Log.i("Main", "Clip PROPNAME=" + MainActivity.KEY_PROPNAME
+ " ,KEYWORDS" + MainActivity.KEY_KEYWORDS + " ,URI="
+ MainActivity.KEY_THUMB_URI);
Log.i("Main", "Clip get PROPNAME=" + map.get(KEY_PROPNAME)
+ " ,KEYWORDS" + map.get(KEY_KEYWORDS) + " ,URI="
+ map.get(KEY_THUMB_URI));
clipsList.add(map);
}
Output of log is something like this:
Clip clipDetailsArr[]: 0=Opt out ,1=Residentail ,2=10000 ,3=Andheri ,4=500 ,5=Carpet ,6=2 ,7=1 ,8=optout ,9=/mnt/sdcard/Clipping/optout.png
Clip PROPNAME= ,KEYWORDS ,URI=
Clip get PROPNAME=/mnt/sdcard/Clipping/optout.png,
KEYWORDS/mnt/sdcard/Clipping/optout.png,
URI=/mnt/sdcard/Clipping/optout.png//AND SO ON
Referring to above log, I have values in my array and I'm putting them into array by using map.put(key,value) but in 2nd log MainActivity.KEY_PROPNAME and other fields are empty. Also, when I use map.get(key), all the column have data of last column.
Note that last column data is getting populated appropriately.
Am I doing something wrong here? Any help appreciated.
You are using the same key "" for at least three .put() !! You have defined :
public static final String KEY_PROPNAME = "";
public static final String KEY_KEYWORDS = "";
public static final String KEY_THUMB_URI = "";
And then you populate the Map as :
map.put(KEY_PROPNAME, clipDetailsArr[i]);
map.put(KEY_KEYWORDS, clipDetailsArr[i + 8]);
map.put(KEY_THUMB_URI, clipDetailsArr[i + 9]);
As per the Javadoc:
If the map previously contained a mapping for the key, the old value is replaced.
public static final String KEY_PROPNAME = "";
public static final String KEY_KEYWORDS = "";
public static final String KEY_THUMB_URI = "";
Here all of your keys have same value/name, Hence all the data will store in the same key !!
public static final String KEY_PROPNAME = "";
public static final String KEY_KEYWORDS = "";
public static final String KEY_THUMB_URI = "";
This is the error, instead of creating empty strings from your name you should use them as the Keys to get your requested behavior.
Just to keep your code almost the same you could to something like:
public static final String KEY_PROPNAME = "KEY_PROPNAME";
public static final String KEY_KEYWORDS = "KEY_KEYWORDS";
public static final String KEY_THUMB_URI = "KEY_THUMB_URI";
That should to the trick even if it is a little funky ;)
Related
I have a bean class that successfully retrieves a string value from another class.= (It prints it just fine within the bean class)
When I try and call that class/string it returns as null.
Here is the relevant code:
public class cityModel implements Serializable {
private String fajr;
public void setFajr(String fajr) {
this.fajr= fajr;
}
public String getFajr() {
return fajr;
}
}
public void mutePrayerTime(View view) {
cityModel cityObj= new cityModel();
String fajr=cityObj.getFajr();
Log.d("LOGCAT", "" + cityObj.getFajr());
//StringBuilder newFajr = new StringBuilder(fajr);
//newFajr.delete(2,5);
//Log.d("newFajr", String.valueOf(newFajr));
// Intent alarm = new Intent(AlarmClock.ACTION_SET_ALARM);
//alarm.putExtra(AlarmClock.EXTRA_HOUR, fajr );
}
the Log.d tag LOGCAT returns as null
edit:
Code that the bean class retrieves the string from:
protected void outputTimings(JSONArray jsonArray) {
String[] prayers = {"fajr", "shurooq", "dhuhr", "asr", "maghrib", "isha"};
cityModel cityObj;
try {
cityObj= new cityModel();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject cityObject =
jsonArray.getJSONObject(i);
result = result + "fajr" + " : "
+ cityObject.getString("fajr") + "\n" + result + "shurooq" + " : "
+ cityObject.getString("shurooq") + "\n" + result + "dhuhr" + " : "
+ cityObject.getString("dhuhr") + "\n" + result + "asr" + " : "
+ cityObject.getString("asr") + "\n" + result + "maghrib" + " : "
+ cityObject.getString("maghrib") + "\n" + result + "isha" + " : "
+ cityObject.getString("isha") + "\n";
cityObj.setFajr(""+cityObject.getString("fajr"));
}
Have a look in this method:
public void mutePrayerTime(View view) {
cityModel cityObj= new cityModel();
String fajr=cityObj.getFajr();
Log.d("LOGCAT", "" + cityObj.getFajr());
//StringBuilder newFajr = new StringBuilder(fajr);
//newFajr.delete(2,5);
//Log.d("newFajr", String.valueOf(newFajr));
// Intent alarm = new Intent(AlarmClock.ACTION_SET_ALARM);
//alarm.putExtra(AlarmClock.EXTRA_HOUR, fajr );
}
You're just creating cityObj like this cityModel cityObj= new cityModel(); at that point all its properties are null that's why you're seeing null in your log. You should pass the cityObj from your outputTimings method to the mutePrayerTime method:
public void mutePrayerTime(View view,cityModel cityObj) {
Log.d("LOGCAT", "" + cityObj.getFajr());
//StringBuilder newFajr = new StringBuilder(fajr);
//newFajr.delete(2,5);
//Log.d("newFajr", String.valueOf(newFajr));
// Intent alarm = new Intent(AlarmClock.ACTION_SET_ALARM);
//alarm.putExtra(AlarmClock.EXTRA_HOUR, fajr );
}
and then in outputTimings:
cityObj.setFajr(""+cityObject.getString("fajr"));
someObj.mutePrayerTime(view, cityObj);
I want to create a run time String name in Java.
I tried something like using in JavaScript, but it is printing value like Status_Name_0 instead Open assigned to the String Status_Name_0
public static void GetPaymentStatusList(){
int i=0;
String Status_Name_0="Open";
String Status_ID_0="0";
String Status_Name_1="Approved";
String Status_ID_1="1";
String Status_Name_2="Denied";
String Status_ID_2="2";
for(i=0; i<3; i++){
Vars.PaymentStatusName_List.add("Status_Name_"+i);
Vars.PaymentStatusId_List.add("Status_ID_"+i);
}
}
but it is printing value like Status_Name_0 instead Open
Because that's what you added to the list...
add("Status_Name_"+i);
The way to get what you want would be a Map<String, String>
Map<String, String> map = new HashMap<>();
map.put("Status_Name_0", "Open");
// ...
for (int i=0;i<map.size();i++) {
String open = map.get("Status_Name_"+i);
}
How about you make some class instead, though?
public class PaymentStatus {
int id;
String name;
public PaymentStatus(int id, String name) {
this.id = id;
this.name = name;
}
#Override
public String toString() {
return String.format("%s[id: %d, name: %s]",
getClass().getSimpleName(), id, name);
}
}
And a List<PaymentStatus> would be preferred over appending integers to any variables.
public static List<PaymentStatus> getPaymentStatusList() {
List<PaymentStatus> list = new ArrayList<>();
paymentStatusList.add(new PaymentStatus(0, "Open"));
paymentStatusList.add(new PaymentStatus(1, "Approved"));
paymentStatusList.add(new PaymentStatus(2, "Denied"));
return list;
}
You're actually concatenating the string "Status_name_" with "0" which would result in "Status_name_0", a string, not a variable like Status_name_0. As far as I understand, you want the values of String_name_i (i= 0, 1, 2,....). To get that working, use String array.
String[] string_names = { "Open", "Approved", "Denied" };
int[] string_id = { 0, 1, 2 };
:You may not need a string_id array, as you can use the values of i in the for loop.
And add them in the list like:
Vars.PaymentStatusName_List.add(string_name[i]);
StringBuilder param = new StringBuilder();
param.append("shopid"
+ "=" + shopDetails.getShopId() + "&" + "username" + "=" + userDetail.getUserName() + "&" + "userid" + "=" + userDetail.getUserId() + "&");
for (int i = 0; i < brandList.size(); i++) {
param.append("brandId" + "[" + i + "]" + "=" + brandList.get(i).getBrandId()
+ "&" + "shortqua" + "[" + i + "]" + "=" + shortageStockList.get(i) + "&");
}
param.append("lattude" + "=" + String.valueOf(latitude) + "&" + "longitude" + "=" + String.valueOf(longitude));
I'm trying to create a Java parser for a Spark log created with Log4J.
I wrote this code to recognize a starting task log-line but it doesn't work and I can't figure out why.
This is the regex:
public static final String datePattern = "\\d{4}\\-\\d{2}\\-\\d{2}";
public static final String timePattern = "\\d{2}\\:\\d{2}\\:\\d{2}\\,\\d{3}";
public static final String timeStampPattern = "(?<timeStamp>" + datePattern + "\\s" + timePattern + ")";
public static final String logLevelPattern = "(?<logLevel>\\w+)";
public static final String loggingClassPattern = "(?<loggingClass>\\w+:)";
public static final String taskUIdPattern = "(?<UIdPattern>\\d+)";
public static final String taskIdPattern = "\\d.\\d:\\d+";
public static final String taskStatusPattern = null;
public static final String endTaskLabelPattern = null;
public static final String stringPatternStartTask = timeStampPattern +
" " + logLevelPattern +
" " + loggingClassPattern +
" " + "Starting task" +
" " + taskIdPattern +
" " + "as TID" +
" " + taskUIdPattern +
"\\z";
This is the parsing attempt:
Pattern patternStartTask = Pattern.compile(stringPatternStartTask);
...
while((temp = br.readLine()) != null) {
if((m = patternStartTask.matcher(temp)).matches()) {
System.out.println(temp);
le = new StartTaskEvent();
}
...
if(m != null && le != null) {
le.setTaskId(m.group("taskId"));
le.setLogLevel(m.group("logLevel"));
le.setLoggingClass(m.group("loggingClass"));
le.setTimeStamp(sdf.parse(m.group("timeStamp")));
result.add(le);
}
}
The lines I'm trying to recognize are like this one:
2016-01-08 14:01:02 INFO TaskSetManager: Starting task 1.0:0 as TID 0 on executor 1
Your regex ends with:
" " + "as TID" +
" " + taskUIdPattern +
"\\z";
but in your string you have on executor 1 after taskUIdPattern, you have to add on executor 1 or, better, on executor \\d in your regex after taskUIdPattern
I am attempting to run a method rgbScore() which passes in parameters from previous methods to calculate the difference between two pixels of a image. I would expect the result to be the difference between the two pixels but all I get is (0,0,0) ...
public void TypeButtonPressed()
{
filteredImage(GetType());
int type = GetType();
filteredrgbValue();
originalrgbValue();
rgbScore(originalred, filteredred, originalgreen, filteredgreen, originalblue, filteredblue); //THIS METHOD
JOptionPane.showMessageDialog(null, "Original RGB Value:" + oRGBValue + "\nFiltered RGB Value:" + fRGBValue + "\nScore:" + rgbDiff, Options[type], JOptionPane.INFORMATION_MESSAGE);
}
...
public String rgbScore(int originalred, int filteredred, int originalgreen, int filteredgreen, int originalblue, int filteredblue){
int redDiff = originalred - filteredred;
int greenDiff = originalgreen - filteredgreen;
int blueDiff = originalblue - filteredblue;
return rgbDiff = "(" + redDiff + "," + greenDiff + "," + blueDiff + ")";
}
I have declared the variables at the top of the class as follows:
public int filteredred; public int filteredgreen; public int filteredblue;
public int originalred; public int originalgreen; public int originalblue;
< Additional Information >
filteredrgbValue();
public String filteredrgbValue(){
BufferedImage filtered = filteredImage;
Color filteredRGBValue = new Color(filtered.getRGB(125, 125));
int filteredred = filteredRGBValue.getRed();
int filteredgreen = filteredRGBValue.getGreen();
int filteredblue = filteredRGBValue.getBlue();
return fRGBValue = "(" + filteredred + "," + filteredgreen + "," + filteredblue + ")";
}
originalrgbValue();
public String originalrgbValue(){
BufferedImage original = unfilteredImage;
Color originalRGBValue = new Color(original.getRGB(125, 125));
int originalred = originalRGBValue.getRed();
int originalgreen = originalRGBValue.getGreen();
int originalblue = originalRGBValue.getBlue();
return oRGBValue = "(" + originalred + "," + originalgreen + "," + originalblue + ")";
}
You are creating new variables in your methods which are hiding your member variables. Remove the type declarations, for example:
public String filteredrgbValue(){
BufferedImage filtered = filteredImage;
Color filteredRGBValue = new Color(filtered.getRGB(125, 125));
// you are declaring new variables here, hiding your member variables...
//int filteredred = filteredRGBValue.getRed();
//int filteredgreen = filteredRGBValue.getGreen();
//int filteredblue = filteredRGBValue.getBlue();
filteredred = filteredRGBValue.getRed();
filteredgreen = filteredRGBValue.getGreen();
filteredblue = filteredRGBValue.getBlue();
return fRGBValue = "(" + filteredred + "," + filteredgreen + "," + filteredblue + ")";
}
Looks like you have never initialized variables, you have just declared the vars.
defualt valu for int is 0 and until you dont initialize your vars it will have default vals.
I'm new to this so please forgive me if I tagged something incorrectly or left something out.
I'm writing a java program (new to java also) - the purpose of the program is to generate an XML file with information from multiple databases.
The setup - I have sql.java which is the main class and has the main method. Sql.java calls methods located in the CCReturns.java class, GBLRets.java class, and CWSReturns.java class. Each method returns a string of XML containing pertinent information and then the main method in sql.java puts them all together in one string and creates an xml file.
Problem: One of my methods in CWSReturns should return a resultset containing 74 rows in all but is only returning the data from one of the rows. When I put this same code into the sql.java main method all 74 rows are returned in the console but the xml file only shows the data from one of the rows and all of the data from all of my other methods is repeated even though I only need it to output once.
What would be the best way to go about fixing this issue? I'm stumped.
Method in CWSReturns:
public static String getUnitInfo(Connection connection, Statement stmt, ResultSet rs) throws SQLException, ClassNotFoundException
{
String unitinfo = null;
//Get Connection
connection = getCWSConnection();
//Create the SQL Query and put it into a String Variable
stmt = connection.createStatement();
//Pull Policy Claim Unit Information from CLM_UNIT Table
String query = "SELECT CLUT.UNIT_TYPE AS CLUNITTYPE, CLUT.UNIT_SUBTYPE AS CLUNITSUBTYPE, CLUT.UNIT_CATEGORY AS CLUNITCATEGORY, CLUT.UNIT_IDENTIFIER AS CLUNITIDENTIFIER, CLUT.UNIT_NUM AS CLUNITNUM, " +
"CLUT.YEAR AS CLUNITYEAR, CLUT.MAKE AS CLMAKE, CLUT.MODEL AS CLMODEL, CLUT.VEHICLE_ID AS CLVEHICLEID, CLUT.ITEM_DESC1 AS CLITEMDESC1, CLUT.LICENSE, " +
"DAM.LOCATION1, DAM.DESC1, " +
"UNT.UNIT_TYPE, UNT.UNIT_SUB_TYPE, UNT.UNIT_CATEGORY, UNT.UNIT_IDENTIFIER, UNT.UNIT_NUM, UNT.YEAR, UNT.MAKE, UNT.MODEL, UNT.VEHICLE_ID, UNT.LICENSE, UNT.ITEM_DESC, " +
//Pull Coverage Information from POL_COVERAGE Table
"COV.COVERAGE_TYPE, COV.DED_TYPE_CODE1, COV.DEDUCTIBLE1, COV.DED_TYPE_CODE2, COV.DEDUCTIBLE2, COV.DED_TYPE_CODE3, COV.DEDUCTIBLE3, COV.LIMIT_TYPE1, COV.LIMIT1, " +
"COV.LIMIT_TYPE2, COV.LIMIT2, COV.LIMIT_TYPE3, COV.LIMIT3, COV.LIMIT_TYPE4, COV.LIMIT4 " +
"FROM DB2ADMIN.CLM_CLAIM CLM, DB2ADMIN.CLM_UNIT CLUT, DB2ADMIN.POL_GENERAL_REC POL, DB2ADMIN.POL_UNIT UNT, DB2ADMIN.POL_COVERAGE COV, DB2ADMIN.CLM_DAMAGE DAM " +
"WHERE CLM.CLAIM_ID = CLUT.CLAIM_ID AND CLM.POLICY_ID = POL.POLICY_ID AND POL.POLICY_ID = UNT.POLICY_ID AND UNT.POL_UNIT_ID = COV.POL_UNIT_ID AND CLUT.UNIT_ID = DAM.UNIT_ID " +
"AND CLM.CLAIM_ID = 14701";
//Execute the query and save it as a ResultSet
rs = stmt.executeQuery(query);
//Pull out all of the information and save it as a string
while(rs.next())
{
//Retrieve by column name
//Claim Unit Info
String CL_UNIT_YEAR = "<CL_UNIT_YEAR>" + rs.getString("CLUNITYEAR") + "</CL_UNIT_YEAR>\n";
String CL_UNIT_TYPE = "<CL_UNIT_TYPE>" + rs.getString("CLUNITTYPE") + "</CL_UNIT_TYPE>\n";
String CL_UNIT_SUB_TYPE = "<CL_UNIT_SUB_TYPE>" + rs.getString("CLUNITSUBTYPE") + "</CL_UNIT_SUB_TYPE>\n";
String CL_UNIT_CATEGORY = "<CL_UNIT_CATEGORY>" + rs.getString("CLUNITCATEGORY") + "</CL_UNIT_CATEGORY>\n";
String CL_UNIT_IDENTIFIER = "<CL_UNIT_IDENTIFIER>" + rs.getString("CLUNITIDENTIFIER") + "</CL_UNIT_IDENTIFIER>\n";
String CL_UNIT_NUM = "<CL_UNIT_NUM>" + rs.getString("CLUNITNUM") + "</CL_UNIT_NUM>\n";
String CL_UNIT_MAKE = "<CL_UNIT_MAKE>" + rs.getString("CLMAKE") + "</CL_UNIT_MAKE>\n";
String CL_UNIT_MODEL = "<CL_UNIT_MODEL>" + rs.getString("CLMODEL") + "</CL_UNIT_MODEL>\n";
String CL_UNIT_VEH_ID = "<CL_UNIT_VEH_ID>" + rs.getString("CLVEHICLEID") + "</CL_UNIT_VEH_ID>\n";
String CL_UNIT_DESC1 = "<CL_UNIT_DESC1>" + rs.getString("CLITEMDESC1") + "</CL_UNIT_DESC1>\n";
String TAG_NUMBER = "<TAG_NUMBER>" + rs.getString("LICENSE") + "</TAG_NUMBER>\n";
String DAMLOC = "<DAMAGE_LOCATION>" + rs.getString("LOCATION1") + "</DAMAGE_LOCATION>\n";
String DAMDESC = "<DAMAGE_DESCRIPTION>" + rs.getString("DESC1") + "</DAMAGE_DESCRIPTION>\n";
String UNIT_TYPE = "<UNIT_TYPE>" + rs.getString("UNIT_TYPE") + "</UNIT_TYPE>\n";
String UNIT_SUB_TYPE = "<UNIT_SUB_TYPE>" + rs.getString("UNIT_SUB_TYPE") + "</UNIT_SUB_TYPE>\n";
String UNIT_CATEGORY = "<UNIT_CATEGORY>" + rs.getString("UNIT_CATEGORY") + "</UNIT_CATEGORY>\n";
String UNIT_IDENTIFIER = "<UNIT_IDENTIFIER>" + rs.getString("UNIT_IDENTIFIER") + "</UNIT_IDENTIFIER>\n";
String UNIT_NUMBER = "<UNIT_NUMBER>" + rs.getString("UNIT_NUM") + "</UNIT_NUMBER>\n";
String UNIT_YEAR = "<UNIT_YEAR>" + rs.getString("YEAR") + "</UNIT_YEAR>\n";
String UNIT_MAKE = "<UNIT_MAKE>" + rs.getString("MAKE") + "</UNIT_MAKE>\n";
String UNIT_MODEL = "<UNIT_MODEL>" + rs.getString("MODEL") + "</UNIT_MODEL>\n";
String VEH_ID = "<VEH_ID>" + rs.getString("VEHICLE_ID") + "</VEH_ID>\n";
String ITEM_DESC = "<ITEM_DESC>" + rs.getString("ITEM_DESC") + "</ITEM_DESC>\n";
//Coverage Info
String COVERAGE_TYPE = "<COVERAGE_TYPE>" + rs.getString("COVERAGE_TYPE") + "</COVERAGE_TYPE>\n";
String DED_TYPE_CODE1 = "<DED_TYPE_CODE1>" + rs.getString("DED_TYPE_CODE1") + "</DED_TYPE_CODE1>\n";
String DEDUCTIBLE1 = "<DEDUCTIBLE1>" + rs.getString("DEDUCTIBLE1") + "</DEDUCTIBLE1>\n";
String DED_TYPE_CODE2 = "<DED_TYPE_CODE2>" + rs.getString("DED_TYPE_CODE2") + "</DED_TYPE_CODE2>\n";
String DEDUCTIBLE2 = "<DEDUCTIBLE2>" + rs.getString("DEDUCTIBLE2") + "</DEDUCTIBLE2>\n";
String DED_TYPE_CODE3 = "<DED_TYPE_CODE3>" + rs.getString("DED_TYPE_CODE3") + "</DED_TYPE_CODE3>\n";
String DEDUCTIBLE3 = "<DEDUCTIBLE3>" + rs.getString("DEDUCTIBLE3") + "</DEDUCTIBLE3>\n";
String LIMIT_TYPE1 = "<LIMIT_TYPE1>" + rs.getString("LIMIT_TYPE1") + "</LIMIT_TYPE1>\n";
String LIMIT1 = "<LIMIT1>" + rs.getString("LIMIT1") + "</LIMIT1>\n";
String LIMIT_TYPE2 = "<LIMIT_TYPE2>" + rs.getString("LIMIT_TYPE2") + "</LIMIT_TYPE2>\n";
String LIMIT2 = "<LIMIT2>" + rs.getString("LIMIT2") + "</LIMIT2>\n";
String LIMIT_TYPE3 = "<LIMIT_TYPE3>" + rs.getString("LIMIT_TYPE3") + "</LIMIT_TYPE3>\n";
String LIMIT3 = "<LIMIT3>" + rs.getString("LIMIT3") + "</LIMIT3>\n";
String LIMIT_TYPE4 = "<LIMIT_TYPE4>" + rs.getString("LIMIT_TYPE4") + "</LIMIT_TYPE4>\n";
String LIMIT4 = "<LIMIT4>" + rs.getString("LIMIT4") + "</LIMIT4>\n";
//Create one large string that incorporates all of the above nodes
String unitinfo1 = CL_UNIT_YEAR + CL_UNIT_TYPE + CL_UNIT_SUB_TYPE + CL_UNIT_CATEGORY + CL_UNIT_IDENTIFIER +
CL_UNIT_NUM + CL_UNIT_MAKE + CL_UNIT_MODEL + CL_UNIT_VEH_ID + CL_UNIT_DESC1 + TAG_NUMBER + DAMLOC + DAMDESC +
UNIT_TYPE + UNIT_SUB_TYPE + UNIT_CATEGORY + UNIT_IDENTIFIER + UNIT_NUMBER + UNIT_YEAR + UNIT_MAKE +
UNIT_MODEL + VEH_ID + ITEM_DESC + COVERAGE_TYPE + DED_TYPE_CODE1 + DEDUCTIBLE1 + DED_TYPE_CODE2 +
DEDUCTIBLE2 + DED_TYPE_CODE3 + DEDUCTIBLE3 + LIMIT_TYPE1 + LIMIT1 + LIMIT_TYPE2 + LIMIT2 +
LIMIT_TYPE3 + LIMIT3 + LIMIT_TYPE4 + LIMIT4;
return unitinfo1;
}
stmt.close();
rs.close();
connection.close();
return unitinfo;
}
sql.java - main method snippet:
//Get unit info
String unitinfo = CWSReturns.getUnitInfo(connection, stmt, rs);
String xmlStr = (Root+mainclaimnode+mainclaiminfo+lossState+clientname+clientaddress+communicationinfo+agentname+adjustername+secondaryclientname+policyinfo+cancelpendinginfo+endmainclaimnode+claimunitnode+unitinfo+OIPName+OIPAddress+rollinjuryinfo+unitaddress+endclaimunitnode+EndRoot);
Document doc = convertStringToDocument(xmlStr);
String str = convertDocumentToString(doc);
System.out.println(str);
PrintWriter writer = new PrintWriter("C:\\Temp\\TestXML.xml");
writer.println(str);
writer.close();
Output when running method from CWSReturns (only one resultset returned...)
<CWS_XML>
<MAIN_CLAIM_INFO>
<CLAIM_ID>14701</CLAIM_ID>
<DATE_LOSS>2013-09-01 04:00:00.0</DATE_LOSS>
<CLAIM_MADE_DATE>null</CLAIM_MADE_DATE>
<CALLER_NAME>asdf asdf</CALLER_NAME>
<ACTUAL_NOT_DATE>2014-02-25 10:25:00.0</ACTUAL_NOT_DATE>
<METHOD_REPORT>PHONE</METHOD_REPORT>
<NAME_TYPE_FLAG>I</NAME_TYPE_FLAG>
<NAME_TYPE>null</NAME_TYPE>
<NAME_PREFIX>null</NAME_PREFIX>
<LAST_NAME>Luke</LAST_NAME>
<NAME_SUFFIX>null</NAME_SUFFIX>
</MAIN_CLAIM_INFO>
**<CLAIM_UNIT_INFO>
<CL_UNIT_YEAR>2014</CL_UNIT_YEAR>
<CL_UNIT_TYPE>DRIVE_OTHR</CL_UNIT_TYPE>
<CL_UNIT_SUB_TYPE>COMBO</CL_UNIT_SUB_TYPE>
<CL_UNIT_CATEGORY>DRIVE_OTHR</CL_UNIT_CATEGORY>
<CL_UNIT_IDENTIFIER>2014 Cadillac</CL_UNIT_IDENTIFIER>
<CL_UNIT_NUM/>
<CL_UNIT_MAKE>Cadillac </CL_UNIT_MAKE>
<CL_UNIT_MODEL/>
<CL_UNIT_VEH_ID/>
<CL_UNIT_DESC1>null</CL_UNIT_DESC1>
<TAG_NUMBER/>
<DAMAGE_LOCATION>Unknown</DAMAGE_LOCATION>
<DAMAGE_DESCRIPTION>Unknown</DAMAGE_DESCRIPTION>
<UNIT_TYPE>NON_OWNED</UNIT_TYPE>
<UNIT_SUB_TYPE>COMBO</UNIT_SUB_TYPE>
<UNIT_CATEGORY>NON_OWNED</UNIT_CATEGORY>
<UNIT_IDENTIFIER>NON OWNED</UNIT_IDENTIFIER>
<UNIT_NUMBER>null</UNIT_NUMBER>
<UNIT_YEAR>null</UNIT_YEAR>
<UNIT_MAKE>null</UNIT_MAKE>
<UNIT_MODEL>null</UNIT_MODEL>
<VEH_ID>null</VEH_ID>
<ITEM_DESC>null</ITEM_DESC>
<COVERAGE_TYPE>ADB</COVERAGE_TYPE>
<DED_TYPE_CODE1>null</DED_TYPE_CODE1>
<DEDUCTIBLE1>null</DEDUCTIBLE1>
<DED_TYPE_CODE2>null</DED_TYPE_CODE2>
<DEDUCTIBLE2>null</DEDUCTIBLE2>
<DED_TYPE_CODE3>null</DED_TYPE_CODE3>
<DEDUCTIBLE3>null</DEDUCTIBLE3>
<LIMIT_TYPE1>LIM</LIMIT_TYPE1>
<LIMIT1>15000.000</LIMIT1>
<LIMIT_TYPE2>null</LIMIT_TYPE2>
<LIMIT2>null</LIMIT2>
<LIMIT_TYPE3>null</LIMIT_TYPE3>
<LIMIT3>null</LIMIT3>
<LIMIT_TYPE4>null</LIMIT_TYPE4>
<LIMIT4>null</LIMIT4>
<OIP_NAME>Null</OIP_NAME>
<OIP_ADDR>Null</OIP_ADDR>
<ROLE_TYPE>DRIVER</ROLE_TYPE>
<INJURY_TEXT>head</INJURY_TEXT>
<CL_UNIT_ID>Null</CL_UNIT_ID>
<CL_UNIT_HOUSE>Null</CL_UNIT_HOUSE>
<CL_UNIT_ADDR1>Null</CL_UNIT_ADDR1>
<CL_UNIT_ADDR2>Null</CL_UNIT_ADDR2>
<CL_UNIT_CITY>Null</CL_UNIT_CITY>
<CL_UNIT_STATE>Null</CL_UNIT_STATE>
<CL_UNIT_ZIP>Null</CL_UNIT_ZIP>
</CLAIM_UNIT_INFO>**
</CWS_XML>
The elements in the "CLAIM_UNIT_INFO" node should repeat upwards of 74 times...
Inside the while loop you are returning. So it iterates only one time.
Make unitinfo in getUnitInfo() method as a StringBuilder and inside the while(rs.next) instead or returning append the unitinfo1 to unitinfo
public static String getUnitInfo(Connection connection, Statement stmt, ResultSet rs) throws SQLException, ClassNotFoundException
{
StringBuilder unitinfo = new StringBuilder();
...
while(rs.next()) {
...
unitinfo.append("<CLAIM_UNIT_INFO>");
//Create one large string that incorporates all of the above nodes
String unitinfo1 = CL_UNIT_YEAR + CL_UNIT_TYPE + CL_UNIT_SUB_TYPE + CL_UNIT_CATEGORY + CL_UNIT_IDENTIFIER +
CL_UNIT_NUM + CL_UNIT_MAKE + CL_UNIT_MODEL + CL_UNIT_VEH_ID + CL_UNIT_DESC1 + TAG_NUMBER + DAMLOC + DAMDESC +
UNIT_TYPE + UNIT_SUB_TYPE + UNIT_CATEGORY + UNIT_IDENTIFIER + UNIT_NUMBER + UNIT_YEAR + UNIT_MAKE +
UNIT_MODEL + VEH_ID + ITEM_DESC + COVERAGE_TYPE + DED_TYPE_CODE1 + DEDUCTIBLE1 + DED_TYPE_CODE2 +
DEDUCTIBLE2 + DED_TYPE_CODE3 + DEDUCTIBLE3 + LIMIT_TYPE1 + LIMIT1 + LIMIT_TYPE2 + LIMIT2 +
LIMIT_TYPE3 + LIMIT3 + LIMIT_TYPE4 + LIMIT4;
unitinfo.append(unitinfo1);
unitinfo.append("</CLAIM_UNIT_INFO>");
}
...
return unitinfo.toString();
}
As #SyamS mentioned you are returning inside of your loop.
If you don't want to combine all of the rows into one String: One way to fix this would be to store the String found in the loop into an ArrayList, and then return the ArrayList of String.
You would have to change the return type of your method, and handle iterating through the resulting ArrayList when you called the method.
public static ArrayList<String> getUnitInfo(...){
ArrayList<String> unitinfo = new ArrayList<String>();
...
while(...){
...
unitinfo.add(unitinfo1); // Instead of return
}
...
return unitinfo; // Only return at the end
}
The reason this is happening is because you have return unitinfo1 inside your while loop. So it is just returning after the first row has been populated.
You need to declare String unitinfo1 outside of the while loop, and append each line to the string during each iteration of the loop.
String unitinfo1;
while(condition)
{
unitinfo1.append(nextLine);
}
return unitinfo1;