How to represent an array of Strings in mySQLdatabase - mysql

I'm new using mySQL database. I have a class in Java that as an variable that is an array of strings.
I would like to save that array of strings on my database.
How can i represent my array of string in the mysql database?
thank you

I can think of some ways:
Serialize the object.
How does that work? First you need a table that can hold the class name and the object serialized. You need the class name for when you need to read the object from the database.
Sample code can be found here:
http://javapapers.com/core-java/serialize-de-serialize-java-object-from-database/,
and
http://www.java2s.com/Code/Java/Database-SQL-JDBC/HowtoserializedeserializeaJavaobjecttotheMySQLdatabase.htm
Sometimes all you need to serialize is not the object, but rather, the attributes of the object. In this case, you can use XML or JSON (for example).
In this case, create a String representation of your object and save it to the database as a text field.
You can map your object to one or more tables using any of the many ORM solutions, such as iBatis, Hibernate, etc.
http://java.dzone.com/articles/getting-started-ibatis-mybatis
http://hibernate.org/orm/
If what you need is to save some attributes of your object to the database then you can either use JDBC and just update the field, or use an ORM and create a POJO with only the attributes you'd like to save.

Related

Extract a specified data object that resides inside a PDF form field to an external file

I am looking for a script to extract a data object nested inside a data object to a new data object. Aside from being able to easily clone the object by creating a new one, while I can also extract one of several objects nested inside an object that includes all its related properties and values, the name associated with the property and values is missing. Hence, I require a script to create a new object containing any one of several names including its related properties and values extracted from the existing object provided below.
{"J Doe Company":{"lastUpdate":"01/05/2023","website":"jdoecompany.com","userID":"jdoe1985#gmail.com","password":"igfndhsi1985","primaryCC":"Discover","secondaryCC":"Capital One","primaryBank":"Chase","secondaryBank":"","sq1":"Year Graduated HS","sa1":"1985","sq2":"","sa2":"","notes1":"Sample password record","notes2":""},"Bob The Builder":{"lastUpdate":"01/05/2023","website":"bobthebuilder.com","userID":"bobthebuilder#gmail.com","password":"bbob1985","primaryCC":"Amazon Visa","secondaryCC":"Mastercard","primaryBank":"Wells Fargo","secondaryBank":"","sq1":"First Girlfriend's Name","sa1":"Kaye","sq2":"","sa2":"","notes1":"Sample password record","notes2":""},"SpongeBob Square Pants":{"lastUpdate":"01/07/2023","website":"spongebobsquarepants.com","userID":"spongebob#gmail.com","password":"spongebob1999","primaryCC":"None/Not Applicable","secondaryCC":"","primaryBank":"None/Not Applicable","secondaryBank":"","sq1":"Year show debuted on TV","sa1":"1999","sq2":"","sa2":"","notes1":"Animated TV show for kids","notes2":""}}
You can run cpdf -output-json in.pdf -o put.json and process the result. The format is described in the cpdf manual.
Upon further trial and error after learning more about how to create and edit an object literal, the script required to add an object along with its key-value properties is as follows:
dsFld =getField("dataSrc");// dataSRC is a hidden field containing a JS object converted to a JSON string
oVendors = JSON.parse(dsFld.value); // oVendors is a JS object
oVendors[event.value]=oVendorsPropValues;//oVendorsPropValues = {key-value pairs}
dsFld.value = JSON.stringify(oVendors);// convert the JS obj back to a JSON string

Create list of custom objects from flat JSON data

I want to create an arraylist of type Adapter from a JSON. But since the JSON is not in arraylist format, I'm unable to use gson.fromJson() method.
Is there any way by which I can create a list of my custom object by parsing the following JSON?
JSON data:
"source":{"adapter-config.adapter[0].name":"testAdapter1",
"adapter-config.adapter[0].resolverName":"serviceResolver",
"adapter-config.adapter[0].parameters[0].key":"serviceId",
"adapter-config.adapter[0].parameters[0].value":"serviceIdPathInEvent",
"adapter-config.adapter[0].parameters[1].key":"appId",
"adapter-config.adapter[0].parameters[1].value":"appIdPathEvent",
"adapter-config.adapter[0].parameters[2].key":"env",
"adapter-config.adapter[0].parameters[2].value":"envPathInEvnet"}
My Adapter Object:
public class Adapter {
private String name;
private String resolverName;
private List<KeyValuePair<String, String>> attributeList;
}
Gson does not provide such functionality out of the box. However you can achieve this by manually reading the JSON data from a JsonReader, consuming the JSON property names with nextName() and then parsing them to determine which data they represent. You could either directly read from a JsonReader, or in case the shown JSON data is only an extract from a larger JSON document, you can implement a TypeAdapter for your List<Adapter>. That TypeAdapter could then either be registered with a GsonBuilder by providing new TypeToken<List<Adapter>>() {}.getType() as type, or you could annotate the field holding the List<Adapter> with #JsonAdapter.
For the actual parsing of List<Adapter>, I would recommend storing a current adapter (and its index in the list) in a local variable. Whenever you parse a JSON property name, you could then check if the index encoded in the name is equal to the index of the current adapter, then you are going to modify the existing instance, otherwise if the encoded index is equal to the index of the current adapter + 1 you create a new Adapter instance, add it to the list of adapters and reassign the current adapter variable and its index variable. Then you continue with parsing the remainder of the property name to find out which Adapter field values to set.
(In case you get stuck there, feel free to let me know in the comments and I can try to provide some concrete code; but it would probably be best if you tried it yourself first.)

Can Hibernate automatically map a HashMap to a string column?

I have a HashMap which was populated from form elements of an HTML page, and when I save it I need Hibernate to convert it automatically to a JSON string and also persist it as a JSON string. Is this doable in Hibernate? Or, can you tell Hibernate in the mapping file (or maybe, as an annotation) to call a Java method that converts HashMap to JSON, and persist the return value of that method, and vice-versa? (read JSON string and convert automatically to a HashMap).
Thanks for all the help!
Sure, you can just create a custom user type:
http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/types.html#types-custom
Hibernate will then use this to persist data to the database, and to read it back as object.

MySQL UDF for working with json?

Are there any good UDFs in MySQL to deal with json data, that supports the ability to retrieve a particular value in json (by dot notation key - EG: json_get('foo.bar.baz')) as well as the ability to set the value of a particular key - EG: json_set('foo.bar.baz', 'value')?
I found http://www.mysqludf.org/lib_mysqludf_json/ - but it seems to only provide the ability to create json data structures from non-json column values, as opposed to interacting with json column values.
This UDF is able to parse JSON and return the value of an attribute:
https://github.com/kazuho/mysql_json
This other one too: https://github.com/webaroo/mysql-json-udf

Create a List-type view dynamically from a Json object in MVC3

I have a controller that access a WCF service which returns a Json object (collection). All rows are of same type, but at different calls the row stricture is different (the return object comes from a user-built sql query, executed with executeReader and serialized as Json
So I don't know the row structure upfront.
What I need is an easy way to pass this Json string to something which will generate a view of type list on the fly for it. Doesn't matter formatting, etc, just should be output easily as a table.
Does anyone knows how can I accomplish this?
Another option might be to have something that generate the view on the fly for a IEnumerable of anonymous objects (since using this I could convert the json to a list of anonymous)
EDIT
I found something that does pretty much what I need, except it display metadata about passed object.
It is preetyPrint.js, and I integrated it in my page as below:
In my controller I set the result json object to ViewBag.Result, and in the view I used this code:
<script src="#Url.Content("~/Scripts/prettyprint.js")" type="text/javascript"> </script>
<div id="resultGrid"></div>
<script>
var resultObject = #Html.Raw(ViewBag.Result);
var ppTable = prettyPrint(resultObject);
document.getElementById('resultGrid').appendChild(ppTable);
</script>
Does anyone knows such script that actually "dump" the data instead of metadata?
Thanks.
You should create a class to deserialize to if you know the properties of the row. Then use the JavaScriptSerializer class to deserialize to a list of your new class you created. Then you can take a look at the WebGrid class to output the HTML, or just manually iterate over the property metadata in your view.
Creating a custom class will provide you the ability to use metadata to control formatting or other display attributes of the output.
If you cannot create a custom class, you can always use Json.NET or the JavaScriptSerializer to deserialize to a list of dictionary objects or ExpandoObject / Dynamic's or something. Then you would have to manually write something to iterate the keys I think. The ModelMetadataProvider in MVC may be able to handle these allowing you to just iterate the properties in your view code.