传递ResultSet是否可以? [英] Is it Ok to Pass ResultSet?

查看:113
本文介绍了传递ResultSet是否可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的情况下,我正在查询数据库以获取特定的回报(在这种情况下,基于用户名的注册信息)。

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?
            }
        }

因此,一旦我查询数据库,结果是发现我创建了一个新的User对象,并用我从数据库收到的数据填充它。我曾经在我将结果集拉入的方法中直接执行所有这些操作,但我意识到我在整个项目中都进行了大量的冗余编码,所以我把它全部移到了一个存在于实际User bean中的中心方法中。

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"));
}

我做了一些测试,我可以确定,因为我关闭查询的finally块中的原始结果集,我传递给fillUser方法的结果集也会被关闭。或者我错了,我是否认真泄露数据?这实际上是我第二次传递结果集(因此它的两个实例),因为我用来查询我的数据库的块是

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.

推荐答案

<从技术上讲,只要您没有序列化并将其传递给不同的JVM,并且您的JDBC连接和语句仍处于打开状态,就可以传递结果集。

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.

然而,拥有数据库访问层可能是一个更好的软件工程师和编程实践,它以Java编码的方式返回结果集(示例中的用户列表) 。这样,您的代码将更清晰,您不必担心ResultSet是否已经打开,或者您必须将其滚动到顶部,您可以将其命名为...

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...

这篇关于传递ResultSet是否可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆