Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6197503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:49:58+00:00 2026-05-24T03:49:58+00:00

I know it may seem like an easy to solve problem, but I can’t

  • 0

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(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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:49:59+00:00Added an answer on May 24, 2026 at 3:49 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this may seem like a silly and tiny issue, but I can't
I know this may seem like a common question on SO but I could
I know this may seem like a math question but i just saw this
This may be a no-brainer for the WPF cognoscenti, but I'd like to know
This question may be too product specifc but I'd like to know if anyone
This may seem like a very simple question, but I have been struggling with
I know this may seem quite basic to geeks. But I want to make
This may not seem like a development question but there is a link. I
I'm learning Qt independently, and this may seem like an easy question (because it
Ok, this may seem like a dupe question, but if you bear with me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.