Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read JSON data type in mysql using JDBC

Tags:

java

jdbc

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.

like image 246
Kushan Avatar asked Apr 07 '17 17:04

Kushan


People also ask

Does MySQL support JSON data type?

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.

What is JSON extract () function in MySQL?

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.

Does MySQL 5.7 support JSON?

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.

What is JSON data type in MySQL?

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.

How to write a JSON array to a database using JDBC?

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.

How to query data from MySQL using JDBC?

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 MySQL Connector/J?

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.


Video Answer


1 Answers

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)

like image 125
Lukas Eder Avatar answered Oct 04 '22 05:10

Lukas Eder