user1616277
user1616277

Reputation: 35

resultset is closed error in java

In my stand-alone application using Java Swing, I am traversing the Excel sheet (backend data) using ResultSet. But after executing the insert query, when I try to traverse the records using ResultSet again it shows 'ResultSet is closed'. No operations can be performed after that.
What is the error? I cannot find it. Please let me know. I am a newbie.. so please explain in simple terms. I am grateful for your help. Thanks :)

public sowui(String s)
{


    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
        con=DriverManager.getConnection("jdbc:odbc:test");
        Statement stmt = con.createStatement();
        stmt = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );
        String sql ="SELECT * FROM [Sheet1$] ";

        rs =stmt.executeQuery(sql);
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    setTitle("MANAGEMENT OF STATEMENT OF WORKS");
    setLayout(null);
    l1=new JLabel("CONTRACT REFERENCE NUMBER");
    t1=new JTextField(20);


    l1.setBounds(50,100,250,50);
    t1.setBounds(300,110,135,30);
    t1.addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent e) {
            String input=t1.getText();
            Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
            Matcher m = p.matcher(input);
            if (m.find()) {
                t1.setText(null);
                JOptionPane.showMessageDialog(null, "Please enter only numbers");
            }
        } });    
    l1m=new JLabel("*");
    l1m.setBounds(260,100,10,50);
    l1m.setForeground(Color.RED);

    l2=new JLabel("CR NUMBER");
    final String [] crn = {"N/A"};
    c6 =new JComboBox(crn);
    c6.setEditable(true);
    c6.addActionListener(new ActionListener() {            

        public void actionPerformed(ActionEvent e) {                
            if (e.getActionCommand().equals("comboBoxEdited")) {                   
                System.out.println("Adding new player!");                   
                c6.insertItemAt(c6.getSelectedItem(),0);               
            }             
        }         
    }); 

    l2.setBounds(50,150,250,50);
    c6.setBounds(300,160,135,30);

    l2m=new JLabel("*");
    l2m.setBounds(130,150,10,50);
    l2m.setForeground(Color.RED);

    l3=new JLabel("SOW NAME");
    t3=new JTextField(20);
    t3.addKeyListener(new KeyAdapter()
    {
        public void keyTyped(KeyEvent e) {
            String input=t3.getText();
            Pattern p = Pattern.compile("[0-9]");
            Matcher m = p.matcher(input);
            if (m.find())
            {
                t3.setText(null);
                JOptionPane.showMessageDialog(null,"Enter only characters A-Z or a-z");

            }
        }
    });

    l3.setBounds(50,200,250,50);
    t3.setBounds(300,210,135,30);

    l3m=new JLabel("*");
    l3m.setBounds(120,200,10,50);
    l3m.setForeground(Color.RED);

    l4=new JLabel("PARTNER SERVICE MANAGER");
    t4=new JTextField(20);
    t4.addKeyListener(new KeyAdapter()
    {
        public void keyTyped(KeyEvent e) {
            String input=t4.getText();
            Pattern p = Pattern.compile("[0-9]");
            Matcher m = p.matcher(input);
            if (m.find())
            {
                t4.setText(null);
                JOptionPane.showMessageDialog(null,"Enter only characters A-Z or a-z");

            }
        }
    });

    l4.setBounds(50,250,250,50);
    t4.setBounds(300,260,135,30);

    l4m=new JLabel("*");
    l4m.setBounds(230,250,10,50);
    l4m.setForeground(Color.RED);


    l5=new JLabel("LBG SERVICE MANAGER");
    t5=new JTextField(20);
    t5.addKeyListener(new KeyAdapter()
    {
        public void keyTyped(KeyEvent e) {
            String input=t5.getText();
            Pattern p = Pattern.compile("[0-9]");
            Matcher m = p.matcher(input);
            if (m.find())
            {
                t5.setText(null);
                JOptionPane.showMessageDialog(null,"Enter only characters A-Z or a-z");

            }
        }
    });

    l5.setBounds(50,300,250,50);
    t5.setBounds(300,310,135,30);

    l5m=new JLabel("*");
    l5m.setBounds(200,300,10,50);
    l5m.setForeground(Color.RED);


    l6=new JLabel("TYPE OF CONTRACT");
    final String [] tct = {"SOW"};
    c4 =new JComboBox(tct);
    c4.setEditable(true);
    c4.addActionListener(new ActionListener() {            

        public void actionPerformed(ActionEvent e) {                
            if (e.getActionCommand().equals("comboBoxEdited")) {                   
                System.out.println("Adding new player!");                   
                c4.insertItemAt(c4.getSelectedItem(),0);               
            }             
        }         
    }); 

    l6.setBounds(50,350,250,50);
    c4.setBounds(300,360,135,30);

    l6m=new JLabel("*");
    l6m.setBounds(180,350,10,50);
    l6m.setForeground(Color.RED);

    l7=new JLabel("VALUE OF CONTRACT");
    t7=new JTextField(20);
    t7.addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent e) {
            String input=t7.getText();
            Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
            Matcher m = p.matcher(input);
            if (m.find()) {
                t7.setText(null);
                JOptionPane.showMessageDialog(null, "Please enter only numbers");
            }
        } });                
    l7.setBounds(50,400,250,50);
    t7.setBounds(300,410,135,30);

    l7m=new JLabel("*");
    l7m.setBounds(190,400,10,50);
    l7m.setForeground(Color.RED);

    l8=new JLabel("REMARKS");
    t8=new TextArea(15,40);
    l8.setBounds(50,450,250,50);
    t8.setBounds(300,460,200,80);

    l9=new JLabel("WORK STATUS");
    final String [] workstatus = {"Live","Pipeline","Working at Risk"};
    c1 =new JComboBox(workstatus);
    c1.setEditable(true);
    c1.addActionListener(new ActionListener() {            

        public void actionPerformed(ActionEvent e) {                
            if (e.getActionCommand().equals("comboBoxEdited")) {                   
                System.out.println("Adding new player!");                   
                c1.insertItemAt(c1.getSelectedItem(),0);               
            }             
        }         
    }); 

    l9.setBounds(500,100,250,50);
    c1.setBounds(750,110,135,30);

    l9m=new JLabel("*");
    l9m.setBounds(600,100,10,50);
    l9m.setForeground(Color.RED);

    l10=new JLabel("CONTRACT APPROVAL");
    final String [] cal = {"Fully signed off","WithPlatform","with partner for signoff"};
    c2 =new JComboBox(cal);
    c2.setEditable(true);
    c2.addActionListener(new ActionListener() {            

        public void actionPerformed(ActionEvent e) {                
            if (e.getActionCommand().equals("comboBoxEdited")) {                   
                System.out.println("Adding new player!");                   
                c2.insertItemAt(c2.getSelectedItem(),0);               
            }             
        }         
    }); 
    l10.setBounds(500,150,250,50);
    c2.setBounds(750,160,135,30);

    l10m=new JLabel("*");
    l10m.setBounds(640,150,10,50);
    l10m.setForeground(Color.RED);


    l11=new JLabel("PLATFORM");
    final String [] PF = {"ADM-WPM","ADM-ALIS","ADM-SWeC","ADM-PAS","ADM-BSM","ADM-RAS","ADM-LP&I"};
    c5 =new JComboBox(PF);
    c5.setEditable(true);
    c5.addActionListener(new ActionListener() {            

        public void actionPerformed(ActionEvent e) {                
            if (e.getActionCommand().equals("comboBoxEdited")) {                   
                System.out.println("Adding new player!");                   
                c5.insertItemAt(c5.getSelectedItem(),0);               
            }             
        }         
    }); 
    l11.setBounds(500,200,250,50);
    c5.setBounds(750,210,135,30);

    l11m=new JLabel("*");
    l11m.setBounds(570,200,10,50);
    l11m.setForeground(Color.RED);


    l12=new JLabel("COMMERCIAL MODEL");
    final String [] cml = {"T&M","FP","PSA"};
    c3 =new JComboBox(cml);
    c3.setEditable(true);
    c3.addActionListener(new ActionListener() {            

        public void actionPerformed(ActionEvent e) {                
            if (e.getActionCommand().equals("comboBoxEdited")) {                   
                System.out.println("Adding new player!");                   
                c3.insertItemAt(c3.getSelectedItem(),0);               
            }             
        }         
    }); 
    l12.setBounds(500,250,250,50);
    c3.setBounds(750,260,135,30);

    l12m=new JLabel("*");
    l12m.setBounds(630,250,10,50);
    l12m.setForeground(Color.RED);

    l13=new JLabel("START DATE");
    t13=new JTextField(20);
    l13.setBounds(500,300,250,50);
    t13.setBounds(750,310,135,30);
    t13.setEditable(false);

    l13m=new JLabel("*");
    l13m.setBounds(580,300,10,50);
    l13m.setForeground(Color.RED);



    l14=new JLabel("END DATE");
    t14=new JTextField(20);
    l14.setBounds(500,350,250,50);
    t14.setBounds(750,360,135,30);
    t14.setEditable(false);

    l14m=new JLabel("*");
    l14m.setBounds(570,350,10,50);
    l14m.setForeground(Color.RED);


    l15=new JLabel("CUMULATIVE VALUE OF CONTRACT");
    t15=new JTextField(20);
    t15.addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent e) {
            String input=t15.getText();
            Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
            Matcher m = p.matcher(input);
            if (m.find()) {
                t15.setText(null);
                JOptionPane.showMessageDialog(null, "Please enter only numbers");
            }
        }});       

    l15.setBounds(500,400,250,50);
    t15.setBounds(750,410,135,30);

    l15m=new JLabel("*");
    l15m.setBounds(710,400,10,50);
    l15m.setForeground(Color.RED);

    l16=new JLabel("SOW TRACKER");
    l16.setBounds(400,50,250,50);
    l16.setFont(new Font("TIMES NEW ROMAN", Font.BOLD, 28));


    firstb=new JButton("FIRST");
    firstb.setBounds(200,600,100,40);

    prev=new JButton("PREVIOUS");
    prev.setBounds(400,600,100,40);

    nextb=new JButton("NEXT");
    nextb.setBounds(600,600,100,40);

    lastb=new JButton("LAST");
    lastb.setBounds(800,600,100,40);

    newb=new JButton("NEW");
    newb.setBounds(200,650,100,40);

    save=new JButton("SAVE");
    save.setBounds(400,650,100,40);

    exitb = new JButton("EXIT");
    exitb.setBounds(600, 650,100,40);

    searchb=new JButton("SEARCH");
    searchb.setBounds(800,650,100,40);

    b9=new JButton("..");
    b9.setBounds(900,315,20,20);

    b10=new JButton("..");
    b10.setBounds(900,365,20,20);



    add(l1);
    add(t1);
    add(l1m);

    add(l2);
    add(c6);
    add(l2m);

    add(l3);
    add(t3);
    add(l3m);

    add(l4);
    add(t4);
    add(l4m);

    add(l5);
    add(t5);
    add(l5m);

    add(l6);
    add(c4);
    add(l6m);

    add(l7);
    add(t7);
    add(l7m);

    add(l8);
    add(t8);

    add(l9);
    add(c1);
    add(l9m);


    add(l10);
    add(c2);
    add(l10m);

    add(l11);
    add(c5);
    add(l11m);

    add(l12);
    add(c3);
    add(l12m);

    add(l13);
    add(t13);
    add(l13m);

    add(l14);
    add(t14);
    add(l14m);

    add(l15);
    add(t15);
    add(l15m);

    add(l16);


    add(firstb);
    firstb.addActionListener(this);

    add(prev);
    prev.addActionListener(this);

    add(nextb);
    nextb.addActionListener(this);

    add(lastb);
    lastb.addActionListener(this);

    add(newb);
    newb.addActionListener(this);

    add(save);
    save.addActionListener(this);

    add(exitb);
    exitb.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    });

    add(searchb);
    searchb.addActionListener(this);

    add(b9);
    b9.addActionListener(this);

    add(b10);
    b10.addActionListener(this);









    setSize(1000,1000);
    setVisible(true);
    setResizable(false);


}








public void actionPerformed(ActionEvent ae)
{

    if(ae.getSource()==save)
    {


        if(t1.getText().length()==0) { try{

            JOptionPane.showMessageDialog(null, "Enter CONTRACT REFERENCE NUMBER"); }catch(Exception e){

                JOptionPane.showMessageDialog(null, "JTextField is " + t1.getText()); }}


        /*else if(t2.getText().length()==0) { try{

JOptionPane.showMessageDialog(null, "Enter CR NUMBER"); }catch(Exception e){

        JOptionPane.showMessageDialog(null, "JTextField is " + t2.getText()); }}*/

        else if(t3.getText().length()==0) { try{

            JOptionPane.showMessageDialog(null, "enter text in JTextField-SOW NAME"); }catch(Exception e){

                JOptionPane.showMessageDialog(null, "JTextField is " + t3.getText()); }}

        else if(t4.getText().length()==0) { try{

            JOptionPane.showMessageDialog(null, "enter text in JTextField-PARTNER SERVICE MANAGER"); }catch(Exception e){

                JOptionPane.showMessageDialog(null, "JTextField is " + t4.getText()); }}

        else if(t5.getText().length()==0) { try{

            JOptionPane.showMessageDialog(null, "enter text in JTextField-LBG SERVICE MANAGER"); }catch(Exception e){

                JOptionPane.showMessageDialog(null, "JTextField is " + t5.getText()); }}

        else if(t7.getText().length()==0) { try{

            JOptionPane.showMessageDialog(null, "Enter VALUE OF CONTRACT"); }catch(Exception e){

                JOptionPane.showMessageDialog(null, "JTextField is " + t7.getText()); }}

        /*else if(t11.getText().length()==0) { try{

JOptionPane.showMessageDialog(null, "enter text in JTextField-PLATFORM"); }catch(Exception e){

        JOptionPane.showMessageDialog(null, "JTextField is " + t11.getText()); }}

else if(t12.getText().length()==0) { try{

JOptionPane.showMessageDialog(null, "enter text in JTextField-COMMERCIAL MODEL"); }catch(Exception e){

        JOptionPane.showMessageDialog(null, "JTextField is " + t12.getText()); }}*/

        else if(t13.getText().length()==0) { try{

            JOptionPane.showMessageDialog(null, "Enter START DATE"); }catch(Exception e){

                JOptionPane.showMessageDialog(null, "JTextField is " + t13.getText()); }}

        else if(t14.getText().length()==0) { try{

            JOptionPane.showMessageDialog(null, "Enter END DATE"); }catch(Exception e){

                JOptionPane.showMessageDialog(null, "JTextField is " + t14.getText()); }}

        else if(t15.getText().length()==0) { try{

            JOptionPane.showMessageDialog(null, "Enter-CUMULATIVE VALUE OF CONTRACT"); }catch(Exception e){

                JOptionPane.showMessageDialog(null, "JTextField is " + t15.getText()); }} 


        String str1=t13.getText();
        String str2=t14.getText();
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");

        Date date1 = null;
        try {
            date1 = (Date) df.parse(str1);
        } catch (ParseException e) {

        }

        Date date2 = null;
        try {
            date2 = (Date) df.parse(str2);
        } catch (ParseException e) {

        } 

        if (date1.after(date2)) try{
            JOptionPane.showMessageDialog(null, "Start Date must be earlier than the end date");


        }catch(Exception e){} 

        if(ae.getSource()==save) 
        {
            try
            {

                String value1=t1.getText();

                String value2=(String)c6.getSelectedItem();

                String value3=t3.getText();

                String value4=t4.getText();

                String value5=t5.getText();

                String value6 = (String) c4.getSelectedItem(); 

                String value7=t7.getText();

                String value8=t8.getText();

                String value9 = (String) c1.getSelectedItem(); 

                String value10 = (String) c2.getSelectedItem(); 

                String value11 = (String) c5.getSelectedItem(); 

                String value12 = (String) c3.getSelectedItem(); 

                String value13=t13.getText();

                String value14=t14.getText();

                String value15=t15.getText();


                Statement st=con.createStatement();
                System.out.println(value1);
                //  String q = "insert into [sheet1$] (CRN,CR_NUM,SOW_N,PSM,LBG,T_CONTRACT,V_CONTRACT,REMARKS,STATUS,APPROVAL,PLATFORM,MODEL,START_D,END_D,CUM_VALUE) values ('12345','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"','"+value7+"','"+value8+"','"+value9+"','"+value10+"','"+value11+"','"+value12+"','"+value13+"','"+value14+"','"+value15+"')";
                String q = "insert into [sheet1$] values ('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"','"+value7+"','"+value8+"','"+value9+"','"+value10+"','"+value11+"','"+value12+"','"+value13+"','"+value14+"','"+value15+"')"; 
                int ds = st.executeUpdate(q);

                System.out.println("Inserted Sucessfully...!!!");
                System.out.println(ds);




            }catch(Exception e)
            {
                System.out.println(e.getMessage());
            }

            finally{
                try {
                    con.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    } 
    else if(ae.getSource()==newb)
    {
        try{

            t1.setText(null);
            c6.addItem(null);
            t3.setText(null);
            t4.setText(null);
            t5.setText(null);
            c4.addItem(null);
            t7.setText(null);
            t8.setText(null);
            c1.addItem(null);
            c2.addItem(null);
            c5.addItem(null);
            c3.addItem(null); 
            t13.setText(null);
            t14.setText(null);
            t15.setText(null); 

        }catch(Exception e){}

    }
    else if(ae.getSource()==firstb)
    {
        try {
            rs.first( );
            int id_col = rs.getInt("CRN");

            String id = Integer.toString(id_col);
            //int id_col1 = rs.getInt("CR_NUM");
            //String cr= Integer.toString(id_col1);
            String cr =rs.getString("CR_NUM");
            String sow = rs.getString("SOW_N");
            String psm = rs.getString("PSM");
            String lbg = rs.getString("LBG");
            String toc = rs.getString("T_CONTRACT");
            String rm = rs.getString("REMARKS");

            String wks = rs.getString("WORK_ST");

            String cap = rs.getString("APPROVAL");
            String pfm = rs.getString("PLATFORM");
            String cmm = rs.getString("MODEL");

            String std = rs.getString("START_D");
            String etd= rs.getString("END_D");

            int id_col2 = rs.getInt("V_CONTRACT");
            String voc= Integer.toString(id_col2);


            int id_col3 = rs.getInt("CUM_VALUE");
            String cvc = Integer.toString(id_col3);
            t1.setText(id);
            //c6.addItem(cr);
            t3.setText(sow);
            t4.setText(psm);
            t5.setText(lbg);
            //c4.addItem(toc);
            t7.setText(voc);
            t8.setText(rm);
            c1.setSelectedItem(wks);
            c2.setSelectedItem(cap);
            c5.setSelectedItem(pfm);
            c3.setSelectedItem(cmm);
            c4.setSelectedItem(toc);
            c6.setSelectedItem(cr);



            //c1.addItem(wks);
            //c2.addItem(cap);
            //c5.addItem(pfm);
            //c3.addItem(cmm);
            t13.setText(std);
            t14.setText(etd);
            t15.setText(cvc);

            System.out.println(wks);


        }
        catch (SQLException err) {
            JOptionPane.showMessageDialog(sowui.this, err.getMessage());
        }
        try{
        stmt.close();
        }catch(SQLException a) {}
    }

    else if(ae.getSource()==nextb)
    {
        try {
            if ( rs.next( ) ) {
                int id_col = rs.getInt("CRN");

                String id = Integer.toString(id_col);
                //int id_col1 = rs.getInt("CR_NUM");
                //String cr= Integer.toString(id_col1);
                String cr =rs.getString("CR_NUM");
                String sow = rs.getString("SOW_N");
                String psm = rs.getString("PSM");
                String lbg = rs.getString("LBG");
                String toc = rs.getString("T_CONTRACT");
                String rm = rs.getString("REMARKS");

                String wks = rs.getString("WORK_ST");

                String cap = rs.getString("APPROVAL");
                String pfm = rs.getString("PLATFORM");
                String cmm = rs.getString("MODEL");

                String std = rs.getString("START_D");
                String etd= rs.getString("END_D");

                int id_col2 = rs.getInt("V_CONTRACT");
                String voc= Integer.toString(id_col2);


                int id_col3 = rs.getInt("CUM_VALUE");
                String cvc = Integer.toString(id_col3);
                t1.setText(id);
                //c6.addItem(cr);
                t3.setText(sow);
                t4.setText(psm);
                t5.setText(lbg);
                //c4.addItem(toc);
                t7.setText(voc);
                t8.setText(rm);
                //c1.addItem(wks);
                //c2.addItem(cap);
                //c5.addItem(pfm);
                //c3.addItem(cmm);
                c1.setSelectedItem(wks);
                c2.setSelectedItem(cap);
                c5.setSelectedItem(pfm);
                c3.setSelectedItem(cmm);
                c4.setSelectedItem(toc);
                c6.setSelectedItem(cr);
                t13.setText(std);
                t14.setText(etd);
                t15.setText(cvc);

            }

            else {
                rs.previous( );
                JOptionPane.showMessageDialog(sowui.this, "End of File");
            }
        }
        catch (SQLException err) {
            JOptionPane.showMessageDialog(sowui.this, err.getMessage());
        }try{
        stmt.close();
        }catch(SQLException a) {}
    }
    else if(ae.getSource()==prev)
    {
        try {
            if ( rs.previous( ) ) {
                int id_col = rs.getInt("CRN");

                String id = Integer.toString(id_col);
                //int id_col1 = rs.getInt("CR_NUM");
                //String cr= Integer.toString(id_col1);
                String cr =rs.getString("CR_NUM");
                String sow = rs.getString("SOW_N");
                String psm = rs.getString("PSM");
                String lbg = rs.getString("LBG");
                String toc = rs.getString("T_CONTRACT");
                String rm = rs.getString("REMARKS");

                String wks = rs.getString("WORK_ST");

                String cap = rs.getString("APPROVAL");
                String pfm = rs.getString("PLATFORM");
                String cmm = rs.getString("MODEL");

                String std = rs.getString("START_D");
                String etd= rs.getString("END_D");

                int id_col2 = rs.getInt("V_CONTRACT");
                String voc= Integer.toString(id_col2);


                int id_col3 = rs.getInt("CUM_VALUE");
                String cvc = Integer.toString(id_col3);
                t1.setText(id);
                //c6.addItem(cr);
                t3.setText(sow);
                t4.setText(psm);
                t5.setText(lbg);
                //c4.addItem(toc);
                t7.setText(voc);
                t8.setText(rm);
                //c1.addItem(wks);
                //c2.addItem(cap);
                //c5.addItem(pfm);
                //c3.addItem(cmm);
                c1.setSelectedItem(wks);
                c2.setSelectedItem(cap);
                c5.setSelectedItem(pfm);
                c3.setSelectedItem(cmm);
                c4.setSelectedItem(toc);
                c6.setSelectedItem(cr);
                t13.setText(std);
                t14.setText(etd);
                t15.setText(cvc);

            }

            else {
                rs.next( );
                JOptionPane.showMessageDialog(sowui.this, "Top of File");
            }
        }
        catch (SQLException err) {
            JOptionPane.showMessageDialog(sowui.this, err.getMessage());
        }try{
        stmt.close();
        }catch(SQLException a) {}
    }
    if(ae.getSource()==lastb)
    {
        try {
            rs.last( ); 
            int id_col = rs.getInt("CRN");

            String id = Integer.toString(id_col);
            //int id_col1 = rs.getInt("CR_NUM");
            //String cr= Integer.toString(id_col1);
            String cr =rs.getString("CR_NUM");
            String sow = rs.getString("SOW_N");
            String psm = rs.getString("PSM");
            String lbg = rs.getString("LBG");
            String toc = rs.getString("T_CONTRACT");
            String rm = rs.getString("REMARKS");

            String wks = rs.getString("WORK_ST");

            String cap = rs.getString("APPROVAL");
            String pfm = rs.getString("PLATFORM");
            String cmm = rs.getString("MODEL");

            String std = rs.getString("START_D");
            String etd= rs.getString("END_D");

            int id_col2 = rs.getInt("V_CONTRACT");
            String voc= Integer.toString(id_col2);


            int id_col3 = rs.getInt("CUM_VALUE");
            String cvc = Integer.toString(id_col3);
            t1.setText(id);
            //c6.addItem(cr);
            t3.setText(sow);
            t4.setText(psm);
            t5.setText(lbg);
            //c4.addItem(toc);
            t7.setText(voc);
            t8.setText(rm);
            //c1.addItem(wks);
            //c2.addItem(cap);
            //c5.addItem(pfm);
            //c3.addItem(cmm);
            c1.setSelectedItem(wks);
            c2.setSelectedItem(cap);
            c5.setSelectedItem(pfm);
            c3.setSelectedItem(cmm);
            c4.setSelectedItem(toc);
            c6.setSelectedItem(cr);
            t13.setText(std);
            t14.setText(etd);
            t15.setText(cvc);



        }
        catch (SQLException err) {
            JOptionPane.showMessageDialog(sowui.this, err.getMessage());
        }try{
        stmt.close();
        }catch(SQLException a) {}
    }

Upvotes: 0

Views: 710

Answers (2)

Robin
Robin

Reputation: 36611

Next to what has already been set in the comments, some other remarks about the code you posted:

  • Get rid of the setLayout( null ) call and all related calls (setBounds and the like). Learn to use Layout Managers instead.
  • Swing components should be created/accessed/modified on the Event Dispatch Thread. This thread is responsible for the UI, and should not be used for long-running tasks (as dealing with ResultSet instances). See the Concurrency in Swing tutorial for more info
  • Validation of the input of a JTextComponent (in your code the variable is called t1 and t3) should not use a KeyListener. Use a DocumentFilter instead, or in case of a text field consider using a JFormattedTextField. For example drag-and-drop of input in your text field will completely bypass your KeyListener.
  • Note that most of the events your receive in your listener have a getSource() method which allows to retrieve the source component on which the event occurred. This can avoid a lot of code duplication, as you have multiple listeners which do exactly the same thing. The only part in which they differ are the source component, which can be retrieved from the event. Another option is to switch from an anonymous class to an inner class and pass the information you need in the constructor.
  • Simply surrounding random pieces of code with try{} catch ( Exception e ) and then doing nothing of error handling is asking for weird bugs in the future. Either you correctly handle the Exception, or you throw it further.
  • Empty catch blocks are even worse. Now you silently ignore the error and the code goes further like nothing happens. Who knows in what state your program will end

Upvotes: 3

eis
eis

Reputation: 53502

Resultset is bound to your statement and your connection. You should read the contents of your ResultSet while you still have a connection to the database open, then close the resultset, statement, and after that the connection.

Note that you don't have to read the results to UI - you can read them into some internal collection, too, and later update the UI if needed. Just get them out of the result set while you still have the references to mentioned objects in place.

Upvotes: 2

Related Questions