Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating simple Inventory java Application so I what to display customer completer order and payment from MySQL tables in java. I wanted to display the customer ledger in java Jtables. I am unable to display the table in required from it is display only in one row . but i want the data in two row. Below images are the both MySql tables :

[http://i.stack.imgur.com/6AIwv.jpg[]]

and this below image is the required format :

http://i.stack.imgur.com/tm7IJ.png[][]


please any one help me....

What I have tried:

The java code i have tried is below

private void Table_display(){

try{
st = conn.createStatement();
rst = st.executeQuery("select a.Ref_ID,b.PayRefID,a.Date as OrderDate,b.Date as PaymentDate,a.Narration,b.Bank_Details,a.Vehicle_No,a.Quantity,a.Price,(a.Quantity * a.Price) as Amount from purchase a left join payment b on a.Ref_ID = b.Ref_Id");
ResultSetMetaData rsmd = rst.getMetaData();
int colcount = rsmd.getColumnCount();
Vector column = new Vector(colcount);
for(int i = 1; i <= colcount; i++)
column.add(rsmd.getColumnName(i));

Vector data = new Vector();
Vector row;

while(rst.next()){
row = new Vector(colcount);
for(int i=1; i <= colcount; i++){
row.add(rst.getString(i));
}
data.add(row);
}
DefaultTableModel model = new DefaultTableModel(data, column);
jAccount_table.setModel(model);
}catch(SQLException e){
JOptionPane.showMessageDialog(null, e);
}
}
Posted
Updated 29-May-16 22:13pm

1 solution

It looks like you are using some form of data binding to display the details, which will result in the table you see. To create the alternate version you will need to do one of two things:
1. Read all data manually and insert each item into the row and column you want.
2. Convert each row from the database into two rows with the data split into the first set and last set of columns.
 
Share this answer
 
Comments
J M V Praveen Kumar 30-May-16 4:38am    
Thank you Richard MacCutchan, can u please tell how to display us in first thing that you said, read all data manually and insert each item into the row and column
Richard MacCutchan 30-May-16 4:55am    
Read the next record of data.
Add two empty rows to your grid.
Copy the relevant fields from your data record to your grid rows and columns.
Repeat until all records have been read.
J M V Praveen Kumar 30-May-16 13:02pm    
sir if you don't mind can you tell with an example please.
Richard MacCutchan 30-May-16 13:06pm    
Example of what? What you are trying to create is something unique to you, so you have to write the code. This is what software development is all about, converting some logical steps into real code.
J M V Praveen Kumar 30-May-16 21:16pm    
Richard MacCutchan thank you once again. I finally got what i needed with the following code i have wrote. I don't know whether it is professional way or not. i tried to display output image in the comment but it is not possible..

private void tableDisplay(){

try{
String sql = "select a.Ref_ID,a.Date as OrderDate,b.Bank_Details,a.Quantity,a.Price,(a.Quantity * a.Price) as Debit,b.Amount as Credit,b.Date from purchase a left join payment b on a.Ref_ID = b.Ref_Id";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();

Vector<string> columnNames = new Vector<string>();
columnNames.add("RefID");
columnNames.add("Date");
columnNames.add("Quantity");
columnNames.add("Price");
columnNames.add("Debit");
columnNames.add("Bank Details");
columnNames.add("Credit");

Vector data = new Vector();

while(rs.next()){

Vector<string> vstring = new Vector<string>();
vstring.add(rs.getString("Ref_ID"));
vstring.add(rs.getString("OrderDate"));
vstring.add(rs.getString("Quantity"));
vstring.add(rs.getString("Price"));
vstring.add(rs.getString("Debit"));
data.add(vstring);

Vector<string> vstring1 = new Vector<string>();
vstring1.add(" ");
vstring1.add(rs.getString("Date"));
vstring1.add(" ");
vstring1.add(" ");
vstring1.add(" ");
vstring1.add(rs.getString("Bank_Details"));
vstring1.add(rs.getString("Credit"));
data.add(vstring1);

}
DefaultTableModel model = new DefaultTableModel(data, columnNames);
jAccount_table.setModel(model);

}catch(SQLException e){
JOptionPane.showMessageDialog(null, e);
}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900