用于java jdbc结果集的数据结构。在java中handeling jdbc记录的事实标准是什么? [英] What data structure to use with a java jdbc resultset. What is the defacto standard in handeling jdbc records in java?

查看:113
本文介绍了用于java jdbc结果集的数据结构。在java中handeling jdbc记录的事实标准是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习java。当我使用java中的查询进行jdbc调用时,如何存储该记录。我已经看到它们被更高级的编码器存储在哈希集合中,但我不确定如何使用java集合框架来存储记录并迭代它们。有没有人有一个简单的代码示例,他们可以共享和解释存储结果集的记录,如hashmap?

Learning java. When I make a jdbc call with a query in java, how can I store that record. I have seen them being stored in hash collections by more advanced coders but I am not sure how to use the java collections framework to store records and iterate through them. Does anyone have a simple code example they can share and explain for storing records of resultset like a hashmap?

提前谢谢你。对不起这个菜鸟问题。

Thank you in advance. sorry for the noob question.

推荐答案

如果您正在使用框架,那么您必须遵循ORM框架的规则。如果您正在自己编写类并自己进行JDBC调用,那么您可以按如下方式提供自己的简单框架:

If you are using a framework, then you have to play by the rules of that framework for ORM. If you're rolling your own classes and making JDBC calls on your own, then you can provide a simple "framework" of your own as follows:


  • 数据库中的每个表都是一个类

  • 表中的每一列都是一个类的实例字段

  • 您可以编写一个通用的查询方法将数据作为对象列表返回

  • 使用工厂生成要为所使用的查询创建的对象

  • Every table in the database is a class
  • Every column in the table is a class's instance field
  • You can write a generic query method that returns your data as lists of objects
  • Use a factory to generate the objects that you wish to create for the queries you use

以下是这个范例的一个简单示例:

Here is a simple example of this paradigm:

我有一个数据库客户端,所以我会写一个类来表示数据模型。此类是使用JavaFX属性编写的,因此元素可以在GUI中表示。

I have a database Clients, so I will write a class to represent the data model. This class is written using JavaFX properties so that the elements can be represented in a GUI.

public class Client extends DB {

private IntegerProperty id =         new SimpleIntegerProperty();
private StringProperty  lastName =   new SimpleStringProperty();
private StringProperty  firstName =  new SimpleStringProperty();

public final int getID() {return this.id.get(); }
void setID(int id) { this.id.set(id); }
public final IntegerProperty idProperty() { return this.id; }

public final String getLastName() { return this.lastName.get(); }
public final void setLastName(String ln) { this.lastName.set(ln); }
public final StringProperty lastNameProperty() { return this.lastName; }

public final String getFirstName() { return this.firstName.get(); }
public final void setFirstName(String fn) { this.firstName.set(fn); }
public final StringProperty firstNameProperty() { return this.firstName; }

Client(ResultSet jrs) {

    try {
        this.id.set(jrs.getInt(DB.CLI_FIELD_ID));
        this.lastName.set(jrs.getString(DB.CLI_FIELD_LAST));
        this.firstName.set(jrs.getString(DB.CLI_FIELD_FIRST));

    } catch (SQLException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        System.exit(1);
    }
}

static class ClientFactory implements Callback<ResultSet,Client> {

    @Override
    public Client call(ResultSet jrs) {
        return new Client(jrs);
    }
}

这个类包含三件事:


  1. 类的实例字段的JavaFX bean定义,以便它们可以与JavaFX框架提供的GUI控件一起使用。

  1. The JavaFX bean definitions for the instance fields of the class so that they may be used with the GUI controls provided by the JavaFX framework.

一个构造函数,它初始化Client类的一个实例,其中包含当前指示的ResultSet行中包含的数据,该行作为参数传递给构造函数。

A constructor which initializes an instance of the Client class with the data contained in the currently indicated row of a ResultSet that is passed as a parameter to the constructor.

一个嵌套的静态类,它提供用于创建新Client对象的工厂。这很简单。所有工厂都通过向其传递ResultSet行来调用Client的构造函数。请注意,此类实现了Callback接口,以指示此工厂希望接收Resultset类型的一个参数,并且它将返回Client类型的一个结果。我选择使用Callback接口来实现此目的,但程序员可以使用自己类似的工厂接口。

A nested static class that provides the factory used to create new Client objects. It is very simple. All the factory does is invoke the constructor for Client by passing a ResultSet row to it. Notice that this class implements the Callback interface to indicate that this factory expects to receive one parameter of the type Resultset and that it will return one result of type Client. I chose to use the Callback interface for this purpose, but the programmer could use his own similar interface for the factory.

为了使这个本地框架工作,可以使用类似于以下内容的本地查询方法。此方法允许程序员使用long类型的单个参数进行查询,但可以轻松地对其进行抽象,以允许使用提供的参数列表构建查询:

To make this homegrown framework work, a home-grown query method can be used similar to the following. This method allows the programmer to query with a single parameter of type long, but it could be easily abstracted to allow a query to be built using a supplied list of parameters:

static <E> ObservableList<E> doGenericQuery(long param,
        String sql, Callback<ResultSet,E> factory) {

    Connection con = null;
    ObservableList<E> queryList = FXCollections.<E>observableArrayList();

    try {
        con = MyDataBase.getConnection(); // provide your own connection here
        PreparedStatement ps = con.PreparedStatement(sql);

        ps.setLong(1, param);

        ResultSet jrs = ps.executeQuery();

        while (jrs.next()) {
            queryList.<E>add(factory.<ResultSet,E>call(jrs));
        }

    } catch (SQLException e) {
        Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, e);

    } finally {            
        if (jrs != null) try { jrs.close(); } catch (SQLException e) { }
    }

    return queryList; 
}

此通用方法查询数据库并将ResultSet转换为对象列表基于您的一个数据模型表类(此处为ObservableList,因为JavaFX也是如此)。关键是通用方法调用:

This generic method queries your database and transforms the ResultSet into a List of objects that is based on one of your data model table classes (here an ObservableList because, again, JavaFX). The key line is the generic method call:

queryList.<E>add(factory.<ResultSet,E>call(jrs));

使用工厂为您的一个数据模型类(E类)构建一个对象E类使用ResultSet中当前指示的数据行。

Which uses the factory for one of your data model classes (Class E) to build an object for that class E using the row of data currently indicated in your ResultSet.

以这种方式构建数据库应用程序有其优点和缺点:它可能比依赖ORM框架更高效,但它还要求你做所有数据库到对象的映射步法。它使您免于依赖框架,但您必须编写更多代码,特别是样板文件。

Building a database app this way has advantages and disadvantages: it may be more performant than relying upon an ORM framework, but it also requires you to do all the database to object mapping footwork. It frees you from dependency upon a framework, but you'll have to write more code, particularly boilerplate.

如果您的应用程序很简单,或者您的数据库不是太复杂你可能希望以类似的方式生成自己的框架。

If your application is simple, or your database is not too complex you may wish to homegrow your own "framework" in a similar way.

这篇关于用于java jdbc结果集的数据结构。在java中handeling jdbc记录的事实标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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