我正在使用JDBC:SQLITE访问SQLite DB,并使用简单的选择语句获取一些记录。数据库中有行,但是Java函数什么也没返回。

   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语句工作。如果有任何区别,我正在使用剃须刀SQL查看DB。

有帮助吗?

解决方案

你关闭你的 ResultSetGetData, ,所以到了 GetStopBS 试图将其加载到数据结构中。

我会重写此代码以正确执行此操作。你很近,但不在那里。

学习太阳编码标准。您正在遵循.NET标准,而不是Java。

当您关闭语句,结果集和连接时,您的心在正确的位置,但是我不同意您的位置。我建议不要从创建它们的方法范围中传递Java.sql软件包引用。创建它们,使用它们,并以相同的方法关闭它们。

您还应该将它们结束在最后一个块中,将近距离包裹在单独的尝试/捕获块中。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top