简体   繁体   中英

How to fix Unknown column 'i' in 'field list'

Unknown column 'i' in 'field list'

for (int i = 1100; i < 20000; i++) {
    stmt.addBatch("insert into student (id, name) values (1100+i, 
        'yang"+i"') ");
}
stmt.executeBatch();
con.commit();

You're using i inside the string, just like you're using values or insert inside the string. So naturally the text i is sent to the database, which doesn't have a column or identifier for it.

Instead, use a prepared statement and setInt :

PreparedStatement ps = con.prepareStatement("insert into student (id, name) values (?, ?)");
for (int i = 1100; i < 20000; i++) {
    ps.setInt(1, 1100 + i); // Or I think you may just want `i`, not `1100 + i`
    ps.setString(2, "yang" + i);
    ps.addBatch();
}
ps.executeBatch();
con.commit();

Let me introduce to you my friend Bobby :

在此处输入图片说明

Don't use string concatenation to put values in SQL strings. First, it's easy to mess it up (as in this case). Second, it's insecure unless you do a lot of work to ensure the resulting string is clean (which is easy to mess up).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM