java - How to read JSON data type in mysql using JDBC -
mysql 5.7 introduced json data type offers tonnes of query functions.
since there isn't compatible resultset function, how , use retrieve data stored in datatype.
it should rs.getstring, because getstring used varchar, text, can consider json string type, can result, using getstring.
simple example
double check mysql 5.7 , postgresql 9.4 :
mysql 5.7 sql
create database db_test; create table table_test(json json); insert table_test values('{"key1": "value1", "key2": "value2"}'); code
public static void checkjson() { try { class.forname(driver); connection connection = drivermanager.getconnection(db_url, db_username, db_password); string q = "select * table_test"; preparedstatement preparedstatement = connection.preparestatement(q); preparedstatement.execute(); resultset rs = preparedstatement.executequery(); while (rs.next()) { system.out.println(rs.getstring("json")); } } catch (classnotfoundexception | sqlexception e) { e.printstacktrace(); } } it print me :
{"key1": "value1", "key2": "value2"}
Comments
Post a Comment