I'm using a BufferedWriter to send data into a database, (I know this is not really what it's meant for... don't ask. Basically I need to log to a database but buffer the data so that we don't end up with a major bottleneck) and I'm running into the issue that the writer will break up the data in awkward places (like in middle of an insert statement) which is causing errors obviously. Is there a way to force the writer to only send in complete chunks of data? or is all I can do just call .write and see what happens? (Setting byte size won't help because there are many options for different kinds of insert statements)
//insertArray is a char[] that's being sent to BufferedWriter's .write method.
//It contains the information for one record. (I know this is not ideal because of sql injection,
//and I would prefer to send a prepared statement, but I'm using SQLlite which apparently doesn't support conversion of prepared statements to strings).
private void callInsert() {
try {
writer.write(insertArray, 0, insertArray.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The .write method in my class then opens a database connection and sends in whatever data it has as one long statement: like this:
insert into students(fname, lname, id) values(('Bob', 'Green', '5), ('Jake', 'Klein', '7'));
Ideally I would like to truncate the data after the last complete insert before writing and then add the rest to the next write. My current issue is that I could end up with this:
insert into students(fname, lname, id) values(('Bob', 'Green', '5), ('Jake', 'Klein
if the BufferedWriter so chooses to break it up there.