Solved

fill JTable  with db data

Posted on 2008-06-15
7
2,068 Views
Last Modified: 2008-06-17
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

0
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 21792172
whats the error?

0
 

Author Comment

by:zolf
ID: 21795384

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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21795921
Just copy the Eclipse console and paste it here
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 21798440
>                                                 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

0
 

Author Comment

by:zolf
ID: 21800252

objects,

where do i add this for column==0 or column==1. or to both.can you please explain more
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 21800275
both, you've got an endless loop.
setting product code will result in a db query which will set product name , which will set product code etc.
All calls to setValueAt() should really be super.setValueAt() to avoid these kinds of problems

0
 

Author Comment

by:zolf
ID: 21800577

thanks mate
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question