I have problem with return statment >.< I want to store all magazine names into
ArrayList<String> ListNameMagazine = new ArrayList<String>();
I have a DB; in the DB there is a table name_magazine and the data in name_magazine is
Magazine1
Magazine2
Magazine3
Magazine4
This my main:
ShowData Show = new ShowData(); int HowManyMagazine = Show.HowManyMagazine(1); // to make sure there is how many Magazine name in my database //System.out.print(HowManyMagazine); //i want to make sure the data is out. String nmeMagazine = null; // this variable for get data from return statement // i want to store in ListNameMagazine ArrayList<String> ListNameMagazine = new ArrayList<String>(); for (int numbeer = 0;numbeer <= HowManyMagazine ; numbeer++) { //Store in 1 variable String, because if arrayList it's error nmeMagazine = Show.getResult("Select Name_Magazine from Magazine"); // Store again in array list ListNameMagazine.add(nmeMagazine); } for (String s : ListNameMagazine) { System.out.println(s); // show the data }
This is my return statement:
public String getResult(String sql) throws SQLException { ResultSet rs = st.executeQuery(sql); ResultSetMetaData resultsetmetadata = rs.getMetaData(); //String just_try = null; while (rs.next()) { System.out.println("Result:"+rs.getString(1)); //just_try = rs.getString(1); //return just_try; } return null; //return just_try; }
The problem is in return statement.
When the comment ( // ) I erase and the last return null; I delete. It become like here:
public String getResult(String sql) throws SQLException { ResultSet rs = st.executeQuery(sql); ResultSetMetaData resultsetmetadata = rs.getMetaData(); String just_try = null; while (rs.next()) { //System.out.println("Result:"+rs.getString(1)); just_try = rs.getString(1); return just_try; } return just_try; }
When I show the data using this statement.
for (String s : ListNameMagazine) { System.out.println(s); // show the data }
the result only
Magazine4
Magazine4
Magazine4
Magazine4
@.@ I have confuse where the miss @.@
but when I show data in return statement like this
public String getResult(String sql) throws SQLException { ResultSet rs = st.executeQuery(sql); ResultSetMetaData resultsetmetadata = rs.getMetaData(); String just_try = null; while (rs.next()) { System.out.println("Result:"+rs.getString(1)); //just_try = rs.getString(1); //return just_try; } return null; }
The data show what I want. I know I only miss in somewhere but I don’t know where that @.@. I hope you guys can found it .THX
Your problem is that return returns only one thing, and it will return immediately and the function will exit! You are retuning the name of a magazine just_try.
So, you start iterating through rs, and you get the name:
And then you tell the code to return just_try.
At this point just_try will be returned and the function will exit! I think your problem is that you are expecting the function to keep going, and to keep returning values to the code that calls it, but this is not the way it works.
I suspect what you want to do is something like this:
then in the function getResult: