我知道這看起來像是一個容易解決的問題,但我在這段代碼中找不到我的錯。我返回int,Eclipse告訴我'這個方法必須返回int類型的結果'。Java:返回值的問題
public static int getLastId(int table) {
Connection connection = null;
String url = "jdbc:postgresql://someServer:port/someDB";
try {
//Verbindung herstellen
connection = DriverManager.getConnection(url, "someUser",
"somePassword");
} catch (SQLException e1) {
//fehlerhafte Verbindung
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
//0 - source ; 1 - destination Table
if(table == 0){
Statement stmt = connection.createStatement();
ResultSet lastId;
lastId = stmt.executeQuery("SELECT index FROM table0 ORDER BY someIndex DESC LIMIT 1");
String theLastId0 = "";
while(lastId.next())
{
//System.out.print(lastId.getString("index"));
theLastId0 = lastId.getString("index");
}
lastId.close();
stmt.close();
connection.close();
int letzteId0 = Integer.parseInt(theLastId0);
return letzteId0;
}else if(table == 1){
Statement stmt = connection.createStatement();
ResultSet lastId;
lastId = stmt.executeQuery("SELECT index FROM table1 ORDER BY someIndexDESC LIMIT 1");
String theLastId1 = "";
while(lastId.next())
{
//System.out.print(lastId.getString("index"));
theLastId1 = lastId.getString("index");
}
lastId.close();
stmt.close();
connection.close();
int letzteId1 = Integer.parseInt(theLastId1);
return letzteId1;
}
}
catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return -1;
}
}
非常感謝這是問題的最佳答案。即使我下班回家,我也不會想到的一件事是^^。有時你需要問一個問題,看看有時候會有多愚蠢。乾杯。 – 1amtoo1337