在Java中填充数组 [英] populating an array in java

查看:94
本文介绍了在Java中填充数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static Customer[] /*String*/ getCustomerDetails()
     {
         try
          {
              ConnnectToDB.setConnection();
              Statement CustList = ConnnectToDB.connection.createStatement();;
//              query = "Select customer.customerId,user.firstName from customer, user where customer.userid = user.userid";
              query = "Select user.firstName from  user";
              CustList.executeQuery(query);

              resultSet = CustList.getResultSet();

                if (resultSet != null)
                {
                    while(resultSet.next())
                    {
                       customer[count] = new Customer();
//                     customer[count].setCustomerNumber (resultSet.getInt(1));
                       firstName = resultSet.getString(1) ;
                       customer[count].setFirstName(firstName);
                       count++;

                    }
                }
          }

         catch(SQLException err)
          {
              JOptionPane.showConfirmDialog(null,"could not get customer Details" + " " + err.getMessage());
          }

         return customer;
     }



我希望此代码使用从查询中获得的结果填充客户数组.它当前正在执行的操作是返回一个以null为值的数组.

请帮帮我.
在此先感谢



i want this code to populate the customer array with the result obtain from the query. what its currently doing is its returning an array with null as the value.

please help me.
Thanks in advance

推荐答案

我猜您的查询不正确.
但这是您必须在数据库中解决的问题.
阵列的形成对我来说很好.


添加
这是一个小示例代码.
您可能应该检查一下是否可以简单地返回Vector. Vector通常更易于使用.
更快的是HashMap,例如如果您需要从给定的数据集中选择某些值.

I guess your query is not ok.
But that''s something you will have to figure out with your DB.
The forming of the array looks fine to me.


Addition
Here is a little example code.
You should probably check if you can simply return the Vector. A Vector is often simpler to work with.
Faster would be a HashMap, e.g. if you need to pick certain values out o the given set of data.

import java.util.Vector;

public class Example {
	private Integer[] getValues(){
	  Vector<Integer> oVector = new Vector<Integer>(); // init Vactor
	  oVector.add(new Integer(1)); // load Vector
	  oVector.add(new Integer(2)); 
	  oVector.add(new Integer(3)); 
	  // ... more fun ...
	  return oVector.toArray(new Integer[oVector.size()]); // return Array
	}	
	
	public static void main(String[] args) {
		Example oExample = new Example();
		Integer[] oArray = oExample.getValues();
		for (Integer oValue : oArray) {
			System.out.println("Value is " + oValue);
		}
	}
}


这篇关于在Java中填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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