i've created a database project in java in which a jtable is used to display the contents of the database.......i've also created an option for insertion of values through a textfield by button actionperformed into the database ......and now when i inserts a particular values through the textfield and when i press the button the corresponding values is inserted and is been displayed at the jtable at that instant itself, but the problem is that when i simply click or move the mouse across the jtable the updated values disappears.......but when i execute the very next time the previously inserted value can be seen ...........can anyone tell me what the error is this............

the below code is the button action performed code

private void submitActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        int flag=0;
     Connection co;
    try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        co = DriverManager.getConnection("jdbc:odbc:hh");
        Statement st1; 
        ResultSet result;
        st1 = co.createStatement();
        String depart = (String) dept1.getSelectedItem();
        result=st1.executeQuery("select count(*) from rooms");
        result.next();
        int cn = result.getInt(1);
        result=st1.executeQuery("select room_number from rooms"); 
        String room=room_no1.getText();
        while(result.next())
        {
            if(room.equals(result.getString("room_number")))
            {
            JOptionPane.showMessageDialog(this, "ROOM NUMBER ALREADY EXISTS!!", "ERROR.", JOptionPane.ERROR_MESSAGE);
            flag=1;
            }  
        }
 if(flag==0)
        {
        cn++; 
        st1.executeUpdate("insert into rooms (room_number, serial, depart) values('"+room_no1.getText()+"','"+cn+"','"+depart+"')");
        JOptionPane.showMessageDialog(this, "ROOM NUMBER ADDED SUCCESSFULLY!!", "SUCCESS", JOptionPane.INFORMATION_MESSAGE);
        room_no1.setText("");
        dept1.select("Select");
        }
        st1.close();
        } catch (Exception ex) {System.out.print(ex);} 

updatess(); //Function call

    }

the below code is the function definition for updatess

public void updatess()  //function definition
{
    JTable teachers;
    
    COUNT=0;c=1;
   
        int wd=0;
        String rooms_no[] = null,ser[] = null,depart[] = null;
        setLayout(new FlowLayout());
        Connection co;
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        co = DriverManager.getConnection("jdbc:odbc:hh");
        Statement st1; 
        ResultSet result;
        st1 = co.createStatement();
        result=st1.executeQuery("select count(*) from rooms");
        result.next();
        wd = result.getInt(1);
        wd++;
        System.out.println("ws:"+wd);
        SERIAL=new String[wd];IT=new int[wd];CS=new int[wd];ME=new int[wd];EE=new int[wd];EC=new int[wd];CE=new int[wd];NA=new int[wd];OT=new int[wd];ROOMS=new int[wd];
        DEPART=new String[wd];
       
        rooms_no=new String[wd];
        depart=new String[wd];
        ser=new String[wd];
        result=st1.executeQuery("select * from rooms"); 
        int i=0;
        while(result.next())
        {
         rooms_no[i]=result.getString("room_number");  
         ser[i]=result.getString("serial"); 
         depart[i]=result.getString("depart"); 
         i++;                 
        }
      
        int count_it=0,count_cs=0,count_ce=0,count_ee=0,count_ec=0,count_na=0,count_me=0;
        for(int j=0;j<wd;j++)
        {
          if("Information Technology".equals(depart[j]))
          {
              IT[count_it]=Integer.parseInt(rooms_no[j]);
              java.util.Arrays.sort(IT);
              SERIAL[j]=""+c++;
          count_it++;
          }
          if("Computer Science".equals(depart[j]))
          {
              CS[count_cs]=Integer.parseInt(rooms_no[j]);
              java.util.Arrays.sort(CS);
              SERIAL[j]=""+c++;
          count_cs++;
          }
          if("Mechanical".equals(depart[j]))
          {
              ME[count_me]=Integer.parseInt(rooms_no[j]);
              java.util.Arrays.sort(ME);
              SERIAL[j]=""+c++;
          count_me++;
          }
          if("Electrical & Electronics".equals(depart[j]))
          {
              EE[count_ee]=Integer.parseInt(rooms_no[j]);
              java.util.Arrays.sort(EE);
              SERIAL[j]=""+c++;
          count_ee++;
          }
          if("Electronics & Communication".equals(depart[j]))
          {
              EC[count_ec]=Integer.parseInt(rooms_no[j]);
              java.util.Arrays.sort(EC);
              SERIAL[j]=""+c++;
          count_ec++;
          }
          if("Civil".equals(depart[j]))
          {
              CE[count_ce]=Integer.parseInt(rooms_no[j]);
              java.util.Arrays.sort(CE);
              SERIAL[j]=""+c++;
          count_ce++;
          }
          if("N/A".equals(depart[j]))
          {
              NA[count_na]=Integer.parseInt(rooms_no[j]);
              java.util.Arrays.sort(NA);
              SERIAL[j]=""+c++;
          count_na++;
          }
         
        } 
    }catch(Exception e){System.out.println(e);}
        for(int k=0;k<wd;k++)
        {
            System.out.println("IT:"+IT[k]);
            System.out.println("CS:"+CS[k]);
            System.out.println("CE:"+CE[k]);
            System.out.println("ME:"+ME[k]);
            System.out.println("EE:"+EE[k]);
            System.out.println("EC:"+EC[k]);
            System.out.println("NA:"+NA[k]);
        }
        for(int k=0;k<wd;k++)
        {
            if(IT[k]>0)
            {
             ROOMS[COUNT]=IT[k];
             DEPART[COUNT]="Information Technology";
             COUNT++;
            }
        }
        for(int k=0;k<wd;k++)
        {
            if(CS[k]>0)
            {
             ROOMS[COUNT]=CS[k];
             DEPART[COUNT]="Computer Science";
             COUNT++;
            }
        }
        for(int k=0;k<wd;k++)
        {
            if(ME[k]>0)
            {
             ROOMS[COUNT]=ME[k];
             DEPART[COUNT]="Mechanical";
             COUNT++;
            }
        }
        for(int k=0;k<wd;k++)
        {
            if(EE[k]>0)
            {
             ROOMS[COUNT]=EE[k];
             DEPART[COUNT]="Electrical & Electronics";
             COUNT++;
            }
        }
        for(int k=0;k<wd;k++)
        {
            if(EC[k]>0)
            {
             ROOMS[COUNT]=EC[k];
             DEPART[COUNT]="Electronics & Communication";
             COUNT++;
            }
        }
        for(int k=0;k<wd;k++)
        {
            if(CE[k]>0)
            {
             ROOMS[COUNT]=CE[k];
             DEPART[COUNT]="Civil";
             COUNT++;
            }
        }
        for(int k=0;k<wd;k++)
        {
            if(NA[k]>0)
            {
             ROOMS[COUNT]=NA[k];
             DEPART[COUNT]="N/A";
             COUNT++;
            }
        }

        String []columnNames={"<html><b><font face=Trebuchet MS color=black size=4>SERIAL NUMBER</font></b></html>","<html><b><font face=Trebuchet MS color=black size=4>ROOM NUMBER</font></b></html>","<html><b><font face=Trebuchet MS color=black size=4>DEPARTMENT</font></b></html>"};
 
         for(int i=0;i<wd;i++)
         {
           for(int j=0;j<3;j++)
           {
            if(j==0)
            data[i][j]=SERIAL[i];
            if(j==1)
            data[i][j]=ROOMS[i];
            if(j==2)
            data[i][j]=DEPART[i];
           }
         }   

      teachers=new JTable(data,columnNames)
              {
            @Override
  public boolean isCellEditable(int rowIndex, int colIndex) {
  return false; //Disallow the editing of any cell
  }
        
          
  public boolean setColumnSelectionAllowed()
  {
     return false; 
  }

// ... Column Packing code HERE

      };
  
      JTableHeader header = teachers.getTableHeader();
      header.setBackground(Color.yellow);
      teachers.setRowHeight(25);
      teachers.setDragEnabled(false);
      teachers.setPreferredScrollableViewportSize(new Dimension(500,50));
      teachers.setFillsViewportHeight(false);
      teachers.setFocusable(false);
      teachers.setRowSelectionAllowed(false);
      teachers.setColumnSelectionAllowed(false);
      teachers.setCellSelectionEnabled(false);
      JScrollPane jsp=new JScrollPane(teachers);
      jsp.reshape(50,35,510,622);
      add(jsp);
      jsp.setVisible(true);      
}

Hi nidhish
I have one suggestion, i think it is better to use TableModel for your
Jtable when you need to change your table data dynamically. According to your code,
you are reconstructing your table and adding into container again and again. It looks like giving more weightage for your container too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.