Text from txt To Table in MSACCESS

Reply

Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Text from txt To Table in MSACCESS

 
0
  #1
Apr 3rd, 2008
Good morning,

I'm facing dificulties to understand what i'm doing wrong!
I'm trying to send the contents of a file...


SPI01<br />
	184-25-03-T-01-R.F.T.1.47e85ab1spicsv, 4609, 2008.03.25, 09:51:45 AM<br />
	184-25-03-T-01-R.F.T.1.47e8966dspicsv, 0, 2008.03.25, 14:06:38 PM<br />
	184-25-03-T-01-R.F.T.10.47e863easpicsv, 48, 2008.03.25, 10:31:06 AM<br />
	184-25-03-T-01-R.F.T.10.47e897f4spicsv, 0, 2008.03.25, 14:13:08 PM

into a database "spimon" with a table LogSPI01 with 4 fields but later will have more...
the source code is here:
  1. package spilog;
  2. /*
  3.  * 1.Read the file line by line,
  4.  * 2.Split the line into multiple words using StringTokenizer and Tab ("\t") as delimeter.
  5.  * 3.Create insert query (if the table is existing, otherwise create it) and
  6.  * add the query to a batch using, statement.addBatch(query);
  7.  * 4.Finally execute the batch, statement.executeBatch();
  8.  * */
  9.  
  10. /*import packages */
  11. import java.io.*;
  12. import java.sql.*;
  13. import java.util.*;
  14.  
  15. public class test {
  16.  
  17. public static void main (String[] args) {
  18. String TABLE_NAME = "LogSPI01",
  19. HOST = "jdbc:odbc:spimon",
  20. DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver",
  21. FILENAME = "F:\\SPI01.log";
  22. try {
  23. // connect to db
  24. Class.forName(DRIVER).newInstance();
  25. Connection con = DriverManager.getConnection(HOST,"","");
  26. PreparedStatement ps = null;
  27. // open text file
  28. BufferedReader in = new BufferedReader(new FileReader(FILENAME));
  29. String line=in.readLine();
  30. while((line=in.readLine()) != null) {
  31. System.out.println(line);
  32. // read and parse a line
  33. StringTokenizer tk = new StringTokenizer(line,"\t");
  34. if(tk.countTokens()>=10) {
  35.  
  36. String PCB = tk.nextToken(),
  37. FAIL_COUNT = tk.nextToken(),
  38. DATE = tk.nextToken(),
  39. TIME = tk.nextToken();
  40.  
  41. // execute SQL insert statement
  42. //String query = "INSERT INTO 1 VALUES(FailureID_CSV,Fail_Qty,DATE_OCURRANCE,TIME_OCURRANCE)";
  43. String query = "INSERT INTO LogSPI01 VALUES(?,?,?,?)";
  44. ps = con.prepareStatement(query);
  45. ps.setString(1,PCB);
  46. ps.setString(2,FAIL_COUNT);
  47. ps.setString(3,DATE);
  48. ps.setString(4,TIME);
  49.  
  50. ps.executeUpdate(query);
  51. }
  52. else {
  53. System.out.println("Error:");
  54. }
  55. }
  56. in.close();
  57. con.close();
  58. } catch( Exception e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }


Can anyone help me?

Thanks in advance,
Regards
Jorge
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Text from txt To Table in MSACCESS

 
0
  #2
Apr 3rd, 2008
We might, if we knew what you meant by "difficulties".

Is it not compiling?
Are you getting Exceptions when running?
Are you not getting the expected Results?

We need some more (actually, alot more) detailed information.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Text from txt To Table in MSACCESS

 
0
  #3
Apr 3rd, 2008
Originally Posted by masijade View Post
We might, if we knew what you meant by "difficulties".

Is it not compiling?
Are you getting Exceptions when running?
Are you not getting the expected Results?

We need some more (actually, alot more) detailed information.

Thank you for replying!

The program is compiling without errors
I don't have any exceptions when is running!
In fact the program is accessing the txt file, is reading every line but is not spliting the text as it should be...

i think it has to be with structure of the text...

184-22-03-T-01-E.F.T.#1.47f023f1.spi.csv, 0, 2008.03.31 , 07:36:17 AM

my idea is to split this line in:
184-22-03-T-01-E.F.T.
.#1.47f023f1.spi.csv
0
2008.03.31
07:36:17 AM

and insert each block of text in each field of the table...

And the program is not performing this at all

Thank you in advance
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Text from txt To Table in MSACCESS

 
0
  #4
Apr 4th, 2008
Rather than StringTokenizer try using split with the regex "\\s*,\\s*"
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Text from txt To Table in MSACCESS

 
0
  #5
Apr 6th, 2008
Originally Posted by masijade View Post
Rather than StringTokenizer try using split with the regex "\\s*,\\s*"
  1. package spilog;
  2.  
  3.  
  4. import java.io.*;
  5. import java.sql.*;
  6. import java.io.BufferedReader;
  7. import java.io.FileReader;
  8.  
  9. class Database {
  10.  
  11. public static void main (String args[]) {
  12.  
  13.  
  14. Database myprog = new Database();
  15. myprog.readFile();
  16. System.out.println("done");
  17. } // end main
  18.  
  19. void readFile() {
  20.  
  21. String record;
  22.  
  23. try {
  24.  
  25. BufferedReader inputStream = new BufferedReader(new FileReader("F:\\SPI01.log"));
  26. record = new String();
  27.  
  28.  
  29. while ((record = inputStream.readLine()) != null) {
  30. String st = record;
  31. String []splitSt = st.split(",");
  32. System.out.println("string : " +st);
  33. System.out.println("length :" + splitSt.length);
  34. int i;
  35.  
  36. //Connecting to the database
  37. //String URL =
  38. //"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=E:\\public\\Log\\Results\\spimon.mdb;}",
  39. String HOST = "jdbc:odbc:spimon",
  40. DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
  41. Class.forName(DRIVER).newInstance();
  42. Connection myConn = DriverManager.getConnection(HOST,"","");
  43. //Connection myConn = DriverManager.getConnection(URL,"","");
  44.  
  45.  
  46. //using the database
  47. Statement stat = myConn.createStatement();
  48. PreparedStatement updaterecord;
  49. String query="INSERT INTO logspi01 VALUES(?,?,?,?)"; //tem que ter o mesmo numero de campos da DB
  50. updaterecord=myConn.prepareStatement(query);
  51.  
  52.  
  53. for (i = 0; i < splitSt.length; i++ )
  54. {
  55. updaterecord.setString ((i+1), splitSt[i]);
  56. }
  57. updaterecord.executeUpdate();
  58.  
  59. stat.close();
  60. myConn.close();
  61. }//end while
  62. } catch (IOException e) {
  63. // catch possible io errors from readLine()
  64. System.out.println("IOException error: ");
  65. e.printStackTrace();
  66. }
  67. catch (Exception ex)
  68. {
  69. ex.printStackTrace();
  70. }
  71. }
  72. }

Thank you Very much... for the tip... during the weekend i've tryed to use split... not so complicated as the previous way... but let me ask you another thing... to avoid having duplicates in the database... there is anyway to avoid that using java.....

After exporting the data to access db... i would like to delete all the records that were duplicated...

Is it possible?!

Regards,

Jorge
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Text from txt To Table in MSACCESS

 
0
  #6
Apr 7th, 2008
Keep an ArrayList of the "keys" (those fields which iniquely identify the record), then use contains before adding the record.

Another way, would be to define a "primary key" in the table, then simply attempt to insert the records and catch the "duplicate key" SQLException.
Last edited by masijade; Apr 7th, 2008 at 3:53 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Text from txt To Table in MSACCESS

 
0
  #7
Apr 7th, 2008
Originally Posted by masijade View Post
Keep an ArrayList of the "keys" (those fields which iniquely identify the record), then use contains before adding the record.

Another way, would be to define a "primary key" in the table, then simply attempt to insert the records and catch the "duplicate key" SQLException.


hmmm i'm not so good!

Can you provide me an example...???? meanwhile i've managed to create a tricky way avoid duplicates.... that it is
  1. if (fdir.getPath().endsWith(".csv") && fdir.lastModified() > HostFile.lastModified())
  2. System.out.println(INDENTS[depth] + fdir.getName() +", "+ countline +","+Date.format(nDate));

By placing that if ,automatically i will filter the contents being exported to the log file... and the they can be exported to the database....
but your tips looked more reliable...

please.. help!

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Text from txt To Table in MSACCESS

 
0
  #8
Apr 7th, 2008
Originally Posted by masijade View Post
Keep an ArrayList of the "keys" (those fields which iniquely identify the record), then use contains before adding the record.

Another way, would be to define a "primary key" in the table, then simply attempt to insert the records and catch the "duplicate key" SQLException.

Really i'm lost... i know what primary keys are but... the way that you've explained look really easy, however i have several dificulties on this subject...

Please help me!

regards,

Jorge
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Text from txt To Table in MSACCESS

 
0
  #9
Apr 8th, 2008
Do you know what a try/catch block is (I hope you do if you're using JDBC)? If so, then, simply put the execute statement for the insert inside of its own try/catch block, and when the insert fails due to duplicate keys, simply ignore that record and go on to the next.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Text from txt To Table in MSACCESS

 
0
  #10
Apr 9th, 2008
Originally Posted by masijade View Post
Do you know what a try/catch block is (I hope you do if you're using JDBC)? If so, then, simply put the execute statement for the insert inside of its own try/catch block, and when the insert fails due to duplicate keys, simply ignore that record and go on to the next.
Hi again,,,

I'm trying to understand,,,, and i can't

I've tryed something like this
  1. package spilog;
  2.  
  3.  
  4. import java.io.*;
  5. import java.sql.*;
  6. import java.io.BufferedReader;
  7. import java.io.FileReader;
  8.  
  9. class Database {
  10.  
  11. public static void main (String args[]) {
  12.  
  13.  
  14. Database myprog = new Database();
  15. myprog.readFile();
  16. System.out.println("done");
  17. } // end main
  18.  
  19. void readFile() {
  20.  
  21. Connection myConn = null;
  22. String record;
  23.  
  24. try {
  25.  
  26. //BufferedReader inputStream = new BufferedReader(new FileReader("F:\\SPI01.log"));
  27. BufferedReader inputStream = new BufferedReader(new FileReader("E:\\public\\Log\\SPIlog.txt"));
  28. record = new String();
  29.  
  30.  
  31.  
  32.  
  33. while ((record = inputStream.readLine()) != null) {
  34. //if(record.trim().length() == 0) {
  35. record=record.trim();
  36. if (record.equalsIgnoreCase("\n")==false){
  37.  
  38. /* Check for characters '#' and replace with ',' */
  39. char [][] maps = {{'R', ' '},{'.', ' '},{'S', ' '},{'E', ' '},{'F', ','},};
  40. for (int i = 0; i<maps.length; ++i)
  41. {
  42. record = record.replace(maps[i][0], maps[i][1]);
  43. }
  44.  
  45. String st = record;
  46. String []splitSt = st.split(",");
  47.  
  48.  
  49.  
  50. System.out.println("string : " + st );
  51.  
  52.  
  53. System.out.println("length :" + splitSt.length );
  54. int i;
  55.  
  56. //Connecting to the database
  57. //String URL =
  58. //"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=E:\\public\\Log\\Results\\spimon.mdb;}",
  59. String HOST = "jdbc:odbc:spimon",
  60. DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
  61. Class.forName(DRIVER).newInstance();
  62. myConn = DriverManager.getConnection(HOST,"","");
  63. //Connection myConn = DriverManager.getConnection(URL,"","");
  64.  
  65.  
  66. //using the database
  67. Statement stat = myConn.createStatement();
  68. PreparedStatement updaterecord;
  69. String query="INSERT INTO logspi01 VALUES(?,?,?,?,?,?,?)"; //tem que ter o mesmo numero de campos da DB
  70. updaterecord = myConn.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
  71.  
  72.  
  73. for (i = 0; i < splitSt.length; i++ )
  74. {
  75. //updaterecord.setString ((i+1), splitSt[i]); /*or*/
  76. updaterecord.setString((1)," "); //machine
  77.  
  78. updaterecord.setString((2),splitSt[0]); //package
  79. updaterecord.setString((3)," "); //lotnumber
  80. updaterecord.setString((4),splitSt[1]); //Fail Location - (primary Key)
  81. updaterecord.setString((5),splitSt[2]); //Fail Qty
  82. updaterecord.setString((6),splitSt[3]); //Date
  83. updaterecord.setString((7),splitSt[4]); //Time
  84. // updaterecord.setString((1),splitSt[0]);
  85. // updaterecord.setString((2),splitSt[1]);
  86. // updaterecord.setString((3)," ");
  87. // updaterecord.setString((4),splitSt[3]);
  88. // updaterecord.setString((5),splitSt[4]);
  89. // updaterecord.setString((6),splitSt[5]);
  90. // updaterecord.setString((7),splitSt[6]);
  91. }
  92. updaterecord.executeUpdate();
  93. int x = updaterecord.executeUpdate();
  94. stat.close();
  95. myConn.close();
  96. myConn = null;
  97. System.out.println(" Inserting Data " + x + " row(s)");
  98.  
  99. }//end while
  100. }
  101. }
  102. catch (IOException e)
  103. {
  104. /* catch possible io errors from readLine() */
  105. System.out.println("IOException error: ");
  106. e.printStackTrace();
  107. }
  108. catch (Exception ex)
  109. {
  110. ex.printStackTrace();
  111. }
  112. finally
  113. {
  114. if (myConn != null) try {myConn.close();
  115. }
  116. catch (Exception ignore){}
  117. }
  118. }
  119. }
  120.  
  121. /**
  122.  * Keep an ArrayList of the "keys" (those fields which iniquely identify the record), then use contains before adding the record.
  123.  
  124. Another way, would be to define a "primary key" in the table, then simply attempt to insert the records and catch the "duplicate key" SQLException.
  125.  
  126.  *
  127.  *
  128.  * Do you know what a try/catch block is (I hope you do if you're using JDBC)? If so, then, simply put the execute statement for the insert inside of its own try/catch block, and when the insert fails due to duplicate keys, simply ignore that record and go on to the next.
  129. Java Programmer and Sun Systems Administrator
  130.  */
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. ///*
  163. //import java.io.*;
  164. //import java.sql.SQLException;
  165. //import java.util.*;
  166.  
  167. //public class T3 {
  168. //public static void main (String[] args){
  169.  
  170. //String line = null;
  171. //boolean first = true;
  172. //int count = 51;
  173. //int rowNum = 0;
  174. //String[] fieldlist = new String[count];
  175. //String monTable = "LogSPI1";
  176. //BufferredReader reader = null;
  177. //Connection con = null;
  178. //Statement stmt = null;
  179. //String url = "jdbc:odbc:Driver={Microsoft Access Driver " +
  180. //"(*.mdb)};DBQ=c:/MyDocu~1/pyxis.mdb";
  181. ////HOST = "jdbc:odbc:spimon";
  182.  
  183. //try {
  184. //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  185. //con = DriverManager.getConnection (url, "", "");
  186. //stmt = con.createStatement ( );
  187. //} catch (ClassNotFoundException e){
  188. //System.err.println ("Unsuccesful loading : " + e.getMessage ( ));
  189. //} catch (SQLException ex){
  190. //System.out.println ("con object error " + ex.getMessage( ));
  191. //}
  192.  
  193.  
  194. //try{
  195. //reader = new BufferedReader(new FileReader("F:\\SPI01.log"));
  196. //} catch (FileNotFoundException e){
  197. //System.out.println ("Check the file name/path.")
  198. //System.exit ();
  199. //}
  200.  
  201. //while ((line = reader.readLine ()) != null ){
  202. //if (first) {
  203. //first = false;
  204. //rowNum++;
  205. //continue;
  206. //}
  207.  
  208. //StringTokenizer tok = new StringTokenizer (line, ",", true );
  209. //if( tok.countTokens() != count ) {
  210. //System.out.println("Problem at row number " + rowNum);
  211. //System.exit();
  212. //}
  213. ////parse fields
  214. //for( int field = 0; field < count; field++ ) {
  215. //String token = tok.nextToken ();
  216. ////remove "," at the end
  217. //fieldlist[field] = token.substring (0, token.length()-1);
  218. //}
  219.  
  220. ////create insert command - handle each field as text
  221. //String insert = "INSERT INTO monTable values(fieldlist[0]";
  222. //for (int field = 1; field < count; field++){
  223. //insert += "," + fieldlist[field];
  224. //}
  225. //insert += ")";
  226.  
  227. //try {
  228. //stmt.executeUpdate( insert );
  229. //} catch (SQLException e) {
  230. //System.out.println ("SQL error " + et.getMessage ( ) );
  231. //}
  232. //}
  233.  
  234. //reader.close ( );
  235. //stmt.close ( );
  236. //con.close ( );
  237.  
  238. //}
  239. //}
  240.  
  241. //}*/
but is providing this exception

java.lang.UnsupportedOperationException
at sun.jdbc.odbc.JdbcOdbcConnection.prepareStatement(Unknown Source)
at spilog.Database.readFile(Database.java:70)
at spilog.Database.main(Database.java:15)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC