Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading a very complex file and inserting in database  RSS feed

 
Sonal Sharma
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody tell me how to read these three types of text files:-
first file format is:-
SERIAL NUMBER : 0 ADJ TIME : 5.5 [sec]
MODE AL
ITEM READ / ADJ / WRITE DATA SPEC MEASURE ETC
2 H_CENTER 135 135 135 0.0 +1.1 -1.1 0.4
3 V_SIZE 147 147 147 0.0 +2.2 -2.2 -0.9
4 V_CENTER 67 67 67 0.0 +0.9 -0.9 -0.1
5 V_SLOPE 123 123 123 0.0 +1.5 -1.5 0.7
second file format is:
2008-4-5 8:15:4700000000000000 0.000NaNNaNNaNNaN-Inf3184.0000.000
2008-4-5 8:21:1500000000000000 4.10649.82934.89880.275190.6522.0454.0007.633
2008-4-5 8:21:4200000000000000 4.11249.99134.08780.014440.0752.0474.0007.913
2008-4-5 8:22:0500000000000000 4.39549.36234.37481.034567.2382.1434.0007.945
2008-4-5 8:22:1900000000000000 3.80250.05048.43779.920459.5401.9344.0008.390
2008-4-5 8:22:3900000000000000 3.18149.56634.56880.701847.3591.6754.0006.601
2008-4-5 8:23:0500000000000000 2.08949.64045.04980.5801097.9041.0664.0005.636
2008-4-5 8:24:1700000000000000 3.61449.85633.35980.230782.2471.8604.0007.230
2008-4-5 8:24:3300000000000000 2.37149.84357.35580.2531003.1601.2504.0006.532
and third file format is:
Model Name,OK, T/T ,,Hx1,Hy1, HY1 ,Lx1,Ly1, LY1 ,,Hx2,Hy2, HY2 ,Lx2,Ly2, LY2 ,,Hx3,Hy3, HY3 ,Lx3,Ly3, LY3 ,,Hx4,Hy4, HY4 ,Lx4,Ly4, LY4 ,,SC1,RD1,GD1,BD1,SB1,RC1,GC1,BC1,,SC2,RD2,GD2,BD2,SB2,RC2,GC2,BC2,,SC3,RD3,GD3,BD3,SB3,RC3,GC3,BC3,,SC4,RD4,GD4,BD4,SB4,RC4,GC4,BC4
21K44 - MUTHU,OK,2.5,,273,280,42.30,292,303,01.74,, 0, 0,00.00, 0, 0,00.00,, 0, 0,00.00, 0, 0,00.00,, 0, 0,00.00, 0, 0,00.00,, 12, 32, 30, 44, 5, 43, 0, 22,, 0, 0, 0, 0, 0, 0, 0, 0,, 0, 0, 0, 0, 0, 0, 0, 0,, 0, 0, 0, 0, 0, 0, 0, 0
21K44 - MUTHU,OK,3.1,,273,281,41.00,291,301,01.63,, 0, 0,00.00, 0, 0,00.00,, 0, 0,00.00, 0, 0,00.00,, 0, 0,00.00, 0, 0,00.00,, 13, 32, 30, 45, 5, 43, 0, 25,, 0, 0, 0, 0, 0, 0, 0, 0,, 0, 0, 0, 0, 0, 0, 0, 0,, 0, 0, 0, 0, 0, 0, 0, 0
21K44 - MUTHU,OK,4.0,,275,275,49.20,291,302,01.97,, 0, 0,00.00, 0, 0,00.00,, 0, 0,00.00, 0, 0,00.00,, 0, 0,00.00, 0, 0,00.00,, 12, 32, 30, 40, 4, 44, 0, 23,, 0, 0, 0, 0, 0, 0, 0, 0,, 0, 0, 0, 0, 0, 0, 0, 0,, 0, 0, 0, 0, 0, 0, 0, 0

Please do tell me how to read these files get all their tokens and put it into the database.

Thanks and regards
--Sonal
 
Anshul Malpani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell do you want special way to insert this .

IF not then take in to FileInputStream and insert.
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use .setBytes() method to insert to database. Whichever formats your file has or your file is, it will insert into the database.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sonal:
Please do tell me how to read these files get all their tokens and put it into the database.


Sonal, parsing a file depends on what are the tokens of your interest. Saying "all tokens" does not really make any sense until you know as to what constitute a token.
You may wish to see StreamTokenizer
for parsing a stream.
If you foresee a very complex parsing process, you may also use free text parsers like antlr, jflex, etc.
[ April 15, 2008: Message edited by: Nitesh Kant ]
 
Sonal Sharma
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kiran,
Can you give a code snippet to use .setBytes() method which formats the file and inserts into the database.

Thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sonal, you're getting ahead of yourself here. Please read and consider what Nitesh wrote. You need to define (and tell us, if you want us to help you) how the file should be parsed; in other words, what differentiates the tokens from one another. Once you know that it makes sense to think about how to store them in a DB.

If, on the other hand, you want to insert the complete contents of the files, then their internal formats don't matter. You can simply use a BLOB or CLOB field in the DB.
[ April 23, 2008: Message edited by: Ulf Dittmer ]
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sonal,
If you are thinking about parsing, my code will not be useful. My code uses FormFile and here it just inserts the file to database. Here is my code.... I hope it helps you....

public boolean loadFile(FormFile formfile)
{
boolean flag=false;
PreparedStatement ps=null;
Connection con=Coredb.getConn();
try
{
String fileName=formfile.getFileName();
int fileSize=formfile.getFileSize();
byte bb[]=formfile.getFileData();

//TABLE: fles(fileName varchar2(20),size Number(10),contents Blob)
ps=con.prepareStatement("insert into fles values(?,?,?)");
ps.setString(1,fileName);
ps.setInt(2,fileSize);
ps.setBytes(3,bb);
System.out.println("finally : "+ps.executeUpdate());
flag=true;
}
catch (Exception ex)
{
ex.printStackTrace();
flag=false;
}
finally
{
try
{
formfile.destroy();
ps.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return flag;
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!