0

toDoList の作成を開始しました。「DataMapper」を作成して、データベースへのクエリを実行したいと考えています。

この Datamapper を作成して処理を代行しましたが、この場合、私の考え方が正しいかどうかはわかりません。私の Datamapper では、クエリを実行する必要があるメソッドを 1 つだけ作成し、(open メソッドと close メソッドを最小限に抑えるために) どのクエリを起動するかを知っているいくつかのメソッドを作成しました。

たとえば、私はこれを持っています:

public Object insertItem(String value) {

    this.value = value;

    String insertQuery = "INSERT INTO toDoList(item,datum) " + "VALUES ('" + value + "', CURDATE())";

    return this.executeQuery(insertQuery);
}

public Object removeItem(int id) {

    this.itemId = id;
    String deleteQuery = "DELETE FROM test WHERE id ='" + itemId + "'";

    return this.executeQuery(deleteQuery);

}

private ResultSet executeQuery(String query) {

    this.query = query;
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    try {

        con = db.connectToAndQueryDatabase(database, user, password);

        st = con.createStatement();
        st.executeUpdate(query);
    } 
    catch (SQLException e1) {
        e1.printStackTrace();
    }
    finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e2) { /* ignored */}
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e2) { /* ignored */}
            }                       
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e2) { /* ignored */}
            }
            System.out.println("connection closed");
     }
    return rs;
}

したがって、このように a を返すことが正しいかどうかはわかりませんResultSet。私は次のようなことをすることにしました

public ArrayList<ToDoListModel> getModel() {    
    return null;
}

で返されたすべてのレコードを挿入するには、ArrayList. でもちょっと引っかかる感じ。誰かが例か何かで私を正しい方法に導くことができますか?

4

3 に答える 3

0

It depends on the way the application works. If you have a lot of databases hits in a short time it would be better to bundle them and use the same database connection for all querys to reduce the overhead of the connection establishment and cleaning.

If you only have single querys in lager intervals you could do it this way.

You should also consider if you want to seperate the database layer and the user interface (if existing). In this case you should not pass the ResultSet up to the user interface but wrap the data in an independent container and pass this through your application.

于 2013-04-19T09:02:24.907 に答える
0

If I understand your problem correctly!, you need to pass a list of ToDoListModel objects to insert into the DB using the insertItem method.

How you pass your object to insert items does not actually matter, but what you need to consider is how concurrent this DataMapper works, if it can be accessed by multiple threads at a time, you will end up creating multiple db connections which is little expensive.Your code actually works without any issue in sequential access.

So you can add a synchronized block to connection creation and make DataMapper class singleton.

于 2013-04-19T09:02:51.267 に答える