Search code examples
mysqljsonjdbcprepared-statement

How to Insert value in varchar as well as in json format in MySQL using Prepare Statement JDBC?


Table Schema: CREATE TABLE `book` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(200) NOT NULL,
  `tags` json DEFAULT NULL,
  PRIMARY KEY (`id`)
)

where the value of title is 'ECMAScript 2015: A SitePoint Anthology' and tags value is in json array '["JavaScript", "ES2015", "JSON"]'


Solution

  • insertStatement.setObject(n++, new Gson().toJson(pii.getEmails()));