field mapping

hi frnds

i need to read a csv file, display its field names on jsp againest field names fetched from database.. the user will map fields from csv to that of db... and data from csv wil be transferred to database...

can anyone help me in how to approach this problem...

thanks...

[304 byte] By [aziz.saiyeda] at [2007-11-27 10:41:48]
# 1

break this into manageable steps. for starters, can you read the .csv file and process it in java?

den2681a at 2007-7-28 19:14:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

hi den

i have written code to read a csv file... where i m stuck is, how to map fields of csv file and mysql db fields.. bcoz all the fields in csv file may not be required in the database... please provide some advice or informative links if u have one..

thanks for ur help...

aziz.saiyeda at 2007-7-28 19:14:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

you'll want to create some kind of DataLine object class, that can take a String line from a .csv file and load the data into the DataLine object.

for instance, if the .csv file contained employee info, you might make an EmployeeLine class, which has a String name, String position, int yearsInCompany...... things like that. you would process the .csv file a line at a time, creating a new EmployeeLine each time and adding them to a List or an Array.

from there you can determine which fields you need from each object and then find a way to store them into the DB.

den2681a at 2007-7-28 19:14:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

hi den,

can u pls ellaborate the solution a little bit... probably if you can send some link on the topic

thanks for the reply..

aziz.saiyeda at 2007-7-28 19:14:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

why dont' you start by posting some code that shows how you are reading the .csv file and we can go from there.

be sure to post using the code tags -- click the code button when posting and put your code inbetween the tags.

den2681a at 2007-7-28 19:14:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

this is how i m reading the csv file and storing the values in an array

[]code]

public class test {

public static void main(String[] args) {

File csv = new File("C:\\Mycsvfile.csv");

try {

BufferedReader in = new BufferedReader(new FileReader(csv));

String lineReader = "";

long t = 0;

String[] linetext;

while ((lineReader = in.readLine()) != null) {

int len = 0;

len = lineReader.length();

lineReader = lineReader.substring(1, len - 1);

linetext = lineReader.split(",");

System.out.println(linetext);

t++;

[/code]

aziz.saiyeda at 2007-7-28 19:14:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Ok, well do you know how to make a new data class? What type of data is contained in that .csv file, because for each line of data you'll want to make a new class Object and set it's properties like I described earlier.

So,

1) What type of data is in your .csv file

2) Trying making a data class and post your code of it

3) Is this for work or school?

den2681a at 2007-7-28 19:14:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

hi ben,

sorry, but m not following you..

m doing this as a part of assignment given to me..

the data in the csv file can vary... but a selected number of predefined fields will be needed from each csv... these fields will need to be copied from csv to database....

for eg. csv may have name, age, city, state, country

but we always need name, city and country out of any csv file...

the name of the fields may vary for different csv's.. this mapping will be done by user..

but each time we need to extract those fields and put it into the database in the fields according to the mapping done by the user..

Message was edited by:

aziz.saiyed

aziz.saiyeda at 2007-7-28 19:14:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Start by making a class called DataLine that contains objects for each variable you'll need to track, like

public class DataLine {

private String name;

private String street1;

private String street2;

private String city;

private int age;

.....

}

then you'll want to make a constructor that can take a String[] and fill a DataLine object. The String[] will be one line from the .csv file

public DataLine(String[] values) {

this.name = values[0];

this.street1 = values[1];

this.street2 = values[2];

this.city = values[3];

this.age = Integer.parseInt(values[4]);

}

den2681a at 2007-7-28 19:14:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...