Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

[SOLVED] Deleting items after import from JPanel

import jpanel

  • This topic is locked This topic is locked
3 replies to this topic

#1 skuskusas

skuskusas

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C++
  • Learning:C++

Posted 29 August 2012 - 02:18 AM

I imported from a db to JPanel and everything was ok/works Posted Image now I need to delete selected item from the db and add new item to db. Please help!

here is me code

import javax.swing.*;  

import java.awt.*;  
import java.awt.event.*;  
import java.sql.*;  

public class nn extends JFrame {  
private DefaultListModel model;  
private JButton buttons[];  
private JList dbList;




private JPanel p1,p2,p3;
private String bLabel[] = {"parodyti","Ikelti","Istrinti","prideti"};  

Connection con;  
Statement st;  
ResultSet rs;  
String db;  


	public nn() {  

	 super("Prekes");  

	 setSize(700,600);  

	 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

	 model = new DefaultListModel();  
	 buttons = new JButton[4];  
	 dbList = new JList(model);



	 dbList.setVisibleRowCount(300);  
	 dbList.setFixedCellHeight(50);  
	 dbList.setFixedCellWidth(200);  
	 dbList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);





  p1 = new JPanel();  
  p1.setBorder(BorderFactory.createTitledBorder("NR: "  ));  


  JLabel label = new JLabel();
label.setText("ItemNo");
p1.add(label);

   JTextField textFieed = new JTextField(12);
textFieed.setText("");
p1.add(textFieed);

  JLabel label2 = new JLabel();
label2.setText("WeekNo");
p1.add(label2);

	JTextField textFieed2 = new JTextField(12);
textFieed2.setText("");
p1.add(textFieed2);

	JLabel label3 = new JLabel();
label3.setText("Quantity");
p1.add(label3);

	  JTextField textFieed3 = new JTextField(12);
textFieed3.setText("");
p1.add(textFieed3);






  p1.add(new JScrollPane(dbList));

p3 = new JPanel();  
  p3.setBorder(BorderFactory.createTitledBorder("NR: " +"				  "+"Week"+"		  "+"Quantity"  ));
  p3.add(new JScrollPane(dbList));

  p2 = new JPanel();  
  p2.setLayout(new GridLayout(4,1));  
  p2.setBorder(BorderFactory.createTitledBorder("Display: "));  


  for(int count=0; count<buttons.length; count++) {  
   buttons[count] = new JButton(bLabel[count]);  
   p2.add(buttons[count]);  
  }  


	 Container pane = getContentPane();  
	 setContentPane(pane);  

	 //Set  
	 GridLayout grid = new GridLayout(1,2);  
	 pane.setLayout(grid);  

	 //Creat
	 try {  
	Class.forName("org.apache.derby.jdbc.ClientDriver");  
	db = "jdbc:derby://localhost:1527/mydatabase";  
	con = DriverManager.getConnection(db,"app","app");  
	st = con.createStatement();	

   } catch (Exception e) {  
	JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.WARNING_MESSAGE);  
	System.exit(0);  
   }  


  buttons[0].addActionListener(  
  new ActionListener() {  


   public void actionPerformed(ActionEvent event) {  
	try {  
	 model.clear();  
	 rs=st.executeQuery("select * from item");  
	 while (rs.next()) {  
		 model.addElement(rs.getString("ItemNO")+"		 "+rs.getString("WeekNO")+"		 "+rs.getString("Quantity"));


	 }  

	} catch (Exception e) {  
	 System.out.println("nepavyko parodyti duomenu(0)");  
	}  
   }  
  }  
  );  


  buttons[1].addActionListener(  
  new ActionListener() {  


   public void actionPerformed(ActionEvent event) {  
	 try {  

	  String fileName = ("C://java/SPI00.xls");

			   Main app = new Main();
app.readDataToExcelFile(fileName);


	} catch (Exception e) {  
	 System.out.println("Nepavyko ikelti duomenu (1)");  
	}  
   }  
  }  
  );
// here need code to remove from db
   buttons[2].addActionListener(  
  new ActionListener() {  

   public void actionPerformed(ActionEvent event) {  
	try {  

	 rs=st.executeQuery("delete * from item");  


	} catch (Exception e) {  
	 System.out.println("nepavyko parodyti duomenu(0)");  
	}  
   }  
  }  
  );  
   // here need code to update DB
   buttons[3].addActionListener(  
  new ActionListener() {  

   public void actionPerformed(ActionEvent event) {  
	try {  

	 rs=st.executeQuery("insert into item");  


	} catch (Exception e) {  
	 System.out.println("nepavyko parodyti duomenu(0)");  
	}  
   }  
  }  
  );  




  pane.add(p1);
  pane.add(p2);
  pane.add(p3);


	 setVisible(true);  
	 setResizable(false);  
	}  


	public static void main (String[] args) {  
	 nn rdjl = new nn();  
}  
}

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2,560 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java, PHP

Posted 29 August 2012 - 03:39 AM

Here's a possible delete example:
                    public void actionPerformed(ActionEvent event) {
					    try {
						    int index = dbList.getSelectedIndex();
						    String line = model.get(index).toString();
						    String itemNO = line.split(" ")[0];
                             
                            rs=st.executeQuery("delete from item where itemNO = '" + itemNO + "');  
						    model.remove(index);
						    System.out.println("Deleted itemNo: " + itemNO);
								   
					    } catch (Exception e) {
						    System.out.println("nepavyko parodyti duomenu(0)");
					    }
				    }

  • 2

#3 skuskusas

skuskusas

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C++
  • Learning:C++

Posted 29 August 2012 - 04:40 AM

CC Mentor thx very much ! this post closed :)
  • 0

#4 Roger

Roger

    Skadoosh!

  • Administrator
  • 1,211 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 29 August 2012 - 07:26 AM

This topic has been marked as SOLVED. If you have a similar question or topic, you can go back to the subforum and start a new topic to continue discussions.
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.






Also tagged with one or more of these keywords: import, jpanel