2011-04-27 15 views
0

我正在使用jdbc:sqlite來訪問sqlite數據庫並通過一個簡單的select語句獲取一些記錄。數據庫中有行,但java函數不返回任何內容。jdbc:sqlite不撤回數據

private static String ConnectionString = "jdbc:sqlite:D:\\My Documents\\NetBeansProjects\\IntelligentBusStop\\BusSystem.db"; 

private static ResultSet GetData(String command) 
{ 
    Connection connection = null; 
    Statement statement = null; 
    ResultSet resultSet = null; 
    try 
    { 
     Class.forName("org.sqlite.JDBC"); 
     connection = DriverManager.getConnection(ConnectionString); 
     statement = connection.createStatement(); 
     resultSet = statement.executeQuery(command); 
     return resultSet; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     return null; 
    } 
    finally 
    { 
     try 
     { 
      resultSet.close(); 
      statement.close(); 
      connection.close(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
public static ArrayList<StopBS> GetStopBS() 
{ 
    ResultSet results = GetData("Select StopNumber, Tag, Latitude, Longitude FROM StopTable "); 
    ArrayList<StopBS> stops = new ArrayList<StopBS>(); 
    try 
    { 
     while (results.next()) 
     { 
      StopBS newStop = new StopBS(); 
      newStop.StopNumber = results.getInt("StopNumber"); 
      newStop.Tag = results.getString("Tag"); 
      newStop.Lat = results.getFloat("Latitude"); 
      newStop.Long = results.getFloat("Longitude"); 
      stops.add(newStop); 
     } 
     return stops; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     return null ; 
    } 
} 

直接在數據庫上使用sql語句工作。我正在使用Razor sql查看分貝,如果這有什麼區別。

+0

你的while循環循環了多少次?把一個system.out.println或東西 – 2011-04-27 10:33:58

回答

1

你關閉你的ResultSetGetData,所以到GetStopBS試圖加載到數據結構光標不可用。

我會重寫這段代碼來正確地做到這一點。你很近,但不在那裏。

瞭解Sun編碼標準。您正在遵循.NET標準,而不是Java。

當你關閉語句,結果集和連接時,你的心是在正確的地方,但我不同意你把代碼放在哪裏。我建議不要將java.sql包引用從它們創建的方法範圍中傳出。創建它們,使用它們,並用相同的方法關閉它們。

您還應該在finally塊中關閉它們,並將其封閉在單個try/catch塊中。

+0

謝謝你是這個問題。 – Pinzel 2011-04-27 10:36:37