简体   繁体   English

如何在 Java 方法中返回一个数组

[英]How to return an array in Java method

I would like to create a method that returns a string array.我想创建一个返回字符串数组的方法。 I tried this code but it has problems with returning T_NazwaJadlospisu array.我尝试了此代码,但返回T_NazwaJadlospisu数组时出现问题。

public static String[] listJadlospis() {
    try {
        state = con.createStatement();
        result = state.executeQuery("SELECT NazwaJadlospisu FROM Jadlospis GROUP BY NazwaJadlospisu");
        while (result.next()) {
            Array z = result.getArray("NazwaJadlospisu");
            String[] T_NazwaJadlospisu = (String[]) z.getArray();

            // String T_NazwaJadlospisu =
            // result.getString("NazwaJadlospisu");
            // System.out.println("Jadłospis: " + T_NazwaJadlospisu);
        }
        return T_NazwaJadlospisu;
    } catch (SQLException e) {
        System.err.println("Query error.");
    } catch (NullPointerException e) {
        System.err.println("Element not found.");
    }
}

SQL returns as follows: SQL 返回如下:

mysql> SELECT NazwaJadlospisu FROM Jadlospis GROUP BY NazwaJadlospisu;
+-----------------+
| NazwaJadlospisu |
+-----------------+
| dieta 1500 kcal |
| dieta testowa1  |
| test1           |
+-----------------+
3 rows in set (0.00 sec)

Any hints on what I do wrong and what would be the best way to return SELECT SQL statement output from a method.关于我做错了什么以及从方法返回 SELECT SQL 语句 output 的最佳方法的任何提示。 Thanks in advance for helping.提前感谢您的帮助。


OK, I have followed most of yours pointers and went a bit further.好的,我遵循了您的大部分指示并走得更远。 Now I got an error message like this: java.sql.SQLFeatureNotSupportedException现在我收到这样的错误消息: java.sql.SQLFeatureNotSupportedException

In general, what I would like to do is to have a method listJadlospis returning a String array with all 3 rows from NazwaJadlospisu column.一般来说,我想做的是让方法 listJadlospis 返回一个字符串数组,其中包含来自 NazwaJadlospisu 列的所有 3 行。

Now, the source looks like this:现在,源代码如下所示:

public static String[] listJadlospis(){
    String[] nazwajadlospisu = null;
    try{
        state = con.createStatement();
        result = state.executeQuery("SELECT NazwaJadlospisu FROM Jadlospis GROUP BY NazwaJadlospisu");
        while(result.next()){
            Array z = result.getArray("NazwaJadlospisu");
            nazwajadlospisu = (String[])z.getArray();
            }
        }
    catch(SQLException e){
        System.err.println("Query error. " +e);
        }
    catch(NullPointerException e){
        System.err.println("Element not found." +e);
        }

    return nazwajadlospisu;
    }

Any hints on how to implement this?关于如何实现这一点的任何提示?

You can try like this.你可以这样试试。 You have to declare the array type which should be returned.您必须声明应该返回的数组类型。 This variable String[] T_NazwaJadlospisu has to be declared.这个变量String[] T_NazwaJadlospisu必须被声明。 Besides you have to return the array outside of try catch block.此外,您必须在 try catch 块之外返回数组。 I provide an example here.我在这里提供一个例子。

public <ReturnType> functionName() {
    <ReturnType> <someName>;

    <try-catch>

    return <someName>;
}

The modified code is given below.修改后的代码如下。

public static String[] listJadlospis() {
    String[] T_NazwaJadlospisu = null; // DECLARE
    try {
        state = con.createStatement();
        result = state.executeQuery("SELECT NazwaJadlospisu FROM Jadlospis GROUP BY NazwaJadlospisu");
        while (result.next()) {
            Array z = result.getArray("NazwaJadlospisu");
            T_NazwaJadlospisu = (String[]) z.getArray(); // ASSIGN

            //String T_NazwaJadlospisu =
            //result.getString("NazwaJadlospisu");
            //System.out.println("Jadłospis: " + T_NazwaJadlospisu);
        }

    } catch (SQLException e) {
        System.err.println("Query error.");
    } catch (NullPointerException e) {
        System.err.println("Element not found.");
    }
    return T_NazwaJadlospisu; // RETURN
}

Is that code that you need?是你需要的代码吗?

 public static String[] listJadlospis() {
        try {
            state = con.createStatement();
            result = state.executeQuery("SELECT NazwaJadlospisu FROM Jadlospis GROUP BY NazwaJadlospisu");

            List<String> nazwaJadlospisu= new ArrayList<String>();
            String[] T_NazwaJadlospisu = null;
            while (result.next()) {
                nazwaJadlospisu.add(result.getString(1))

                // String T_NazwaJadlospisu =
                // result.getString("NazwaJadlospisu");
                // System.out.println("Jadłospis: " + T_NazwaJadlospisu);
            }
            T_NazwaJadlospisu = new String[nazwaJadlospisu.size()];
            T_NazwaJadlospisu = nazwaJadlospisu.toArray(T_NazwaJadlospisu );
            return T_NazwaJadlospisu;
        } catch (SQLException e) {
            System.err.println("Query error.");
        } catch (NullPointerException e) {
            System.err.println("Element not found.");
        }
    }

In your code T_NazwaJadlospisu is declared inside the while block.在您的代码T_NazwaJadlospisu在 while 块内声明。 So it's scope is limited to that block only.所以它的 scope 仅限于该块。

So there can be two solutions to that problem:所以这个问题可以有两种解决方案:

1) return it from the while loop. 1)从while循环中返回它。 This this will return the first occurrence from the loop.这将返回循环中的第一次出现。

2) Declare it outside the loop and return it after the loop. 2)在循环外声明它并在循环后返回它。 And this will return the last occurrence from the loop.这将返回循环中的最后一次出现。

So depending on your business scenario, you can use the solution accordingly.因此,根据您的业务场景,您可以相应地使用该解决方案。

Returning First Occurrence:返回第一次出现:

public static String[] listJadlospis() {

    try {
        state = con.createStatement();
        result = state.executeQuery("SELECT NazwaJadlospisu FROM Jadlospis GROUP BY NazwaJadlospisu");
        while (result.next()) {
            Array z = result.getArray("NazwaJadlospisu");
            String[] T_NazwaJadlospisu = (String[]) z.getArray();

            // String T_NazwaJadlospisu =
            // result.getString("NazwaJadlospisu");
            // System.out.println("Jadłospis: " + T_NazwaJadlospisu);
            return T_NazwaJadlospisu;
        }
    } catch (SQLException e) {
        System.err.println("Query error.");
    } catch (NullPointerException e) {
        System.err.println("Element not found.");
    }
    return null;
}

Returning last occurrence:返回最后一次出现:

public static String[] listJadlospis() {

        try {
            state = con.createStatement();
            result = state.executeQuery("SELECT NazwaJadlospisu FROM Jadlospis GROUP BY NazwaJadlospisu");
            String[] T_NazwaJadlospisu = null;
            while (result.next()) {
                Array z = result.getArray("NazwaJadlospisu");
                T_NazwaJadlospisu = (String[]) z.getArray();

                // String T_NazwaJadlospisu =
                // result.getString("NazwaJadlospisu");
                // System.out.println("Jadłospis: " + T_NazwaJadlospisu);
            }
            return T_NazwaJadlospisu;
        } catch (SQLException e) {
            System.err.println("Query error.");
        } catch (NullPointerException e) {
            System.err.println("Element not found.");
        }
        return null;
    }

Few other observations:其他一些观察:

1) Variables names are not as per Java standards. 1) 变量名称不符合 Java 标准。

2) Exception handling should do something meaningful rather than only printing it. 2)异常处理应该做一些有意义的事情,而不是仅仅打印它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM