SQL Problem

Hi,

I am new to java so apologises if I am doing something stupid.

Ok I have a text file called access.log what I am trying to do is break the file into tokens using StreamTokenizer and then store the tokens in a multiple dimensional array. I then want to write the elements to an access database file using an insert statement. However when I execute my code it only adds one of the elements to each record set and leaves the rest blank it is also not the first element. I know that the file is being broken into tokens properly and stored in the array properly because I can print to the screen(have commented the lines out in my code that I used to test this) Any help would be appreciated.

import java.io.*;

import java.io.StreamTokenizer.*;

import java.sql.*;

publicclass TestAgain{

public TestAgain()throws Exception{

FileReader log =new FileReader("logs/access.log");

BufferedReader bufflog =new BufferedReader(log);

StreamTokenizer stlog =new StreamTokenizer(bufflog);

int j = 0;

int k = 0;

String [][] process;

process =new String [61][];

stlog.resetSyntax();

stlog.whitespaceChars(34, 34);

stlog.whitespaceChars(43, 43);

stlog.whitespaceChars(45, 45);

stlog.whitespaceChars(91, 91);

stlog.whitespaceChars(93, 93);

stlog.wordChars(32, 33);

stlog.wordChars(35, 42);

stlog.wordChars(44, 44);

stlog.wordChars(46, 90);

stlog.wordChars(92, 92);

stlog.wordChars(94, 125);

stlog.eolIsSignificant(false);

int x = 0;

boolean eof =false;

do{

int token = stlog.nextToken();

switch(token){

case StreamTokenizer.TT_EOF:

//System.out.println(x);

eof =true;

break;

case StreamTokenizer.TT_EOL:

j++;

k = 0;

//x++;

break;

case StreamTokenizer.TT_WORD:

process[j] =new String[13];

process [j][k] = stlog.sval;

//System.out.println(process[j][k]);

k++;

break;

default:

if (token =='!') eof =true;

}

}while (!eof);

for(int l =0; l< 61; l++)

do{

String data ="jdbc:odbc:LogFiles1";

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection conn = DriverManager.getConnection(data,"","");

PreparedStatement prep2 = conn.prepareStatement(

"INSERT INTO Logs VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

prep2.setString(1, process[l][0]);

prep2.setString(2, process[l][1]);

prep2.setString(3, process[l][2]);

prep2.setString(4, process[l][3]);

prep2.setString(5, process[l][4]);

prep2.setString(6, process[l][5]);

prep2.setString(7, process[l][6]);

prep2.setString(8, process[l][7]);

prep2.setString(9, process[l][8]);

prep2.setString(10, process[l][9]);

prep2.setString(11, process[l][10]);

prep2.setString(12, process[l][11]);

prep2.setString(13, process[l][12]);

prep2.executeUpdate();

conn.close();

}catch (SQLException sqe){

System.out.println("SQL Error: " + sqe.getMessage());

}catch (ClassNotFoundException cnfe){

System.out.println(cnfe.getMessage());

}

}while (j < 61);

}

publicstaticvoid main(String[] arguments)throws Exception{

TestAgain ta =new TestAgain();

}

}

[5640 byte] By [Carl666a] at [2007-10-1 1:37:56]
# 1

Your for loop looks a bit wierd. You have a do/while loop right inside the for loop. You never change the value of "j" so I don't understand why you even coded the do loop.

You are connecting the database inside the for loop. This shouldn't cause a problem but it is very inefficient. You should:

a) connect to the data

b) do all updates to the database using the same PreparedStatement, just change the data

c) close the connection.

camickra at 2007-7-8 1:57:34 > top of Java-index,Security,Event Handling...
# 2
I have removed the do while loop, I think it must of got mixed in when I was experimenting.Still have the same problem.Thanks
Carl666a at 2007-7-8 1:57:34 > top of Java-index,Security,Event Handling...
# 3

I don't see where the exit condition for the loop is incremented:

while (j < 61);

You open and close the database connection every time you go through the loop. Very bad idea. Open it once outside the loop, execute all the PreparedStatements inside a transaction, and close the connection in a finally block. You don't close your PreparedStatement, either. Create that outside the loop and close it in that finally block, too.

Geez, ever heard of object orientation? This is some pretty ugly procedural code.

All that stuff does not belong in a constructor.

Your problem statement has two easily identifiable tasks:

(1) Break a file into tokens & store in multiple dimensional array

(2) Store the elements in a SQL database

You'll do yourself a big favor if you decompose this problem a bit. You might say that your code "works" (sorta), but the exercise will do you some good.

Ever heard of the concept of "magic numbers"? Your code is full of them. Bad idea.

Ever heard of "3rd normal form"? I don't think your database schema looks very well normalized. I'm not sure that you'd qualify for first normal form.

I'd rework this stuff if I were you.

%

duffymoa at 2007-7-8 1:57:34 > top of Java-index,Security,Event Handling...