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 8 of 8

Thread: some help in my code

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Exclamation some help in my code

    i have make that code search method is running well but update method does not work i don't know why i have an error in it (java.sql.SQLException: ResultSet is closed)
    this is my code :

     
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    public class updateDeletePro extends JFrame {
        private JButton search = new JButton();
        private JButton clear = new JButton();
        private JButton update = new JButton();
        private JLabel productNamelbl = new JLabel();
        private JLabel productIDlbl = new JLabel();
        private JLabel productQulitylbl = new JLabel();
        private JLabel productDatelbl = new JLabel();
        private JLabel productWeightlbl = new JLabel();
        private JLabel productBePricelbl = new JLabel();
        private JTextField producNametText = new JTextField();
        private JTextField SearchText = new JTextField();
        private JTextField productTextID = new JTextField();
        private JTextField productQtext = new JTextField();
        private JTextField productDateText = new JTextField();
        private JTextField productPriceText = new JTextField();
        private JTextField productWeightText = new JTextField();
        private JLabel lblll1 = new JLabel();
        private JLabel lblll3 = new JLabel();
        private JLabel lblll4 = new JLabel();  
        Connection conn = null;
        Statement state = null;
        ResultSet rs=null;
           private class ClickListener implements ActionListener {
     
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == search) {
     
                    SearchP();
            }
                else if (e.getSource() == clear) {
     
                    clear();
            }
                  else if (e.getSource() == update) {
                        up_date();
        }
           }
           }
      private class Closer implements WindowListener {
     
            @Override
            public void windowClosing(WindowEvent e) {
            }
     
            @Override
            public void windowActivated(WindowEvent e) {
            }
     
            @Override
            public void windowClosed(WindowEvent e) {
            }
     
            @Override
            public void windowDeactivated(WindowEvent e) {
            }
     
            @Override
            public void windowDeiconified(WindowEvent e) {
            }
     
            @Override
            public void windowIconified(WindowEvent e) {
            }
     
            @Override
            public void windowOpened(WindowEvent e) {
            }
      }
     
            public updateDeletePro() {
            this.setLayout(null);
            this.setFont(new Font("Arial", 1, 13));
            search.setText("Search");
            search.setBounds(new Rectangle(5, 250, 75, 25));
            search.setToolTipText("Click to Search by ID");
            clear.setText("New");
            clear.setBounds(new Rectangle(200, 250, 75, 25));
            clear.setToolTipText("Click to Search New One");
            update.setText("update");
            update.setBounds(new Rectangle(285, 250, 75, 25));
            update.setToolTipText("Click to Update");
            productNamelbl.setText("Product Name");
            productNamelbl.setBounds(new Rectangle(5, 10, 65, 25));
            productIDlbl.setText("ID");
            productIDlbl.setBounds(new Rectangle(5, 40, 65, 25));
            productQulitylbl.setText("Qulity");
            productQulitylbl.setBounds(new Rectangle(5, 75, 65, 25));
            productDatelbl.setText("Date");
            productDatelbl.setBounds(new Rectangle(5, 115, 65, 25));
            productWeightlbl.setText("Weight");
            productWeightlbl.setBounds(new Rectangle(5, 155, 65, 25));
            productBePricelbl.setText("Before Price");
            productBePricelbl.setBounds(new Rectangle(5, 190, 75, 25));
            producNametText.setBounds(new Rectangle(95, 10, 180, 25));
            productTextID.setBounds(new Rectangle(95, 45, 100, 25));
            productQtext.setBounds(new Rectangle(95, 80, 100, 25));
            SearchText.setBounds(new Rectangle(95, 250, 100, 25));
            productDateText.setBounds(new Rectangle(95, 115, 100, 25));
            productPriceText.setBounds(new Rectangle(95, 190, 100, 25));
            lblll1.setText("By Pounds");
            lblll1.setBounds(new Rectangle(205, 185, 85, 30));
            lblll1.setFont(new Font("Arial", 3, 12));
            productWeightText.setBounds(new Rectangle(95, 150, 100, 25));
            lblll3.setText("By Kg");
            lblll3.setBounds(new Rectangle(205, 150, 85, 30));
            lblll3.setFont(new Font("Arial", 3, 12));
            lblll4.setText("Day / Mon /Year");
            lblll4.setBounds(new Rectangle(205, 115, 100, 25));
            lblll4.setFont(new Font("Arial", 3, 12));
            JPanel panel=new JPanel();
            panel = (JPanel)getContentPane();
            panel.add(lblll4, null);
            panel.add(lblll3, null);
            panel.add(productWeightText, null);
            panel.add(lblll1, null);
            panel.add(productPriceText, null);
            panel.add(productDateText, null);
            panel.add(productQtext, null);
            panel.add(productTextID, null);
            panel.add(producNametText, null);
            panel.add(productBePricelbl, null);
            panel.add(productWeightlbl, null);
            panel.add(productDatelbl, null);
            panel.add(productQulitylbl, null);
            panel.add(productIDlbl, null);
            panel.add(productNamelbl, null);
            panel.add(search, null);
            panel.add(SearchText,null);
            panel.add(clear,null);
            panel.add(update,null);
            panel.setBackground(Color.WHITE);
            this.setVisible(true);
            this.setSize(375,325);
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.setResizable(false);
            this.setTitle(":: Find Product ::");
            updateDeletePro.ClickListener cl = new updateDeletePro.ClickListener();
            search.addActionListener(cl);
            clear.addActionListener(cl);
            update.addActionListener(cl);
            this.addWindowListener(new Closer());
            }
            public void SearchP()  {
             String Name =SearchText.getText();
     
             String SQL = "SELECT Product_ID,Product_Name,Product_Qulity,Product_Weight,Product_Date,Product_Price FROM ProductT WHERE Product_Name = '"+Name+"'";
                 try{
                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                 conn = DriverManager.getConnection("jdbc:odbc:SuperMarket");
                 state = conn.createStatement();
                 rs = state.executeQuery(SQL);
                 while(rs.next()){
                       producNametText.setText(rs.getString("Product_Name"));
                       productTextID.setText(rs.getString("Product_ID"));
                       productQtext.setText(rs.getString("Product_Qulity"));
                       productDateText.setText(rs.getString("Product_Date"));
                       productWeightText.setText(rs.getString("Product_Weight"));
                       productPriceText.setText(rs.getString("Product_Price")); 
                       JOptionPane.showMessageDialog(null,"Record Found");
                 }
                 conn.close();
                 state.close();
                 }
             catch (Exception er) {
                 JOptionPane.showMessageDialog(null ,"Record not Found","Warning", JOptionPane.INFORMATION_MESSAGE);
        }
            }
     
         public void up_date()
    	{	
                   String Name =SearchText.getText();
                  int PID=Integer.parseInt(productTextID.getText());
                  String PN=producNametText.getText();
    	      String PQ=productQtext.getText();
    	      int PW=Integer.parseInt(productWeightText.getText());
    	      String PD=productDateText.getText();
    	      int PP=Integer.parseInt(productPriceText.getText());
     
             String SQL = "SELECT Product_ID,Product_Name,Product_Qulity,Product_Weight,Product_Date,Product_Price FROM ProductT WHERE Product_Name = '"+Name+"'";
                 try{
                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                 conn = DriverManager.getConnection("jdbc:odbc:SuperMarket");
                 state = conn.createStatement();
                 rs = state.executeQuery(SQL);
                 while(rs.next()){
                     String str="UPDATE ProductT SET  Product_Name = '"+PN+"' ,Product_ID="+PID+", Product_Qulity= '"+PQ+"',Product_Price = "+PP+", Product_Date= '"+PD+"',Product_Weight = "+PW+"  WHERE Product_Name = '"+Name+"'" ;
    	     state.executeUpdate(str);	
                 }
                 conn.close();
                 state.close();
                 }
             catch (Exception er) {
                 JOptionPane.showMessageDialog(null ,"Record not Updated","Warning", JOptionPane.INFORMATION_MESSAGE);
                 System.out.println(er);
        }
     
    	}
     
    public void clear(){
     
            productPriceText.setText("");
            productDateText.setText("");
            productQtext.setText("");
            productTextID.setText("");
            producNametText.setText("");
            productWeightText.setText("");
            SearchText.setText("");
            productTextID.requestFocus();
    }
     
    }
    Last edited by mostafa_mohamed; September 4th, 2012 at 09:21 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: some help in my code

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    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:

    mostafa_mohamed (September 5th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: some help in my code

    do this for me please

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: some help in my code

    I can't edit your post so you will have to fix it. The ending code tag needs a / before code

    Post the full text of the error message that shows where the error happens. You need to add calls to the printStackTrace() method to the catch blocks to get the full error message.
    If you don't understand my answer, don't ignore it, ask a question.

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

    mostafa_mohamed (September 5th, 2012)

  7. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: some help in my code

    oh i get all of that but i can't know how can i solve it

    java.sql.SQLException: ResultSet is closed
    java.sql.SQLException: ResultSet is closed
    at sun.jdbc.odbc.JdbcOdbcResultSet.checkOpen(JdbcOdbc ResultSet.java:6645)
    at sun.jdbc.odbc.JdbcOdbcResultSet.next(JdbcOdbcResul tSet.java:1248)
    at supermarket.updateDeletePro.up_date(updateDeletePr o.java:200)
    at supermarket.updateDeletePro$ClickListener.actionPe rformed(updateDeletePro.java:50)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEven tMulticaster.java:289)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102 )
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 673)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: some help in my code

    You are calling the next() method on a closed result set. Where does the code close the result set?
    Make sure you don't call next() when the result set is closed.

    If you need help with SQL try asking the question in the DB section:
    http://www.javaprogrammingforums.com/jdbc-databases/
    Last edited by Norm; September 4th, 2012 at 09:39 AM.
    If you don't understand my answer, don't ignore it, ask a question.

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

    mostafa_mohamed (September 5th, 2012)

  10. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: some help in my code

    thank you very much my friend ^_^ Solved

  11. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: some help in my code

    Mostafa_Mohamed: Mark the thread as Solved.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  2. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  3. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  4. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM