Mysql 5.7 introduced the JSON data type which offers tonnes of query functions.
Since there isn't a compatible resultset function, how and what to i do use retrieve the data stored in this datatype.
MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column: Automatic validation of JSON documents stored in JSON columns.
In MySQL, the JSON_EXTRACT() function returns data from a JSON document. The actual data returned is determined by the path you provide as an argument. You provide the JSON document as the first argument, followed by the path of the data to return.
JSON support was introduced in MySQL 5.7. 8. In addition to the benefits just listed, having JSON as a native type in MySQL means that the database can automatically validate JSON documents stored in JSON columns.
Introduction to MySQL JSON data type MySQL supports the native JSON data type since version 5.7.8. The native JSON data type allows you to store JSON documents more efficiently than the JSON text format in the previous versions. MySQL stores JSON documents in an internal format that allows quick read access to document elements.
To write the contents of the a JSON array to a database using JDBC − Register the Driver class of the desired database using the registerDriver () method of the DriverManager class or, the forName () method of the class named Class.
Querying Data From MySQL Using JDBC In this tutorial, we will show you how to query data from MySQL using JDBC Statement and ResultSet objects. To query data from MySQL, you first need to establish a connection to MySQL using Connection object. Connection conn = DriverManager.getConnection (url,username,password);
What's New in Connector/J 8.0? MySQL Connector/J is flexible in the way it handles conversions between MySQL data types and Java data types. In general, any MySQL data type can be converted to a java.lang.String, and any numeric type can be converted to any of the Java numeric types, although round-off, overflow, or loss of precision may occur.
In case JDBC isn't a hard requirement, recent jOOQ versions implemented an out-of-the-box way to map SQL JSON values to arbitrary Java types using the popular JSON mapping libraries Jackson or gson (whichever can be found on your classpath). You can then write:
List<MyClass> list =
ctx.select(TABLE.ID, TABLE.JSON_COLUMN)
.from(TABLE)
.where(...)
.fetchInto(MyClass.class);
With, for example:
public class MyClass {
public int id;
public MyOtherClass jsonColumn;
}
public class MyOtherClass {
public String something;
public List<String> list;
}
Assuming your JSON_COLUMN
contains data of the form:
{
"something": "x",
"list": [ "a", "b", "c" ]
}
This also works with all the natively supported SQL/JSON functions, to create such JSON documents on the fly.
(Disclaimer: I work for the company behind jOOQ)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With