hello here i import from to db to JPalnel everithing its ok thats work now me need to delete selected item from 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("Dis play: "));
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();
}
}