返回类型是一个String数组。我返回了该数组,但仍然显示错误消息。
public static String[] get_dept_list()
{
establishConnection();
try
{
stmt = conn.createStatement();
sql = "select * from DEPARTMENT_LIST";
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Query Ran");
int in=-1;
String name [] = {};
while(rs.next())
{
in++;
}
rs = stmt.executeQuery(sql);
name = new String [in+1];
in=-1;
while(rs.next())
{
name[++in] = rs.getString("DEPT_NAME");
}
return name;
}
catch (SQLException e)
{
System.out.println("Problem in finding the dept_name_list");
}
}
最佳答案
当异常被捕获时,你必须确保也返回一个数组。
catch (SQLException e)
{
System.out.println("Problem in finding the dept_name_list");
return new String[] { };
}
关于java - 我在此代码中返回一个字符串数组,但它仍然表示此方法必须返回 String 类型的结果。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863429/