Jump to content

Need help in Java and database coding


Recommended Posts

I'm a student who is new to java programming, and I have a project which my job is to make a shopping cart, so here is one of the few questions I have. My school wants us to JCreatorLE and Eclipse 3.1 to do our project and Microsoft Access to do our database.

move1cd.jpg

I need to make something like the privacy section of MSN messenger which I can move a product from the left (list of all products for sale) to right (shopping cart). The below is the code for moving an item from left to right.

private void selectListItem(){
 cartModel.addElement(this.jlMSProducts.getSelectedValue());
 model.removeElement(this.jlMSProducts.getSelectedValue());
 this.jlCart.setModel(cartModel);
}

I wonder what is the code for moving from right to left. I tried reshuffle it into the following code but it doesn't work.

private void removeListItem(){
 cartModel.removeElement(this.jlMSProducts.getSelectedValue());
 model.addElement(this.jlMSProducts.getSelectedValue());
 this.jlCart.setModel(cartModel);
}

And on top the declarations for model and cartModel are

private DefaultListModel model;
private DefaultListModel cartModel;

I don't understand what are model and cartModel are for? Can some explain it to me?

 

And here is mine second question, our database has 4 tables (I can't combine them because my groupmates need them to be seprated). Here is my code to read from one of them.

private void addItemsToList(){
 model = new DefaultListModel();     
 cartModel = new DefaultListModel();	
 
 
    dbControl = new DBController();
 dbControl.setUp("Example");  
 ResultSet rs = dbControl.readRequest("Select ProductName from SecuritySoftware"); 	 
 try { 	 
	 while(rs.next()){
   model.addElement(rs.getString(1));    
	 }
	 this.jlMSProducts.setModel(model);    
 } catch (Exception e){
	 e.printStackTrace();
 }

}

"Example" is the name of the database we added using odbcad32, while "SecuritySoftware" is one of the 4 tables. Let's say the another table is called Microsoft, how do I add it? I try adding another method and it looks fine, I wonder if I'm doing the right way or there is an easier way like having just one method instead of four.

private void addItems2ToList(){
 
 dbControl = new DBController();
 dbControl.setUp("Example");  
 ResultSet rs = dbControl.readRequest("Select ProductName from Microsoft"); 	 
 try { 	 
	 while(rs.next()){
   model.addElement(rs.getString(1));    
	 }
	 this.jlMSProducts.setModel(model);    
 } catch (Exception e){
	 e.printStackTrace();
 }

}

 

BTW, thanks for reading this, and please guide me slowly because this whole database adding isn't in my notes, thanks in advance.

Link to comment
Share on other sites

private void selectListItem(){
cartModel.addElement(this.jlMSProducts.getSelectedValue());
model.removeElement(this.jlMSProducts.getSelectedValue());
this.jlCart.setModel(cartModel);
}

 

Would reversing the entire thing work?

 

private void selectListItem(){
model.removeElement(this.jlMSProducts.getSelectedValue());
cartModel.addElement(this.jlMSProducts.getSelectedValue());
this.jlCart.setModel(cartModel);
}

 

Or is cartModel your shopping cart and model the store inventory? I don't know because I don't know how your code is put together overall..........and I don't do Java.

 

If that doesn't work, maybe your first try was correct in application but wrong in order? Can you add an item thats already been removed? Does it still exist in that case?

 

Try this instead

 

private void selectListItem(){
model.addElement(this.jlMSProducts.getSelectedValue());
cartModel.removeElement(this.jlMSProducts.getSelectedValue());
this.jlCart.setModel(cartModel);
}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...