Good morning,
I'm facing dificulties to understand what i'm doing wrong!
I'm trying to send the contents of a file...
SPI01
184-25-03-T-01-R.F.T.1.47e85ab1spicsv, 4609, 2008.03.25, 09:51:45 AM
184-25-03-T-01-R.F.T.1.47e8966dspicsv, 0, 2008.03.25, 14:06:38 PM
184-25-03-T-01-R.F.T.10.47e863easpicsv, 48, 2008.03.25, 10:31:06 AM
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:
package spilog;
/*
* 1.Read the file line by line,
* 2.Split the line into multiple words using StringTokenizer and Tab ("\t") as delimeter.
* 3.Create insert query (if the table is existing, otherwise create it) and
* add the query to a batch using, statement.addBatch(query);
* 4.Finally execute the batch, statement.executeBatch();
* */
/*import packages */
import java.io.*;
import java.sql.*;
import java.util.*;
public class test {
public static void main (String[] args) {
String TABLE_NAME = "LogSPI01",
HOST = "jdbc:odbc:spimon",
DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver",
FILENAME = "F:\\SPI01.log";
try {
// connect to db
Class.forName(DRIVER).newInstance();
Connection con = DriverManager.getConnection(HOST,"","");
PreparedStatement ps = null;
// open text file
BufferedReader in = new BufferedReader(new FileReader(FILENAME));
String line=in.readLine();
while((line=in.readLine()) != null) {
System.out.println(line);
// read and parse a line
StringTokenizer tk = new StringTokenizer(line,"\t");
if(tk.countTokens()>=10) {
String PCB = tk.nextToken(),
FAIL_COUNT = tk.nextToken(),
DATE = tk.nextToken(),
TIME = tk.nextToken();
// execute SQL insert statement
//String query = "INSERT INTO 1 VALUES(FailureID_CSV,Fail_Qty,DATE_OCURRANCE,TIME_OCURRANCE)";
String query = "INSERT INTO LogSPI01 VALUES(?,?,?,?)";
ps = con.prepareStatement(query);
ps.setString(1,PCB);
ps.setString(2,FAIL_COUNT);
ps.setString(3,DATE);
ps.setString(4,TIME);
ps.executeUpdate(query);
}
else {
System.out.println("Error:");
}
}
in.close();
con.close();
} catch( Exception e) {
e.printStackTrace();
}
}
}
Can anyone help me?
Thanks in advance,
Regards
Jorge