我正在尝试从数据库中获取值并将它们插入到数组中并返回,但它给出了一个错误:“在return语句中找不到变量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;
}
}
答案 0 :(得分:1)
str在try
块内声明。你需要在该区块内移动回报。
作为旁注,System.out.println
例外
答案 1 :(得分:1)
正在发生的事情是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
。
答案 2 :(得分:0)
你应该在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;
}
}
答案 3 :(得分:0)
从底部寻找第二行,variate STR声明在哪里? 在使用STR之前,请声明并初始化它。 当你的声明在Try / Catch块中时,编译器找不到它。 只需将Try / Catch块中的声明移出。
答案 4 :(得分:0)
谢谢大家。我试过这个......工作:)
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;
}
}
}