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

Thread: Updating TableModel if changing data on DataBase(MySQL)

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Updating TableModel if changing data on DataBase(MySQL)

    Hi everyone!. I have a problem with the table (JTable). I created a table model:
    import java.util.ArrayList;
     
    import javax.swing.table.AbstractTableModel;
     
    @SuppressWarnings("serial")
    public class MyListTableModel extends AbstractTableModel {
     
    	private ArrayList<String>				columnNames	= new ArrayList<String>();
    	private ArrayList<ArrayList<Object>>	data		= new ArrayList<ArrayList<Object>>();
     
    	public MyListTableModel(ArrayList<ArrayList<Object>> data, ArrayList<String> columnNames) {
    		this.columnNames = columnNames;
    		this.data = data;
    		fireTableDataChanged();
    	}
     
    	@SuppressWarnings({ "unchecked", "rawtypes" })
    	public Class getColumnClass(int c) {
    		return getValueAt(0, c).getClass();
    	}
     
    	public int getColumnCount() {
    		return columnNames.size();
    	}
     
    	public String getColumnName(int col) {
    		return columnNames.get(col);
    	}
     
    	public int getRowCount() {
    		synchronized (data) {
    			return data.size();
    		}
    	}
     
    	public Object getValueAt(int row, int col) {
    		synchronized (data) {
    			return data.get(row).get(col);
    		}
    	}
     
    	public boolean isCellEditable(int row, int col) {
    		return false;
    	}
     
    	public void setValueAt(String obj, int row, int col) {
    		synchronized (data) {
    			data.get(row).set(col, obj);
    			fireTableDataChanged();
    		}
    	}
    }
    and develop JTable from this model:
    	private class CalcTableUI extends JScrollPane {
    		CalcTableUI() throws Exception {
    			calcTbl = new JTable(new MyListTableModel(createArrayListData(SQLQueriesBank.getCalcPosting), createArrayListColumns(SQLQueries.getPostTitle)));
    			setViewportView(calcTbl);
    			revalidate();
    		}
    	}

    public static ArrayList<ArrayList<Object>> createArrayListData(String sqlQuery) throws Exception {
    		Connection conn = getConnectToMySQLDB();
    		ArrayList<ArrayList<Object>> dataArLst = new ArrayList<ArrayList<Object>>();
    		try {
    			PreparedStatement st = conn.prepareStatement(sqlQuery);
    			ResultSet rs = st.executeQuery();
    			ResultSetMetaData md = rs.getMetaData();
    			int columnCount = md.getColumnCount();
    			while (rs.next()) {
    				ArrayList<Object> rowArr = new ArrayList<Object>(columnCount);
    				for (int i = 1; i <= columnCount; i++) {
    					rowArr.add(rs.getString(i));
    				}
    				rowArr.trimToSize();
    				dataArLst.add(rowArr);
    			}
    			dataArLst.trimToSize();
    		} catch (Exception e) {
    			JOptionPane.showMessageDialog(new JDialog(), e);
    		} finally {
    			if (conn != null)
    				conn.close();
    		}
    		return dataArLst;
    	}
    public static ArrayList<String> createArrayListColumns(String sqlQuery) throws Exception {
    		Connection conn = getConnectToMySQLDB();
    		ArrayList<String> columnsArr = new ArrayList<String>();
    		try {
    			PreparedStatement st = conn.prepareStatement(sqlQuery);
    			ResultSet rs = st.executeQuery();
    			while (rs.next()) {
    				columnsArr.add(rs.getString(1));
    			}
    			columnsArr.trimToSize();
    		} catch (Exception e) {
    			JOptionPane.showMessageDialog(new JDialog(), e);
    		} finally {
    			if (conn != null)
    				conn.close();
    		}
    		return columnsArr;
    	}
    but when i changing DB tables its not effect to the my CalcTableUI, when i rerun my app, its shows changing, why. where i wrong and/or what is missing ?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Updating TableModel if changing data on DataBase(MySQL)

    Are you saying you're updating the data stored in the model but the JTable isn't showing the change?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Updating TableModel if changing data on DataBase(MySQL)

    You shouldn't expect the table to change, as when you load the data into the table you have 2 copies of the data. How does one know to change when the other changes? This problem is a lot harder to solve than one might think, one solution would be to build a notification system (which in its simplest form would require an inner tier to keep track and notify all clients of a datachange, and all changes go through this tier). This is not trivial

Similar Threads

  1. Replies: 6
    Last Post: September 5th, 2011, 10:02 AM
  2. Error in updating database
    By surendran610 in forum Web Frameworks
    Replies: 2
    Last Post: August 12th, 2011, 04:33 AM
  3. [SOLVED] Passing data to a MySQL database
    By JDyson in forum JDBC & Databases
    Replies: 1
    Last Post: December 1st, 2010, 01:31 PM
  4. [SOLVED] Get Data From Database MySQL using ComboBox
    By Simple in forum Java Theory & Questions
    Replies: 2
    Last Post: June 4th, 2010, 02:45 AM
  5. updating database
    By gurpreetm13 in forum JDBC & Databases
    Replies: 3
    Last Post: October 9th, 2009, 11:43 AM