| Author |
Error states "This method must return a result of type List"
|
Geri Calderon
Greenhorn
Joined: Apr 10, 2008
Posts: 9
|
|
I am very green and need a bit of help. Here is the code: public VehSearch() { } public List getYear(List carYr){ List yearList = new ArrayList(); Connection connection = null; Statement stmt = null; try { Class.forName(JDBC_DRIVER); Connection db2Conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement st = db2Conn.createStatement(); ResultSet rs = st.executeQuery("SELECT DISTINCT YEAR FROM VEHICLE"); while(rs.next()) { String carYear = rs.getString(1); System.out.println(carYear); } rs.close(); st.close(); db2Conn.close(); } I am not sure where to put the return or what to put in it for that matter. Any direction would be great!
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
"Golf Queen," Welcome to JavaRanch! Please check your private messages by clicking on My Private Messages. Thanks!
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Your getYear method declares that it returns a List, so all normal paths of execution within the method body must end with a statement that returns a List. It looks like your method is just printing years to the console. If that's all you want to do, then there's no need to return anything, and your method can declare a return type of "void" instead of "List." But if you do want the method to return a List, then you should be adding these years to your List (yearList) as you get them from the ResultSet, and then return yearList when you're done.
|
 |
Silpa Sunkavalli
Greenhorn
Joined: Jun 04, 2008
Posts: 3
|
|
public VehSearch() { } public List getYear(List carYr){ List yearList = new ArrayList(); Connection connection = null; Statement stmt = null; try { Class.forName(JDBC_DRIVER); Connection db2Conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement st = db2Conn.createStatement(); ResultSet rs = st.executeQuery("SELECT DISTINCT YEAR FROM VEHICLE"); while(rs.next()) { String carYear = rs.getString(1); //Add the carYear element to the list variable yearList.add(carYear); System.out.println(carYear); } rs.close(); st.close(); db2Conn.close(); //Here you have to return the list return yearList; }
|
 |
Geri Calderon
Greenhorn
Joined: Apr 10, 2008
Posts: 9
|
|
Thank you both for your input. I made the changes and am still getting the error: I must be missing something in the translation... [edit]Add code tags and change indentation. CR[/edit] [ September 09, 2008: Message edited by: Campbell Ritchie ]
|
 |
Norm Radder
Ranch Hand
Joined: Aug 10, 2005
Posts: 681
|
|
|
Can you post the full text of the error message?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
|
And please always use the code button when posting code, and make sure the indentation is right. I have edited your last post and you can see how much easier it is to read. You haven't posted the entire method; is there anything after what you have posted other than a catch block?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
|
And what are you doing with the carYr parameter? It doesn't appear to be used in that method, in which case you ought to lose the parameter.
|
 |
Geri Calderon
Greenhorn
Joined: Apr 10, 2008
Posts: 9
|
|
Here's the enter code (with company info blocked out) [ September 09, 2008: Message edited by: Geri Calderon ]
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by Geri Calderon: Here's the catch I'm trying to get the data from the data base and the yearList goes into a drop down box.
So if an ClassNotFoundException or SQLException exception is thrown, what gets returned from the method ? [ September 09, 2008: Message edited by: Joanne Neal ]
|
Joanne
|
 |
Geri Calderon
Greenhorn
Joined: Apr 10, 2008
Posts: 9
|
|
Thanks for everyone's help. found the problem. after the last catch:
|
 |
 |
|
|
subject: Error states "This method must return a result of type List"
|
|
|