【发布时间】:2015-08-29 04:26:17
【问题描述】:
我正在尝试返回一个变量 String authServer,但我似乎做不到。
public static String getAuth () {
Connection connection = null;
try {
connection = ConnectionConfig.getConnection();
if (connection != null) {
Statement query = connection.createStatement();
ResultSet rs = query.executeQuery("SELECT auth FROM auth");
while (rs.next()) {
String authServer = rs.getString("auth");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return authServer;
}
}
上面的代码给了我一个未知符号“authServer”的错误。
我做错了什么?
【问题讨论】:
-
您已经在 while 循环的上下文中声明了 authServer,使得该方法的其余部分无法访问它
-
好的,谢谢!我现在试试