ResultSet and String array

C

cleanair4me

In Tomcat 6.0.20 I am attempting to create an Oracle 9i resultset that
outputs as a String array and need help creating the method.

The CityData method will be called in the CityServlet:
String [] cityArray = cityData.getCityList();


Please help me with my attempt in the CityData class. Here is what I
have so far and not sure how to get a String array resultset.
public ArrayList<String> getCityList()
{
try {
//resultset, stmt and sql here....
//................
ArrayList<String> citys = new ArrayList<String>();
while(rs.next())
{
citys.add(rs.getString("city"));
}
String [] cityArray = citys.toArray(new
String[citys.size()]);
}
return cityArray;
}
 
J

Jean-Baptiste Nizet

cleanair4me a écrit :
In Tomcat 6.0.20 I am attempting to create an Oracle 9i resultset that
outputs as a String array and need help creating the method.

The CityData method will be called in the CityServlet:
String [] cityArray = cityData.getCityList();


Please help me with my attempt in the CityData class. Here is what I
have so far and not sure how to get a String array resultset.

What happens when you try compiling the class containing this snippet?
Have you tried? Do you have error messages? What do they say? Have you
tried to understand them?

I'll give you some hints below.
public ArrayList<String> getCityList()
What do you want to return from your method? An array of Strings
(String[]), or a list of Strings (List<String>). The two types are not
the same, and are not interchangeable. You should almost never return an
ArrayList<String>. The caller doesn't have to know that the method uses
the ArrayList kind of List. Knowing that it returns a List<String> is
sufficient, and you might want, in the future, to change the
{
try {
//resultset, stmt and sql here....
//................

What is the code hidden behind these comments? Does it compile? Do you
master it?
ArrayList<String> citys = new ArrayList<String>();

You should declare the variable this way:

List<String> cities = new ArrayList<String>();

Once again, use the interface type rather than the concrete type. And
use correct English: your code will be easier to uderstand and maintain.
while(rs.next())
{
citys.add(rs.getString("city"));
}
String [] cityArray = citys.toArray(new
String[citys.size()]);

If you want to return a List<String>, why do you transform the list into
an array? It's not useful.
If you want to return an array, the variable pointing to the array
should be in the same scope (or a larger scope) than the return
statement. So you might want to either place the return statement inside
the try block, or declare the cityArray variable outside the try block.

You have a try block, but without any catch or finally block. This is
not valid in Java.
return cityArray;
}

If you really want helpful answers, provide all the code, tell what you
have tried and what you have got.
But honestly, given the code you have shown us, I think you should try
to master the Java syntax with small and simple exercises before trying
to use APIs as complex as the generic collections framework and JDBC.

JB.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top