开发者

Java: Problem with return value

I know it may seem like an easy to solve problem, but I can't find my fault in this piece of code. I'm returning int and Eclipse is telling me that 'This method must return a result of type int'.

public static int getLastId(int table) {

    Connection connection = null;
    String url = "jdbc:postgresql://someServer:port/someDB";

    try {
        //Verbindung herstellen
        connection = DriverManager.getConnection(url, "someUser",
                "somePassword");
        } catch (SQLException e1) {
        //fehlerhafte Verbindung
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        //0 - source ; 1 - destination Table
        if(table == 0){

        Statement stmt = connection.createStatement();
        ResultSet lastId;
        lastId = stmt.executeQuery("SELECT index FROM table0 ORDER BY someIndex DESC LIMIT 1");
        String theLastId0 = "";
         while(lastId.next())
          {

              //System.out.print(lastId.getString("index"));
              theLastId0 = lastId.getString("index");

          }
          lastId.close();
          stmt.close();
          connection.close();
          int letzteId0 = Integer.parseInt(theLastId0);

        return letzteId0;

        }else if(table == 1){

            Statement stmt = connection.createStatement();
            ResultSet lastId;
            lastId = stmt.executeQuery("SELECT index FROM table1 ORDER BY someIndexDESC LIMIT 1");
            String theLastId1 = "";
              while(开发者_Python百科lastId.next())
              {

                  //System.out.print(lastId.getString("index"));
                  theLastId1 = lastId.getString("index");

              }
              lastId.close();
              stmt.close();
              connection.close();
              int letzteId1 = Integer.parseInt(theLastId1);

            return letzteId1;
            }

    } 
    catch (SQLException e) {

        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return -1;
    }
}


What happens if table != 0 and table != 1? Then your method doesn't return anything. So either add an else statement to your if or just a regular return, returning a dummy value like -1.

Even if you, the programmer, know that this case will never be executed, the compiler doesn't know that so you gotta make it happy anyways. Plus nasty things can be done using reflection so it's never a good idea to assume the input to your methods are valid.


This method must Always return an int.

You must add the following else statement

else {
return 0;
}

after the else if because in your version you don't return anything if your if AND your else if evaluates to false.


If table != 0 or 1, then your code does not return anything. Just add return 0; at the very end or whatever the appropriate behavior is, then you should be fine.

For future reference, your methods must return a value in every condition if a return type is specified. If there is a situation where nothing is returned, then the code is faulty. Hope it helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜