1

i have data in tabular format as per following

id | Firstname | Lastname | Mo_Number 
--------------------------------------
1    Chirag      patel      77887*****
1    Chirag      patel      99002*****
1    Chirag      patel      88937*****
2    Devang      patel      90900*****
2    Devang      patel      45090*****
2    Devang      patel      65900***** 

I want to parse this table in to java.

have made a following class in java class

package com.mirdb.model;

import java.util.Set;

public class Person {

private int id;
    private String fName;
private String lName;
private Set<String> moNumbers;


}

Can any one tell me the method which can give me the list of person objects by parsing above table.

5
  • tabular format is ok..but is it in database? Commented Apr 14, 2014 at 8:11
  • You should first search for Java CSV and come back with a concrete question. Commented Apr 14, 2014 at 8:12
  • yes .. the table is in the resultset object Commented Apr 14, 2014 at 8:13
  • what is primary key and combination I mean how you want to retrive data back..like one chirag patel having three mobile no? Commented Apr 14, 2014 at 8:15
  • Ya .. So I want to make one object with id 1 ...and it has mobile number attribute with Set datatype. So all mobile numbers with id 1 should be there in that object's set Commented Apr 14, 2014 at 8:20

2 Answers 2

2

So assuming your data is in a database which you access via JDBC and you have a ResultSet that holds the data, you probably want to do something like this:

while(result.next()) {
    Person p = new Person();
    p.setId( result.getInt("id") );
    p.setfName( result.getString("Firstname ") );
    // ...
}

Obviously I am assuming your Person class has a visible default constructor and getter/setter methods for all fields. If this is pointing in the right direction, you should look at this tutorial.


Edit: Adding info how you could (there are many ways!) handle multiple phone numbers per person.

Map<Integer,Person> persons = new HashMap<Integer,Person>();
while(result.next()) {
    int id = result.getInt("id");
    Person p = persons.get(id);
    if (null == p) {
        p = new Person();
        // .. add all info to person (without phone number)
        persons.put(id,p);
    }
    p.moNumbers.add(result.getString("Mo_Number"));
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is Fine but how can I get the mobile numbers Set so that I can set it to Person P
0

I doubt that there is something which can do that out of the box, especially since you seem to be missing the ID parameter in your Person object.

A possible solution could be to have a constructor in your Person class which takes a string as a parameter. This string would represent a line in your text file which the constructor would then deconstruct (.split("\\t") maybe?) and then initialize the proper values according to the string which you have passed.

EDIT: Seeing your comment, you could try and do something as suggesting in this previous SO post:

List<Person> pList = new ArrayList<Person>();
while (rs4.next()) {
    pList.add(new Person(rs4.getString(0), rs4.getString(1)...);
}
...
return pList;

And for person:

package com.mirdb.model;

import java.util.Set;

public class Person {

private String ID;
private String fName;
private String lName;
private Set<String> moNumbers;

    public Person(String id, String fName, String lName, Set<String> moNumbers)
    {
        this.ID = id;
        this.fName = fName;
        ...
    }

    ...
    public String getID(){return this.ID;}
    ...

    public boolean equals(object obj)
    {
        if (obj == null)
            return false;
        if (obj == this)
            return true;
        if (!(obj instanceof Person))
            return false;

        Person rhs = (Person) obj;
        return rhs.getID().equals(this.getID());
     }
}

EDIT: As per your other question: But question is how can I make Set<String> moNumbers so that I can pass it to constructor??

In your example you are only showing one masked number (at least that is what I think the * represent. If you have only one number, you could do this:

List<Person> pList = new ArrayList<Person>();
while (rs4.next()) {
    Set<String> mobileNumbers = new HashSet<String>();
    mobileNumbers.add(rs4.getString(3));
    // if on the other hand you have multiple comma delimeted numbers, you could try the approach below:
    //Set<String> mobileNumbers = new HashSet<String>(Arrays.asList(rs4.getString(3).split(",")));
    pList.add(new Person(rs4.getString(0), rs4.getString(1), rs.getString(2), mobileNumbers);
}

return pList;

3 Comments

id is there in the person object. one more thing is two persons are equal if ids are same
Accessing database columns based on their order is a very bad idea!
i got your point. But question is how can I make Set<String> moNumbers so that I can pass it to constructor??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.