jdbc how to insert mysql SET type?

蹲街弑〆低调 提交于 2021-01-29 07:14:22

问题


Hi I have a code for table creating:

create table clt (id bigint not null, sources set('A1', 'empty', 'A2', 'A3'), text varchar(50));

table was created successfully.

now I'm trying to insert data:

java.sql.PreparedStatement stmt = null;
String query = "insert into clt (id, sources, text) values (?, ?, ?)";
stmt = conn.prepareStatement(query);
int it = 0;
stmt.setLong(++it, 25);
stmt.setString(++it, "A1, A2");
stmt.setString(++it, "some text data");
stmt.executeUpdate();

and gettting an error :( exception: java.sql.SQLException: Data truncated for column 'sources' at row 1

without sources everything is ok. where is my mistake?

thank you.


回答1:


Get rid of the parentheses around A1:

stmt.setString(++it, "A1");


来源:https://stackoverflow.com/questions/5398133/jdbc-how-to-insert-mysql-set-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!