簡體   English   中英

在java方法中從數據庫返回數據數組

[英]Returning an array of data from database in a java method

我正在嘗試從數據庫中獲取值並將它們插入到數組中並返回,但它給出了一個錯誤:“無法在返回語句中找到變量str符號”。

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;
}
}

str在try塊中聲明。 您需要在該塊內移動返回。

作為旁注, System.out.println異常幾乎不是一個好主意

發生的事情是在try/catch語句中聲明的所有內容都可能不會發生 - 編譯器正試圖阻止這種情況。

例如,如果您有一個具有以下結構的方法(如您在另一條評論中所述):

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

您告訴編譯器您的方法將返回一個布爾值,但如果發生somethingExceptional()並拋出異常,則不會返回任何內容,因為將跳過其余的try!

所以你應該在try語句之前定義你的變量,給它一個默認值,然后 catch語句之后返回:

public boolean trueOrFalse()
{
    boolean result = false;

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

    return result;
}

這樣,您確保即使somethingExceptional()確實拋出,也會從該方法返回一些內容。

或者你可以改為向方法簽名添加一個throws並擺脫try/catch

你應該在bloc try之后聲明你的數組,並在bloc catch之后返回它。

嘗試這個

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;
    }
}

從底部尋找第二行,變量STR在哪里聲明? 在使用STR之前,請聲明並初始化它。 當你的聲明在Try / Catch塊中時,編譯器找不到它。 只需將Try / Catch塊中的聲明移出。

謝謝大家。我試過這個......工作:)

 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;
      }

  }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM