Reputation: 13
Im quite new to database connection i want to ask a question regarding java. how do i print the array list in this code ?
public String[] getList(String listname){
try{
ResultSet rs;
rs=sta.executeQuery("Select * from book;");
rs.next();
int count = Integer.parseInt(rs.getString(1));
rs=sta.executeQuery("Select * from book;");
String[] tobeout = new String[count];
for(int x=0;rs.next();x++){
tobeout[x]=rs.getString(1);
}
return tobeout;
}catch(SQLException e){e.printStackTrace();return null;}
}
what to do i write in my tester class so i can print the array ( after the query ) .can anyone please help me . thank you
note : MyListBackEnd is the name of the class which contains the query"getList" method. here is what i added so far in my tester class
public static void main(String[] args) {
MyListBackEnd en = new MyListBackEnd();
en.getList("book");
System.out.println(en.tobeout[1]);
en.close();
}
}
but it has a error on the System.out.print.
Upvotes: 0
Views: 74
Reputation: 48404
If you mean to simply print out your tobeout
String[]
once populated, you can use the java.util.Arrays
' static method:
System.out.println(Arrays.toString(tobeout));
Upvotes: 1
Reputation: 3323
You should change your main method like this:
String[] bookList = en.getList("book");
for (int i=0; i<bookList.length; i++){
System.out.println(bookList[i]);
}
You have made two mistakes:
1st mistake: en.getList("book");
You haven't save list which method getList("book")
returned.
2nd mistake: If you want print array you mast iterate through all array elements, like this:
for (int i=0; i < ARRAY.length; i++){
Upvotes: 0
Reputation: 3684
Simply modifying loop.
for(int x=0;rs.next();x++){
tobeout[x]=rs.getString(1);
System.out.println(tobeout[x]);
}
Upvotes: 0