Reputation: 49
I have fetch data from database and put result-set in Array-list. I need to Print each String from Array-list can split or trim to string. Ouput for below code and Expect result.
public static ArrayList<Object> myList = new ArrayList<Object>();
public ArrayList<Object> databaseconection(WebDriver driver, String sqlquery)
throws InterruptedException, SQLException {
Statement stmt = null;
Connection conn = null;
DriverManager.registerDriver(new SybDriver());
Thread.sleep(1000);
String url = "***************";
conn = DriverManager.getConnection(url, "451552", "Welcome" conn.setAutoCommit(false);
stmt = conn.createStatement();
// Database query to get value
ResultSet rs = stmt.executeQuery(sqlquery);
while (rs.next()) {
for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
// System.out.print(" " + rs.getMetaData().getColumnName(i) + "=" +
// rs.getObject(i));
// System.out.println("");
myList.add(rs.getObject(i));
}
System.out.println(myList);
// round off to nearest million
for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
Object object2 = rs.getObject(i);
object2 = ((BigDecimal) object2).divide(new BigDecimal("1000000")).setScale(0, RoundingMode.HALF_DOWN)
+ "";
System.out.println(object2);
// Print each Array from my list
Add_Log.info(myList.toString() + " round off to nearest million = " + object2);
Reporter.log(myList.toString() + " round off to nearest million = " + object2);
}
}
return myList;
}
Console result for above code.
[1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436]
1995538
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =1995538
1131933
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =1131933
1174448
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =1174448
39942
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =39942
2224235
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =2224235
2940469
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =2940469
I Expect output each line
[1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] mylist Arraylist
///// Each line
[1995537811893.93004] round off to nearest million =1995538
[1131932531051.9879252] round off to nearest million = 1131933
[1174448486100.98669] round off to nearest million = 1174448
[39942430884.26804] round off to nearest million = 39942
[2224235008605.54787] round off to nearest million = 2224235
[2940469028187.27436] round off to nearest million = 2940469
Upvotes: 0
Views: 71
Reputation: 49
ResultSet rs = stmt.executeQuery(sqlquery);
while (rs.next()) {
for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
// System.out.print(" " + rs.getMetaData().getColumnName(i) + "=" +
// rs.getObject(i));
// System.out.println("");
myList.add(rs.getObject(i));
}
System.out.println(myList);
// round off to nearest million
for (int i1 = 0; i1 < myList.size(); i1++) {
// Print each Array from my list
myListdec = myList.get(i1);
Add_Log.info(myList.get(i1) + " round off to nearest million = " + ((BigDecimal) myListdec).divide(new BigDecimal("1000000")).setScale(0, RoundingMode.HALF_DOWN)+ "");
Reporter.log(myList.get(i1) + " round off to nearest million = " + ((BigDecimal) myListdec).divide(new BigDecimal("1000000")).setScale(0, RoundingMode.HALF_DOWN)+ "");
}
Upvotes: 0
Reputation: 316
You are printing the entire list to string each time. Use mylist.get(i) instead (you don't need toString() here).
Upvotes: 1