0

I have table that stores what a customer would have bought. The table is of the following format:

Columns: id fullName cell items.

The column items is of data type LONGTEXT. I want to store items as strings there. Under normal circumstances, the string will be represented as Text line 1,Text line 2,Text line 3,Text line 4 in the same cell and are shown the same way when displayed in a JTable.

My question: Is there a way to store strings as shown below in the same cell?

Text line 1
Text line 2
Text line 3
Text line 4

Or is it possible to show them in that layout in JTable cell?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
CN1002
  • 1,115
  • 3
  • 20
  • 40
  • 1
    a string is just a group of characters, so you can't store data in a string on separate lines, that said you can store line breaks and carriage returns in the string so that when you display it, the string appears on separate lines – mgrenier May 15 '15 at 15:21
  • a better option might be to store your values as a string array having each line as a separate element and loop through the elements writing each one to a new line – mgrenier May 15 '15 at 15:23
  • 1
    More [here](http://stackoverflow.com/q/25043191/230513) and [here](http://stackoverflow.com/a/13876867/230513). – trashgod May 15 '15 at 22:09
  • @mgrenier I think an array will not work because I need the data even when the program is exited/not running; that is why I settled for a database. I would want to use the data to create some reports. I would want to try your idea in the first comment. I would have to append the `\n`at the end of each string/line-of-text, right? – CN1002 May 16 '15 at 11:16
  • @trashgod Let me work on the link you have given me; I will get back soon to notify of my progress. – CN1002 May 16 '15 at 11:18
  • See also [`TablePopupEditor `](http://stackoverflow.com/a/3591230/230513). – trashgod May 16 '15 at 12:19

1 Answers1

1

I suggest that this really isn't the way you should design your tables in the database. Since you're using a relational database, I recommend designing your tables like this:

For your first table, have columns id fullName cell

Then, create another table named items with columns id table_1_id item.

When you insert the customer information into the first table you need to also get back the unique ID so that you can use it to insert related data into the items table. Here's a link to show how that is done.

Then, you put your list of items in the items table with each item occupying its own row. When you want to view the items later you can select from your first table and then join in your items table:

SELECT id, fullName, cell, item
FROM table1
JOIN items
ON table1.id = items.table_1_id
John Hodge
  • 1,645
  • 1
  • 13
  • 13
  • The data I must store is created dynamically, I am not sure how to deal with two tables that are linked with some keys. My idea above was that a record is identified by an `id` which is `auto-increment`. – CN1002 May 20 '15 at 07:09
  • I'm not really sure what you're asking. In my example above, both 'Id' columns should be auto-incremented. What do you mean when you say that you're not sure how to deal with tables that are linked? – John Hodge May 20 '15 at 09:20
  • Well, let me try to explain myself. I have a system that generate quotations on products. Now, I want a mechanism to store information of a quotation so that a sales person may call the customer confirming whether the customer will come back or not-for the later why?(product too expensive?...etc). The infor which is necessary is `customer name, customer cell and names of products that the customer was quoted`. This infor is from a JTable- I want to store all product names/description for one quotation in one table cell so that when a sales rep calls a customer -....continued below – CN1002 May 20 '15 at 10:58
  • He/she will see what products the customer was quoted on. So my program works this way: When the sales rep send the quotation to be printed it captures the information of that quotation and store it in a database. When I said I am not sure how to deal with tables linked the way you have suggested I mean: in your second table `table_1_id` is required when inserting a record into that table. The user does not have to know the program is working this way or saving something. All I want is to capture infor I have stated so that ref can be made when calling the customer. – CN1002 May 20 '15 at 11:08
  • I've updated my answer to show how to handle a situation like that for you. – John Hodge May 20 '15 at 15:35