0

I'm trying to get the values from a database and insert them into an array and return, but it gives me an error: "cannot find symbol for variable str in return statement".

public class getDates {
public static Date[] Dates(){

   Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "GreetingCard";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "";
    try{
      Class.forName(driver);
      con = DriverManager.getConnection(url+db, user, pass);
      Statement st = con.createStatement();
      ResultSet rs=st.executeQuery("select date from profile");
      ResultSetMetaData metadata = rs.getMetaData();
      int columnCount = metadata.getColumnCount();
      Date[] str = new Date[columnCount];
      int a=0;
    //getting the dates from database to an array

      while(rs.next()){

      str[a++]=rs.getDate("date");
    }

    }
     catch(Exception e){
         System.out.println(e);
      }
     //returning the array str

    return str;
}
}
2
  • think about what the method should return when an expedition is thrown and caught. An empty array? Null? should it throw a new exception? Commented Mar 8, 2014 at 7:40
  • tried putting return statement in try block and return null in catch block.it worked.Thankss!! :) Commented Mar 8, 2014 at 8:10

5 Answers 5

1

str is declared inside your try block. You need to move the return inside that block.

As a side note, it is almost never a good idea to System.out.println an exception

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your quick reply.i have tried putting the return statement in the try block but then i get an error missing return statement in the method
I guess I assumed too much ;) remove the try - catch entirely, and let your method throw (the correct) Exceptions, and you should be good
It should work as well.tried putting return statement in try block and return null in catch block.it worked.Thanks :)
1

What's happening is that everything declared within try/catch statements can potentially not happen—the compiler is trying to prevent this.

For example, if you have a method with the following structure (as you said in another comment):

public boolean trueOrFalse()
{
    try
    {
        somethingExceptional();
        boolean result = true;
        return result;
    }
    catch(Exception e)
    {
        doSomething(e);
    }
}

You are telling the compiler that your method will return a boolean, but if somethingExceptional() happens, and an exception is thrown, nothing will be returned because the rest of the try will be skipped!

So instead you should define your variable before the try statement, give it a default value, and return after the catch statement:

public boolean trueOrFalse()
{
    boolean result = false;

    try
    {
        somethingExceptional();
        result = true;
    }
    catch(Exception e)
    {
        doSomething(e);
    }

    return result;
}

This way, you ensure that even if somethingExceptional() does happen to throw, something will be returned from the method.

Or you can instead add a throws to the method signature and get rid of the try/catch.

1 Comment

Since array is initialized inside the while loop i get the error in return statement "variable str might not have been initialized".I tried putting "return str" in try block and "return null" in catch block.it worked :)
0

Seek the second line from the bottom ,where does the variate STR declare? Before use STR,declare and initialize it. While your declaring is in the Try/Catch block , the compiler couldn't find it. Just move the declaring out of the Try/Catch block.

Comments

0

You should declare you array after bloc try and return it after bloc catch.

Try this

public class getDates {
    public static Date[] Dates(){

       Connection con = null;
        String url = "jdbc:mysql://localhost:3306/";
        String db = "GreetingCard";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "";
        // STR declaration outside try catch blocs
        Date[] str = new Date[10 /*any value > 0 */];
        try{
          Class.forName(driver);
          con = DriverManager.getConnection(url+db, user, pass);
          Statement st = con.createStatement();
          ResultSet rs=st.executeQuery("select date from profile");
          ResultSetMetaData metadata = rs.getMetaData();
          int columnCount = metadata.getColumnCount();
          // STR instantiation 
          str = new Date[columnCount];
          int a=0;
          //getting the dates from database to an array
          while(rs.next()){
            str[a++]=rs.getDate("date");
          }

        }catch(Exception e){
          System.out.println(e);
        }
         //returning the array str
        return str;
    }
}

4 Comments

then i get the error "cannot find symbol" for str in return statement and also in str[a++]=rs.getDate("date")
Try this code, I think that it will work. @user2700115
get the error in return statement "variable str might not have been initialized".I tried putting "return str" in try block and "return null" in catch block.it worked :)
You can also initialize the str array with any length the reinitialize it with columnCount.
0

Thanks all.I tried this..It worked :)

 public class getDates {
  public static Date[] Dates(){

   Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "GreetingCard";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "";
    try{
      Class.forName(driver);
      con = DriverManager.getConnection(url+db, user, pass);
      Statement st = con.createStatement();
      ResultSet rs=st.executeQuery("select date from profile");
      ResultSetMetaData metadata = rs.getMetaData();
      int columnCount = metadata.getColumnCount();
      Date[] str = new Date[columnCount];
      int a=0;
      while(rs.next()){

      str[a++]=rs.getDate("date");
    }

      return str;
    }
     catch(Exception e){
         System.out.println(e);
         return null;
      }

  }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.