Alright folks I am in need of a lil guidance/assistance here. I have a program which reads in a txt file. This txt file contains lines of the form
January 3, 2007, 85.8
Now each line of the txt file is to be read into my java program and then inserted (from the program into an Access Database). I am not exactly sure of where the problem lies. I know it has something to do with how I am parsing the file, or how I am passing my arguments to my insertRecord().
Ok enough chatter, here is the code I need help with.
This is the function reading the file obviously.
Here is the function inserting the records
Lastly here is my createTable function, just in case anyone was interested:)
I have been able to insert records if i hardcode them into my program directly (obviously I don't want to do this for thousands of records). I suppose my output may also be helpful for the brave soul who undertakes the role of my hero. It should also be noted that I am not a very experienced programmer so please "dumb" down any help you may have for the sake of my sanity:)
Output:
-= database driver loaded =-
-= connected to database to DB =- stocks
-= statement created =-
-= table created =-
January 3,2007,83.8
record = insert into Apple values ('January 3,2007',83.8);
Unable to insert record: njava.lang.Null PointerExceptio n
January 3, 2007, 85.8
Now each line of the txt file is to be read into my java program and then inserted (from the program into an Access Database). I am not exactly sure of where the problem lies. I know it has something to do with how I am parsing the file, or how I am passing my arguments to my insertRecord().
Ok enough chatter, here is the code I need help with.
This is the function reading the file obviously.
Code:
public static void readFile(String fileName)
{
JDBC dbInstance = new JDBC();
String line;
try
{
BufferedReader in = new BufferedReader(new FileReader(fileName));
while((line = in.readLine())!= null)
{
StringTokenizer tokens = new StringTokenizer(line, ",");
String month = tokens.nextToken();
String day = tokens.nextToken();
String year = tokens.nextToken();
String close = tokens.nextToken();
double aDouble = Double.parseDouble(close);
String date = month+" "+day;
System.out.println(date+","+year+","+aDouble);
dbInstance.insertRecord(date,year,aDouble);
if(line == null)
break;
// System.out.println(line);
}
in.close();
}catch(Exception e)
{
System.out.println("error loading file or something. " + e.getMessage());
}
}
Here is the function inserting the records
Code:
public void insertRecord(String Date, String Year, double Close )
{
String dateAndYear = Date+","+Year;
String data = "("+"'"+dateAndYear+"'"+","+Close+");";
String sqlRecord = "insert into Apple values " + data;
System.out.println ("record = " + sqlRecord);
try
{
db_statement.executeUpdate (sqlRecord);
System.out.println ("-= record inserted =-");
commitChanges();
} catch (Exception excep) {
System.out.println ("Unable to insert record: n" + excep);
System.exit(0);
}
}
Code:
public void createTable(String stockName)
{
String custTable = "CREATE TABLE "+ stockName +
"([Date] TEXT(50) NOT NULL, Close NUMBER NOT NULL)";
try
{
db_statement.executeUpdate(custTable);
System.out.println ("-= table created =-");
} catch (Exception excep) {
System.out.println ("Unable to create table: n" + excep);
System.exit(0);
}
}
Output:
-= database driver loaded =-
-= connected to database to DB =- stocks
-= statement created =-
-= table created =-
January 3,2007,83.8
record = insert into Apple values ('January 3,2007',83.8);
Unable to insert record: njava.lang.Null PointerExceptio n
Comment