Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV file Read Problem! Help needed

Status
Not open for further replies.

abc73

Programmer
Apr 28, 2004
89
0
0
US
I am using the following code to read data from a csv file. Can someone help me when I have a numeric with leading zero's in my csv file
in the output the leading zero's are automatically being removed. I want to keep the leading zero's. e.g. I have a following csv-file:

Code:
1234,Mike,1235 XYZ,1
0001234,,,2
12340000,Test,1235 XYZ,3
Now when I read the file in the second row the data 0001234 comes out to be 1234, but I need it to be as 0001234 and don't want any data
to be modified. May be Microsoft Driver doing something. Any work arrounds. Following is a code snippet>
Code:
c = DriverManager.getConnection("jdbc:odbc:;Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=.;Extensions=csv,txn");			
stmt = c.createStatement();
String sql = "select * from " + filelocation;
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
while(rs.next())
{				
   String Number = rs.getString("Number");
   String Name = rs.getString("Name");
   String Address = rs.getString("Addr");
   String Count = rs.getString("Count");
}
Any help or workarround is really appreciated. Thanks
 
I'm pretty sure that Microsoft considers a csv file to be a type of Excel format - in which case it would convert the 00001234 to a number.

If you can, you might try putting quotes around each of the elements in the file so the driver treats them as text, like this:

"00001234","Test","1235 XYZ","3
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top