使用Java将HSQLDB表中的数据转换为二维数组

时间:2022-03-29 12:53:03

I am trying to get data from LivEx table as list, and then save that data into two dimensional array called array. My method is returning object[][].

我试图从LivEx表中获取数据作为列表,然后将该数据保存到称为数组的二维数组中。我的方法是返回object [] []。

I made up this code from googling here and there and taking bits from every example, however I am doing something wrong which I can't quite seem to put my finger on. It gives me an exception which is a NullPointerException whenever I call getLivExList(currentPlan).

我在这里和那里用谷歌搜索这个代码并从每个例子中取出一些东西,但是我做错了什么我似乎无法用手指。每当我调用getLivExList(currentPlan)时,它给出了一个异常,即NullPointerException。

Here is my code:

这是我的代码:

public static Object[][] getLivExList(String currentPlan) throws Exception {
    Session s = null;

    try {
        s = HibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();
        String query = "select F.item, F.category, F.amount from LivEx F where F.planName=?";
        Query q = s.createQuery(query);
        System.out.println(query);
        List fp = q.list();
        s.getTransaction().commit();
        Object array[][] = new Object[fp.size()][3];
        for (int i = 0; i < fp.size(); i++) {
            for (int j = 0; j < 3; j++) {
                array[i][j] = fp.get(j +(3 * i));
            }
        }
         System.out.println("getLivExdata in networthentitymanager OKAY");
        return array;
    } catch (Exception e) {
         System.out.println("problem at getLivExdata in networthentitymanager");
         throw e;
    } finally {
        if (s != null) {
            HibernateUtil.closeSession();
        }
    }

Please help me find out where the problem is coming from.

请帮我找出问题的来源。

This is the exception I am getting:

这是我得到的例外:

    SEVERE: null
    org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [] [select F.item, F.category, F.amount from LivEx F where F.planName=?]
    0 problem 
    at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:319)
    at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:275)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:75)
    at com.elitconsultancy.finplanner.entity.NetWorthEntityManager.getLivExList(NetWorthEntityManager.java:213)

2 个解决方案

#1


2  

You're not setting the plan name for the query, even 'though you've specified a positional parameter (?).

您没有为查询设置计划名称,即使您已指定位置参数(?)。

Put this before your call to list:

在你的名单呼吁之前把这个:

query.setString(0, currentPlan);

#2


0  

List fp = q.list();         
s.getTransaction().commit();         
Object array[][] = new Object[fp.size()][3]; 

you are getting size of list. If list returned by q.list() is null, that is no matching rows found then you will get Null pointer exception.

你正在获得列表的大小。如果q.list()返回的列表为null,那么找不到匹配的行,那么您将获得Null指针异常。

Thats the first thing i see that might be wrong in the code

这是我看到的第一件可能是错误的代码

before invoking methods on list, u can check whether it is null or not.

在调用列表上的方法之前,你可以检查它是否为null。

#1


2  

You're not setting the plan name for the query, even 'though you've specified a positional parameter (?).

您没有为查询设置计划名称,即使您已指定位置参数(?)。

Put this before your call to list:

在你的名单呼吁之前把这个:

query.setString(0, currentPlan);

#2


0  

List fp = q.list();         
s.getTransaction().commit();         
Object array[][] = new Object[fp.size()][3]; 

you are getting size of list. If list returned by q.list() is null, that is no matching rows found then you will get Null pointer exception.

你正在获得列表的大小。如果q.list()返回的列表为null,那么找不到匹配的行,那么您将获得Null指针异常。

Thats the first thing i see that might be wrong in the code

这是我看到的第一件可能是错误的代码

before invoking methods on list, u can check whether it is null or not.

在调用列表上的方法之前,你可以检查它是否为null。