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

Thread: [HELP] Refreshing JTable connected to database

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [HELP] Refreshing JTable connected to database

    What can be the problem with my code here? I'm trying to refresh/update the table connected to database.

     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.sql.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
     
    public class manipulatingData extends JPanel implements ActionListener
    {
    	private JButton btnAdd, btnEdit, btnDelete, btnSearch, btnSave, btnCancel;
    	private JFrame f;
    	private JPanel p,panel;
    	private JDialog addData;
    	private JLabel lblEnterName;
    	private JTextField txtName;
     
    	MyModel model = new MyModel();
    	ResultSet rs;
     
    	JTable table;
    	JTableHeader header;
    	TableColumn tc;
     
     
    	String strCount;
    	String name = "";
    	String sql;
     
     
    	private int confirm = 0;
     
    	public manipulatingData()
    	{
     
    		model.connectDB();
     
    		table = new JTable(model);
     
     
    		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    		table.setPreferredScrollableViewportSize(new Dimension(300,150));
    		table.setFillsViewportHeight(true);
    		table.setRowSelectionInterval(0,0);
    		table.changeSelection(0,0,false,false);
     
    		table.tableChanged(new TableModelEvent (model));
     
    		f = new JFrame();
    		f.setTitle("Data Manipulation");
    		f.setSize(527,467);
    		f.setResizable(false);
    		f.setLocationRelativeTo(null);
    		f.setVisible(true);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		header = table.getTableHeader();
    		header.setBackground(new Color(25,125,250));
    		header.setReorderingAllowed(false);
    		header.setResizingAllowed(false);
     
     
    		btnAdd = new JButton("Add");
    		btnEdit = new JButton("Edit");
    		btnDelete = new JButton("Delete");
    		btnSearch = new JButton("Search");
     
     
    		btnAdd.addActionListener(this);
    		btnEdit.addActionListener(this);
    		btnDelete.addActionListener(this);
    		btnSearch.addActionListener(this);
     
    		JScrollPane scroll = new JScrollPane(table);
     
    		scroll.setBounds(10,20,500,300);
    		btnAdd.setBounds(10,350,120,30);
    		btnEdit.setBounds(150,350,120,30);
    		btnDelete.setBounds(10,390,120,30);
    		btnSearch.setBounds(350,350,120,30);
     
     
     
    		p = new JPanel();
     
    		p.setLayout(null);
    		p.setBackground(new Color(115,55,93));
    		p.add(scroll);
    		p.add(btnAdd);
    		p.add(btnEdit);
    		p.add(btnDelete);
    		p.add(btnSearch);
     
    		f.add(p);
    	}
     
    	public static void main (String [] args)
    	{
    		new manipulatingData();
     
    	}
     
    	public void actionPerformed (ActionEvent e)
    	{
     
    		rs = model.rs;
     
    		if(e.getSource()==btnAdd)
    		{
    			//JOptionPane.showMessageDialog(null, model);
    		}
     
     
    		else if (e.getSource()==btnDelete)
    		{
    			confirm = JOptionPane.showConfirmDialog(null,"Do you want to delete this?","Confirmation",0);
     
    			if(confirm == 0)
    			{
    				try
    				{
    					rs.beforeFirst();
     
    					while(rs.next())
    					{
    						if(table.getSelectedRow() == 0 && rs.getRow() == 1)
    						{
    							rs.deleteRow();
    						//	JOptionPane.showMessageDialog(null,"Record Deleted","Successful",1);
     
    							break;
     
     
    						}
     
    						else if(table.getSelectedRow() == rs.getRow())
    						{
     
    							rs.next();
    							rs.deleteRow();
     
    							table.tableChanged(new TableModelEvent (model));
    						//	JOptionPane.showMessageDialog(null,"Record Deleted","Successful",1);
     
     
     
    							break;
     
    						}
     
    					}
     
     
     
    				}
    				catch (SQLException a)
    				{
    					System.err.println(a);
    				}
     
     
     
    			}
    		}
     
     
    	}
     
     
    }
     
    	class MyModel extends AbstractTableModel
    	{
     
     
    		Connection con;
    		Statement s;
    		ResultSet rs;
    		ResultSetMetaData rsmd;
    		String cDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
    		Object rowData[][] = {};
    		String columnNames[] = {};
     
     
     
    		int count = 0;
    		int recordCount = 0;
     
     
     
    	    public int getColumnCount()
    	    {
    	       return columnNames.length;
    	    }
     
    	       public String getColumnName(int column)
    	       {
     
    	        	return columnNames[column];
    	        }
     
    	        public int getRowCount()
    	        {
    	        	return rowData.length;
    	        }
     
    	        public Object getValueAt(int row, int column)
    	        {
    	        	return rowData[row][column];
    	        }
     
    	        public void setValueAt(Object value, int row, int column)
    	        {
    	        	rowData[row][column] = value;
    				fireTableCellUpdated(row, column);
     
     
     
    		    }
     
    	        public boolean isCellEditable(int row, int column)
    	        {
    	        	return false;
    	        }
     
     
     
    		public void connectDB()
    		{
     
     
    			try
    			{
     
    				Class.forName(cDriver);
     
    				con = DriverManager.getConnection("jdbc:odbc:testDBjava3.accdb","","");
    				s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    				rs = s.executeQuery("Select * from friends");
    				rsmd = rs.getMetaData();
     
    				recordCount = 0;
    				count = rsmd.getColumnCount();
     
    	 			String fieldNames[] = new String[count];
     
    	 			for(int a=1;a<=count;a++)
    	 			{
    	 				fieldNames[a-1] = rsmd.getColumnName(a);
    	 			}
     
    				while(rs.next())
    				{
    	 				recordCount++;
    				}
     
     
     
    				Object data [][]  = new Object[recordCount][count];
     
    				rs.beforeFirst();
    				for(int x = 0;x<recordCount;x++)
    				{
    					rs.next();
    					for(int y = 0;y<count;y++)
    					{
    						data[x][y] = rs.getObject(y+1);
    					}
    				}
     
    				columnNames = fieldNames;
    				rowData = data;
     
     
     
     
     
    			}
     
    			catch (ClassNotFoundException exp)
    			{
    				System.err.println(exp);
    			}
     
    			catch(SQLException exp)
    			{
    				System.err.println(exp);
    			}
    		}
    	}

    Please help me. It's really not refreshing. Thank you so much.


  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: [HELP] Refreshing JTable connected to database

    I'm trying to refresh/update the table connected to database.
    Can you explain? What does the program do when it starts? What do you do next?
    What does the program then do in response?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Refreshing JTable connected to database

    The problem that I am now facing with that program is when I add/delete, the refresh/update of the JTable comes late. I need to do it two or more times for the model to apply the changes. Seems like the problem is on the database connection? This is just a test for me to make an election system. Thanks in advance....

  4. #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: [HELP] Refreshing JTable connected to database

    when I add/delete, the refresh/update of the JTable comes late
    There is no code in the listener for the add button.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Refreshing JTable connected to database

    I'm sorry. I forgot.

    	model.connectDB();
     
    		if(e.getSource()==btnAdd)
    		{
    				String name = JOptionPane.showInputDialog(null,"Enter here: ");
    				sql = "Insert into friends values('"+(name)+"')";
     
    				try
    				{
     
    					model.s.executeUpdate(sql);
     
    					model.fireTableStructureChanged();
     
    				//	JOptionPane.showMessageDialog(null,"Data Saved","Save Successful",1);
     
     
     
     
     
    				}
    				catch(SQLException exp)
    				{
    					System.err.println(exp);
    				}
     
    		}

  6. #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: [HELP] Refreshing JTable connected to database

    Where is the logic for deleting a row from the table that is being displayed?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Refreshing JTable connected to database

    What do you mean? It is stated in the code above in "btnDelete".

  8. #8
    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: [HELP] Refreshing JTable connected to database

    Where does the code remove elements from the array in the model class?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Refreshing JTable connected to database

    Sir, I used rs.deleteRow() to remove a row..

  10. #10
    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: [HELP] Refreshing JTable connected to database

    That's for the DB. Does it work? Is the row removed from the DB?
    What about the display of the row in the JTable?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Refreshing JTable connected to database

    It works but won't take effect until I delete it again or delete some other record. I don't use any method to delete record from JTable. Just the database. Do you get me?

    --- Update ---

    It works but won't take effect until I delete it again or delete some other record. I don't use any method to delete record from JTable. Just the database. Do you get me?

  12. #12
    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: [HELP] Refreshing JTable connected to database

    Should what is seen in the JTable be the same as what is in the DB? If a row is deleted, should it be deleted from the DB and from the GUI?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Refreshing JTable connected to database

    It is exactly what I choose to delete. The record from database is what I always successfully deleted and JTable is not even responding immediately to the change..

  14. #14
    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: [HELP] Refreshing JTable connected to database

    JTable is not even responding immediately to the change..
    Is that what you want to happen?
    If not, where is the code to change the JTable that I asked about in posts #6 and #8
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Refreshing JTable connected to database

    Yeah. That's what I want. Just like in visual basic 6's adodc/datagrid. Immediate response to change with ".refresh".

  16. #16
    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: [HELP] Refreshing JTable connected to database

    Sorry, I don't know anything about visual basic.

    From post #8
    Where does the code remove elements from the array in the model class?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Refreshing JTable connected to database

    I don't have any remove command in there, I just used direct deletion from database. Is that wrong? Also, I didn't use vectors. Sorry if I'm confusing you.

  18. #18
    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: [HELP] Refreshing JTable connected to database

    I don't have any remove command in there
    You need to have code there to remove the deleted row so that what is displayed in the JTable matches what is in the DB.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Dec 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Refreshing JTable connected to database

    I thought firing events from the table model can do it alone.

  20. #20
    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: [HELP] Refreshing JTable connected to database

    I thought firing events from the table model can do it alone.
    How would that change the contents of the array that you created and use?

    Try debugging the code to see what happens by adding some println statements to different methods in the model class so you can see when they are called and what args are passed to them.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. can anyone help me about javabank gui connected in database?
    By jabaman in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 13th, 2012, 08:50 AM
  2. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM
  3. Refreshing JTable!!
    By DarkGhetto22 in forum JDBC & Databases
    Replies: 1
    Last Post: April 5th, 2012, 12:08 AM
  4. refreshing a jtable with database
    By kollyisrealisaac in forum JDBC & Databases
    Replies: 0
    Last Post: May 7th, 2011, 01:50 PM

Tags for this Thread