Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inserted data lost!

 
Li Shangqiang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have encountered a very strange problem: I've inserted n lines into a table, but i can just get n-1 lines in table.


The database is Access, and driver is JDBC-ODBC Bridge, code is like this:
<pre>
in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line, supplierId = "test";
StringTokenizer parser;
int count = 0;
while ( (line = in.readLine()) != null) {
System.out.println(line);
parser = new StringTokenizer(line, ",");
// MedId, MedCName, DemandAmount, DemandUnit, DemandPrice, ValidTime, Description
count = parser.countTokens();
if (count != 6) continue;
System.out.println("insert: " + line);
StringBuffer buf = new StringBuffer(sqlStr);
buf.append("('");
buf.append(supplierId); buf.append("','");
buf.append((String)parser.nextToken().trim()); buf.append("','"); //Medid
buf.append((String)parser.nextToken().trim()); buf.append("','"); //MedCName
buf.append((String)parser.nextToken().trim()); buf.append("','"); //SupplyAmount
buf.append((String)parser.nextToken().trim()); buf.append("','");//SupplyPrice
buf.append((String)parser.nextToken().trim()); buf.append("','");//SupplyUnit
buf.append(" ','");
buf.append(date); buf.append("',");
buf.append((String)parser.nextToken().trim()); //ValidTime
buf.append(")");
System.out.println(buf.toString());
System.out.println("insert: " + stmt.executeUpdate(buf.toString())); //return 1 to every line
}
</pre>
Who can give me answer?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give the output from your println statements? And the file line that is not being inserted?
By the way, did you consider using a PreparedStatement for this?
- Peter
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen this problem extensively with certain combinations of the jdbc dbc bridge and MSAccess. Stop pulling your hair out, there are 2 workarounds to force this last record to insert:
1) Close the connection each time afterwards.
2) Do a dummy select each time afterwards. That seems to be the better one.
That means: after your last insert (not after each record) do this "trick".(after "insert into emp..." execute a query "select something from emp...) Not sure if the select has to be from the same table as the insert, but you can try it both ways
Jamie
 
Li Shangqiang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's very strange now no problem at all. If I encountered this problem again, I'll do like what you said.
 
Joe Fisher
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have experienced the same problem if I fail to close the connection when the program terminates.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic