-1

在这个程序中,我为数据库中的每个字段使用了不同的数组变量。在数据库中,所有字段都具有相同的数据类型,现在我想将所有字段值存储到一个数组变量中。这可能吗???

import java.sql.*;
class ja1
{
    public static void main(String ar[])
    {
        try
        {
            int x,i,j,k,l;
            int a[]=new int[30];        
            int b[]=new int[30];        
            int c[]=new int[30];        
            int d[]=new int[30];        
            int count[]=new int[10];
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection c1=DriverManager.getConnection("Jdbc:Odbc:ds");
            Statement s=c1.createStatement();
            ResultSet r=s.executeQuery("select * from pro");
            i=0;
            j=0;
            k=0;
            l=0;
            x=0;
            while(r.next())
            {
                a[i]=r.getInt(2);
                i++;
                b[j]=r.getInt(3);
                j++;
                c[k]=r.getInt(4);
                k++;
                d[l]=r.getInt(5);               
                l++;
            }
            for(i=0;i<6;i++)
                System.out.println(""+a[i]);

            for(j=0;j<6;j++)
                System.out.println(""+b[j]);
            System.out.print("\n\n");
            for(k=0;k<6;k++)
                System.out.println(""+c[k]);
            System.out.print("\n\n");
            for(l=0;l<6;l++)
                System.out.println(""+d[l]);


        }
        catch(Exception e)
        {
            System.out.print(e);
        }
    }
}
4

2 回答 2

0

Yes, it is possible in several ways :

1) If you want to use only one array then please create an array with size of 120 (4X30) and use 4 counter to arrange your data in range. That means, your first variable would be from index 0 to 29, the 2nd would be from 30 to 69 and so on. This is not good if you don't know the exact size of your array as you are binding them with perfect size of 30.

2) You can create a POJO and have 4 arrays into it, you can use a List instead of array, but it depends on your implementation. So, create a class, put 4 arrays into it, give good variable names and access thru getter/setter methods. This will be a clear code

3) You can use a Map<Integer,Integer[]> or Map<Integer,List<Integer>> and have a single variable/reference which is holding your key value pair.

It all depends on you, if you dont know, why are you using arrays,then please move the Collection

于 2012-06-19T02:57:52.580 回答
0

I would define a POJO for the record and then use a generic list to add and iterate the record/s

Try this:

class RecordData
{
    public int First; 
    public int Second; 
    public int Third; 
    public int Fourth; 
}

class ja1
{
    public static void main(String ar[])
    {
        try
        {
           List<RecordData> list = new ArrayList<RecordData>;

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection c1=DriverManager.getConnection("Jdbc:Odbc:ds");
            Statement s=c1.createStatement();
            ResultSet r=s.executeQuery("select * from pro");
            while(r.next())
            {
                            RecordData data = new RecordData();
                            data.First = r.getInt(2);
                            data.Second = r.getInt(3);
                            data.Third = r.getInt(4);
                            data.Fourth = r.getInt(5);
                            list.add(data);
            }
                        for(RecordData data : list) {
                                System.out.println(data.First);
                                System.out.println(data.Second);
                                System.out.println(data.Third);
                                System.out.println(data.Fourth);
                        }
        }
        catch(Exception e)
        {
            System.out.print(e);
        }
    }
}
于 2012-06-19T02:58:14.540 回答