Link to home
Create AccountLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

fill JTable with db data

hello there,

i have a jtable where the i can insert the product code and the table row gets filled with data for that product code.which is working.now i went a step forward and put a cb for the product name in the table.so that i can select the product name and the data will be filled.but i get an error.but if i remove the product code and only keep the product name feature it works.but not both. the problem i have is when i use one of the two what happens is that when the prouct name is filled the program again starts product name.i hope you understand what i mean.
please help.

cheers
zolf
@Override
		public void setValueAt(Object value, int row, int column) {
			if(column==0)
			{
				System.out.println(value.toString());
				PreparedStatement stmt=null;
				ResultSet rs=null;
				try
				{
					stmt=conn.prepareStatement("SELECT     RawMaterialID, RawMaterialName, RawMaterialCode, RawMaterialUnitPrice, RawMaterialUnit" +
							         		   "	FROM         RawMaterial "+
											   "WHERE RawMaterialCode=?");
					stmt.setString(1,value==null?null:value.toString());
					rs=stmt.executeQuery();
					if(rs.next())
					{
						super.setValueAt(value, row, column);
						
						setValueAt(rs.getString(2),row,1);
						setValueAt(rs.getString(5),row,3);
						setValueAt(rs.getDouble(4),row,4);
					}
					else
					{
						super.setValueAt(null, row, column);
						JOptionPane.showMessageDialog(BatchSheetInternalFrame.this,"Sorry there are not products with id \""+value+"\"","Information",JOptionPane.INFORMATION_MESSAGE);
						int[] columns=new int[]{1,3};
						for(int i:columns)
						{
							setValueAt(null,row,i);
						}
						setValueAt(0.0,row,4);
					}
					calculateTotal(row);
				} catch (SQLException SQLEx) {
					GUITools.displayErrorInformation(BatchSheetInternalFrame.this,SQLEx);
					SQLEx.printStackTrace();
				}finally
				{
					rs=DBTools.clearRes(rs);
					stmt=DBTools.clearRes(stmt);
				}
			}
			else if(column == 1)
			{
				System.out.println(value.toString());
				System.out.println(Integer.toString(rawMaterialIds.get(rawMaterialNameCB.getSelectedIndex())));
				int index = rawMaterialIds.get(rawMaterialNameCB.getSelectedIndex());
				PreparedStatement stmt=null;
				ResultSet rs=null;
				try
				{
					stmt=conn.prepareStatement("SELECT     RawMaterialID, RawMaterialName, RawMaterialCode, RawMaterialUnitPrice, RawMaterialUnit" +
							         		   "	FROM         RawMaterial "+
											   "WHERE RawMaterialID=?");
					stmt.setInt(1,value==null?null:index);
 
					rs=stmt.executeQuery();
					if(rs.next())
					{
						super.setValueAt(value, row, column);
						setValueAt(rs.getString(3),row,0);
						setValueAt(rs.getString(5),row,3);
						setValueAt(rs.getDouble(4),row,4);
					}
					else
					{
						super.setValueAt(null, row, column);
						JOptionPane.showMessageDialog(BatchSheetInternalFrame.this,"Sorry there are not products with id \""+value+"\"","Information",JOptionPane.INFORMATION_MESSAGE);
						int[] columns=new int[]{1,3};
						for(int i:columns)
						{
							setValueAt(null,row,i);
						}
						setValueAt(0.0,row,4);
					}
					calculateTotal(row);
				} catch (SQLException SQLEx) {
					GUITools.displayErrorInformation(BatchSheetInternalFrame.this,SQLEx);
					SQLEx.printStackTrace();
				}finally
				{
					rs=DBTools.clearRes(rs);
					stmt=DBTools.clearRes(stmt);
				}
				
			}
			else
			{
				super.setValueAt(value, row, column);
				if(column==2)
				{
					calculateTotal(row);
				}
					
			}
		}
	}

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

whats the error?

Avatar of Zolf

ASKER


i dont get to see the error.i mean the console does not display the start of the error because of the loop.how can i capture all the error.i am using eclipse IDE.
Just copy the Eclipse console and paste it here
>                                                 setValueAt(rs.getString(2),row,1);
>                                                 setValueAt(rs.getString(3),row,0);
>  ...

you should be calling super.setValueAt()

what you are doing results in it making another database query, which sets the values, then makes another db query etc

Avatar of Zolf

ASKER


objects,

where do i add this for column==0 or column==1. or to both.can you please explain more
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Zolf

ASKER


thanks mate