Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In my situation, I am querying a database for a specific return (in this case registration information based on a username).

            //Build SQL String and Query Database.
        if(formValid){
            try {
                SQL = "SELECT * FROM users WHERE username=? AND email=?";
                Collections.addAll(fields, username, email);
                results = services.DataService.getData(SQL, fields);
                if (!results.next()){
                    errMessages.add("User account not found.");
                } else {
                    user = new User();
                    user.fillUser(results); //Is it ok to pass ResultSet Around?
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                services.DataService.closeDataObjects(); //Does this close the ResultSet I passed to fillUser?
            }
        }

So once I query the database, if a result is found I create a new User object and populate it with the data I received from the database. I used to do all of this directly in the method that I was pulling the resultset into, but I realized I was doing a lot of redundant coding throughout my project so I moved it all into one central method that lives in the actual User bean.

    public void fillUser(ResultSet data) throws SQLException{
    setUserId(data.getInt("id"));
    setFirstName(data.getString("first_name"));
    setLastName(data.getString("last_name"));
    setUsername(data.getString("username"));
    setType(data.getString("type"));
    setEmail(data.getString("email"));
}

I have done a few tests and from what I can determine, because I close the original resultset in the finally block of the query, the resultset that I pass into the fillUser method also gets closed. Or am I wrong and am I seriously leaking data? This is actually the second time I pass a resultset (so its two instances of one) because the block I use to query my database is

    public static ResultSet getData(String SQL, ArrayList fields) throws SQLException {
    try{
        connection = Database.getConnection();
        preparedStatement = connection.prepareStatement(SQL);

        for(int i=0; i<fields.size(); i++){
            Integer num = i + 1;
            Object item = fields.get(i);

            if(item instanceof String){
                preparedStatement.setString(num, (String) item); //Array item is String.
            } else if (item instanceof Integer){
                preparedStatement.setInt(num, (Integer) item); //Array item is Integer.
            }
        }

        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }finally{

    }
}

All of these code snippets live in separate classes and are reused multiple times throughout my project. Is it ok to pass a resultset around like this, or should I be attempting another method? My goal is to reduce the codes redundancy, but i'm not sure if i'm going about it in a legal manner.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
88 views
Welcome To Ask or Share your Answers For Others

1 Answer

Technically, it's OK to pass result sets, as long as you are not serializing and passing it to a different JVM, and your JDBC connection and statement are still open.

However, it's probably a better software engineer and programming practice to have DB access layer that returns you the result set in a Java encoded way (a list of User in your example). That way, your code would be cleaner and you won't have to worry if the ResultSet is already opened, or you have to scroll it to the top, you name it...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...