How to join the multiple row values from jTable into one row of mysql
How to join the multiple row values from jTable into one row of mysql
So i have a jframe that looks like this:
everytime i send the data to my database it creates 3 rows which exactly look like this (please see attached image for reference)
Purchase Order | Name | Item_ID | Item_Name | Item_price | Item_Stock | Total
1 | Marc | 2 | 9V Battery | 90.0 | 1 | 90.0
1 | Marc | 3 | Stranded Wire | 10.0 | 1 | 10.0
1 | Marc | 4 | Solid Wire | 12.0 | 1 | 12.0
What i want to do is merge the value of this three rows and send this to database like this using java
1 | Marc | 2,3,4 | 9V Battery, Stranded Wire, Solid Wire | 90.0,10.0,12.0 | 1,1,1 | 90.0,10.0,12.0
is that even possible?? this is my first time to ask so pardon me if i make mistakes
this is my current code
try
Class.forName("com.mysql.cj.jdbc.Driver") ;
conn = DriverManager.getConnection("jdbc:mysql://localhost/dbms?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","");
st = conn.createStatement();
String PurchOrder = JL_POrder.getText();
String Name = JL_Name.getText();
TableModel model = jTable3.getModel();
for(int j=0;j<model.getRowCount();j++)
String Item_ID=jTable3.getValueAt(j, 0).toString();
String Item_Name=jTable3.getValueAt(j, 1).toString();
String Item_price=jTable3.getValueAt(j, 2).toString();
String Item_Stock=jTable3.getValueAt(j, 3).toString();
String Total = jTable3.getValueAt(j, 4).toString();
st.executeUpdate("INSERT INTO `customer_order_details` (`Purchase Order`, `Name`, `Item_ID`, `Item_Name`, `Item_price`, `Item_Stock`, `Total`) VALUES ('"+PurchOrder+"','"+Name+"','"+Item_ID+"','"+Item_Name+"','"+Item_price+"','"+Item_Stock+"','"+Total+"')");
JOptionPane.showMessageDialog(null, "Successfully Checkout");
catch (SQLException ex)
Logger.getLogger(customer.class.getName()).log(Level.SEVERE, null, ex);
See also: RDBMS normalization. Then isolate how the data is persisted from how the user interacts with it.
– Andrew S
Sep 5 '18 at 18:10
hey @camickr well the problem is the column purchase order is set to be a primary key so that will be a problem
– Eamyr
Sep 6 '18 at 17:53
order is set to be a primary key so that will be a problem
- then that is the problem you should be fixing. In any case, I'm still not sure what your question is. If you know how to update a data base with a String for a column then you can update a dabase with a string that contains data separated with commas. A String is a String.– camickr
Sep 6 '18 at 19:22
order is set to be a primary key so that will be a problem
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Anything is possible but kind of defeats the purpose of using a database. Each column in the database should store 1 data item, not 3 items separated by commas. So you should have 3 rows in the database. The purchase order and the item id would be the unique keys which would allow you to have 1 or more rows for each purchase order.
– camickr
Sep 5 '18 at 17:41