Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java io program suggestion  RSS feed

 
YuenLian Wu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need to write a Java program to insert 1,000,000 records from a text file to the DataBase. Each row in the file represents a record. Each field has it's own fix width. So I guess what I have to do is to define the width for each field, for example 1-4 represents BatchNumber, 5-8 represents CampCode. There are a lot of fields in the file, I guess until 500. Seems like I have to hardcode those field width in the Java program, such as


and then I would have to insert them to the database, such as



The above codes are tedious, and I think it's a good design. I want to start with a good design so that folks can follow it up later on. Please give me some suggestions and ideal codes.

Thanks in advance

Transistor

[ February 07, 2006: Message edited by: YuenLian Wu ]
[ February 07, 2006: Message edited by: YuenLian Wu ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, two suggestions. The first one is never build SQL like that; use a PreparedStatement instead. It will be more performant, less error-prone, and more secure.

As far as hard-coding the fields widths, you could use an array, which could either be hard-coded or read from a file, and then do this:

[ February 07, 2006: Message edited by: Ernest Friedman-Hill ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!