質問

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ステートメントを直接使用します。私はRazor SQLを使用してDBを表示しています。

役に立ちましたか?

解決

あなたはあなたを閉じます ResultSetGetData, 、だから時代 GetStopBS データ構造にロードしようとします。カーソルは使用できません。

このコードを書き直して適切に行います。あなたは近いですが、そこにはそうではありません。

サンコーディング基準を学びます。 Javaではなく、.NET標準をフォローしています。

あなたがあなたの声明、結果セット、および接続を閉じるとき、あなたの心は正しい場所にありますが、私はあなたがコードを置く場所に同意しません。 Java.sqlパッケージの参照を作成したことをお勧めします。それらを作成し、それらを使用し、同じ方法で閉じます。

また、最終的なブロックでそれらを閉じて、個々のトライ/キャッチブロックで閉鎖を包みます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top