Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: hello i am new i java please some help!!

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question hello i am new i java please some help!!

    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();
    }
    }
    Last edited by skuskusas; August 29th, 2012 at 06:16 AM. Reason: fix code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,646
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: hello i am new i java please some help!!

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Also posted at http://www.java-forums.org/awt-swing...tml#post299134
    Last edited by Norm; August 29th, 2012 at 07:44 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Tjstretch (August 29th, 2012)