Prev: ArrayList issue
Next: JVM doing many Full GC and Unloading class sun.reflect.GeneratedMethodAccessor
From: cleanair4me on 22 Feb 2010 17:55 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; }
From: Jean-Baptiste Nizet on 22 Feb 2010 18:21
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 implementation to LinkedList<String> for example without affecting the caller. > { > 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. |