I want to store a Vector<Vector<Object>> in mysql database. What is the best data Tpye that i can use in mysql and java to store and retrieve the datas.

Dani AI

Generated

Quick summary and practical guidance. asked how to persist a Vector<Vector<Object>>. suggested either mapping it into table columns or storing a BLOB; correctly reminded that size is dynamic. Below are safe, practical choices with tradeoffs so you can pick the best fit for your use case.

Relational (best when you need to query or update individual elements): model the nested vectors as rows instead of one opaque cell. Keep one parent table and one child table that stores each element with its position. This preserves ordering, lets you index and search, and survives schema evolution better than raw serialization.

CREATE TABLE vectors (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255)
);

CREATE TABLE vector_elements (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  vector_id BIGINT NOT NULL,
  pos INT NOT NULL,
  value_text TEXT,
  value_type VARCHAR(100)
);

JSON (good for snapshots and flexible schemas): serialize the nested Vector to JSON and store it in a TEXT or native JSON column. JSON is human-readable, easier to migrate than Java serialization, and many drivers/libraries (Gson, Jackson) give simple round-trip code. Example with Gson:

Gson gson = new Gson();
String json = gson.toJson(myVector);
ps.setString(1, json);            // store into TEXT/JSON column

Type t = new TypeToken<Vector<Vector<Object>>>(){}.getType();
Vector<Vector<Object>> v = gson.fromJson(json, t);

BLOB / Java serialization (fast to implement but discouraged long-term): serializing with ObjectOutputStream into a BLOB works, but it is brittle (class changes break reads), opaque (not queryable), and has security/version risks. If you need cross-language stability or long-term storage, prefer JSON, Protobuf/Avro, or a normalized schema. Also consider replacing Vector with List/ArrayList unless you rely on its synchronized behavior.

Recommended Answers

All 2 Replies

You can either do one of to thing..

[1] If you know what the vector sizes are, create a table within mysql for the vector vector where the mysql table is the same size.

[2] create a table with a field that has a data type "BLOB". Save the vector vector as a binary string to this BLOB field. I recommend you use a unique ID to associate the BLOB with an identifier.

fpsasm: one of the reasons to use Vector, is because you don't know up front what the size will be, and the size can change.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.