在java方法中从数据库返回数据数组
我试图从数据库中获取值并将其插入数组并返回,但它给了我一个错误:“在return语句中找不到变量在java方法中从数据库返回数据数组,java,arrays,database,methods,Java,Arrays,Database,Methods,我试图从数据库中获取值并将其插入数组并返回,但它给了我一个错误:“在return语句中找不到变量str的符号” str在try块中声明。您需要将返回移动到该块内 作为旁注,几乎从来都不是一个好主意System.out.println异常您应该在bloctry之后声明数组,并在bloccatch之后返回它 试试这个 public class getDates { public static Date[] Dates(){ Connection con = null;
str
的符号”
str在
try
块中声明。您需要将返回移动到该块内
作为旁注,几乎从来都不是一个好主意
System.out.println
异常您应该在bloctry
之后声明数组,并在bloccatch
之后返回它
试试这个
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;
}
}
发生的情况是,在
try/catch
语句中声明的所有内容都可能不会发生,编译器正试图阻止这种情况发生
例如,如果您有一个具有以下结构的方法(如您在另一条评论中所述):
您告诉编译器您的方法将返回一个布尔值,但是如果发生someThingException()
,并且抛出异常,则不会返回任何内容,因为剩余的尝试将被跳过
因此,您应该在try语句之前定义变量,给它一个默认值,然后在catch语句之后返回:
public boolean trueOrFalse()
{
boolean result = false;
try
{
somethingExceptional();
result = true;
}
catch(Exception e)
{
doSomething(e);
}
return result;
}
通过这种方式,您可以确保即使发生了抛出SomethingException()
,也会从该方法返回一些内容
或者,您可以改为向方法签名添加一个
throws
,并去掉try/catch
从底部查找第二行,变量STR声明在哪里?
在使用STR之前,声明并初始化它。
当您的声明位于Try/Catch块中时,编译器找不到它。
只需将声明从Try/Catch块中移出。谢谢大家。我尝试了这个……它成功了:)
感谢您的快速回复。我已尝试将return语句放入try块中,但随后在method中出现了一个错误缺少return语句我想我假设的太多了;)完全删除try-catch,让您的方法抛出(正确的)异常,您应该很好。它应该也可以工作。尝试将return语句放在try块中,并在catch块中返回null。它成功了。谢谢:)请考虑在抛出和捕获探测时该方法应该返回什么。空数组?无效的它是否应该抛出新异常?是否有效@User2700115尝试将return语句放在try块中,并在catch块中返回null。成功了。谢谢!!:)然后我在return语句中以及str[a++]=rs.getDate(“date”)中得到str的错误“找不到符号”,请尝试这段代码,我认为它可以工作@user2700115get返回语句“variable str可能尚未初始化”中的错误。我尝试将“return str”放在try块和“return null”中在catch block.it worked:)中,您还可以使用任何长度初始化str数组,然后使用columnCount重新初始化。由于数组是在while循环中初始化的,因此我在返回语句“variable str可能尚未初始化”中得到错误。我尝试将“return str”放在try block中,将“return null”放在catch block中。it worked:)
public boolean trueOrFalse()
{
try
{
somethingExceptional();
boolean result = true;
return result;
}
catch(Exception e)
{
doSomething(e);
}
}
public boolean trueOrFalse()
{
boolean result = false;
try
{
somethingExceptional();
result = true;
}
catch(Exception e)
{
doSomething(e);
}
return result;
}
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;
}
}
}