0 Replies - 661 Views - Last Post: 16 July 2014 - 10:50 AM Rate Topic: -----

#1 Sylvane   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-July 14

Saving Edited Data from a JTable into a database

Posted 16 July 2014 - 10:50 AM

Please, I seriously need help, I've tried everything but in vain..I want to save the data from the table into the database, I can easily connect to the database. All I want is how to get the values from the table.Thank you in advance
I've tried this method to get the values but its not working because of the contructor error in the BgTableModel class.

Tried Method
    public void writeBgEdu()
      {
         int last=gtm.getRowCount();
         System.out.println(last);
      	
         for( int i=0;i<last;i++)
         {
            if(((String) gtm.getValueAt(i,0))!=null)
            {
               String column1= (String) gtm.getValueAt(i,0);
               String column1= (String) gtm.getValueAt(i,2);
            	String column1= (String) gtm.getValueAt(i,3);
            	String column1= (String) gtm.getValueAt(i,4);
               
               JOptionPane.showMessageDialog(null, " "+column1);
            }
         }
      }



The Table Model
   package presentation;
 
   import java.util.List;
   import javax.swing.table.AbstractTableModel;
   import javax.swing.*;
   import javax.swing.table.*;
   import java.awt.*;
 
   import business.*;
 
   public class BgTableModel extends AbstractTableModel
   {
      
      private List<GetBgEduDetails> employeeList;
     
      private final String[] columnNames = new String[] {
            "School Name", "City/Village", "From", "To"
         };
      private final Class[] columnClass = new Class[] {
         String.class, String.class, String.class, String.class
         };
         
      public BgTableModel()
      {
         setRowCount(employeeList);
      }
      
      public BgTableModel(List<GetBgEduDetails> employeeList)
      {
         this.employeeList = employeeList;
      }
     
      @Override
      public String getColumnName(int column)
      {
         return columnNames[column];
      }
   
      @Override
      public Class<?> getColumnClass(int columnIndex)
      {
         return columnClass[columnIndex];
      }
   
      @Override
      public int getColumnCount()
      {
         return columnNames.length;
      }
   
      @Override
      public int getRowCount()
      {
         return employeeList.size();
      }
      public void setRowCount(List<GetBgEduDetails> employeeList)
      {
         this.employeeList=employeeList;
      }
   
      @Override
      public Object getValueAt(int rowIndex, int columnIndex)
      {
         GetBgEduDetails row = employeeList.get(rowIndex);
         if(0 == columnIndex) {
            return row.getSName();
         }
         else if(1 == columnIndex) {
            return row.getCity();
         }
         else if(2 == columnIndex) {
            return row.getFrom();
         }
         else if(3 == columnIndex) {
            return row.getTo();
         }
         return null;
      }
      
      @Override
      public boolean isCellEditable(int rowIndex, int columnIndex)
      {
         return true;
      }
      
      @Override
      public void setValueAt(Object aValue, int rowIndex, int columnIndex)
      {
         GetBgEduDetails row = employeeList.get(rowIndex);
         if(0 == columnIndex) {
            row.setSName((String) aValue);
         }
         else if(1 == columnIndex) {
            row.setCity((String) aValue);
         }
         else if(2 == columnIndex) {
            row.setFrom((String) aValue);
         }
         else if(3 == columnIndex) {
            row.setTo((String) aValue);
         }
      }
   }


The Table Creation
         row1 = new GetBgEduDetails("", "", "", "");
         GetBgEduDetails row2 = new GetBgEduDetails("", "", "", "");
         GetBgEduDetails row3 = new GetBgEduDetails("", "","", "");
         GetBgEduDetails row4 = new GetBgEduDetails("", "","", "");
         GetBgEduDetails row5 = new GetBgEduDetails("", "","", "");
         
        //build the list
         List<GetBgEduDetails> employeeList = new ArrayList<GetBgEduDetails>();
         employeeList.add(row1);
         employeeList.add(row2);
         employeeList.add(row3);
         employeeList.add(row4);
         employeeList.add(row5);
         
        //create the model
         BgTableModel model = new BgTableModel(employeeList);
        //create the table
         table = new JTable(model);
         JScrollPane jsp = new JScrollPane(table);
         JTableHeader header = table.getTableHeader();
         header.setBackground(Color.red);
         //table.setBackground(Color.red);
         
         table.getTableHeader().setFont(new Font("times new roman", Font.BOLD, 16));
         //table.getRow().setFont(new Font("times new roman", Font.BOLD, 16));
         //int gapWidth = 10;
         //int gapHeight = 5;
         //table.setIntercellSpacing(new Dimension(gapWidth, gapHeight));
         table.setRowHeight(30);


The Business side of the Table (Mutators)
   package business;
 
   public class GetBgEduDetails
   {
      private String sName;
      private String city;
      private String from;
      private String to;
   
      public GetBgEduDetails(String sName, String city, String from, String to)
      {
         this.sName = sName;
         this.city = city;
         this.from = from;
         this.to = to;
      }
    /***************School Name Setters********/
      public void setSName(String sName)
      {
         this.sName = sName;
      }
      public String getSName()
      {
         return sName;
      }
   /***************City Name Setters********/
      public void setCity(String city)
      {
         this.city = city;
      }
      public String getCity()
      {
         return city;
      }
   
   /***************School Name Setters********/
      
      public void setFrom(String from)
      {
         this.from = from;
      }
      public String getFrom()
      {
         return from;
      }
   /***************School Name Setters********/
      
      public void setTo(String to)
      {
         this.to = to;
      }
      public String getTo()
      {
         return to;
      }
   
   }



Is This A Good Question/Topic? 0
  • +

Page 1 of 1