3 Replies - 630 Views - Last Post: 19 December 2011 - 10:07 AM Rate Topic: -----

#1 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

updating sql field w/JTable data from a different Table?

Posted 19 December 2011 - 05:37 AM

Is it possible to do this. We have a JTable which displays data from Table1 w/checkbox at the end. and we want to check off the selected rows and save them to fields in Table2. is this possible? Let me know. I can post our code. Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: updating sql field w/JTable data from a different Table?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: updating sql field w/JTable data from a different Table?

Posted 19 December 2011 - 05:43 AM

Everything is possible :)
JTable is just a fancy GUI component that display the data contained into its model. The JTable by itself does not contain/hold any data. The data in the TableModel can be contain in array, in Vector or ArrayList, in a flat file, in a SQL database.

Whenever an evenement occurs, you can always update the fields contained in the JTable model accordingly and the JTable will display these fields.
Was This Post Helpful? 1
  • +
  • -

#3 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: updating sql field w/JTable data from a different Table?

Posted 19 December 2011 - 06:34 AM

thanks pbl. would you be able to take a look at our code and see what we're doing wrong? when we hit the submit button it is currently not working. I don't know if it's our String sql that is wrong or not.+


class Coursetable extends JFrame implements ActionListener
{
	JTable Coursetable;
	JButton b1;

	public Coursetable()
	{
		b1 = new JButton("Update");
		Vector ColumnName = new Vector();
		Vector data = new Vector();

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(1200, 420);
		//pack();
		setVisible(true);
		setLocationRelativeTo(null);

		try
		{
			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
			Class.forName(driver);
			Connection conn = DriverManager.getConnection("jdbc:odbc:db1");


			String db = "jdbc.odbc.db1";
			String sql = "Select * from Courses";
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			ResultSetMetaData md = rs.getMetaData();
			int columns = md.getColumnCount();

			for(int i = 1; i <= columns; i++)
			{
				ColumnName.addElement(md.getColumnName(i));
			}

			while(rs.next())
			{
				Vector row = new Vector(columns);

				for(int i = 1; i <= columns; i++)
				{
					row.addElement(rs.getObject(i));
				}

				data.addElement(row);
			}

			rs.close();
			stmt.close();
			conn.close();
		}

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


		JTable Coursetable = new JTable(data, ColumnName)
		{
			public Class getColumnClass(int column)
			{
				for(int row = 0; row < getRowCount(); row++)
				{
					Object o = getValueAt(row, column);

					if(o != null)
					{
						return o.getClass();
					}
				}

				return Object.class;
			}
		};

		JScrollPane scrollPane = new JScrollPane(Coursetable);
		getContentPane().add(scrollPane);

		JPanel buttonPanel = new JPanel();
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);

		buttonPanel.add(b1);
		b1.addActionListener(this);
		b1.setVisible(true);
	}

	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();

		if(cmd.equals("Update"))
		{
			update();

		}


	}

	private void update()
				{
					Connection con = null;

				//	String fName = firstName.getText();





					try
					{
						Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					  	con = DriverManager.getConnection("jdbc:odbc:db1");
						String sql = "Update Registration Set course1 = ? ,course2 = ?, course3 = ?, course4 = ? where user='"+StudentPassword.t.getText()+"'";
		//			  	String sql = "Update Registration Set course1 = ? ,course2 = ?, course3 = ?, course4 = ? Where firstName = '"+user+"'";
						Statement statement = con.createStatement();
						statement.execute(sql);

					JOptionPane.showMessageDialog(null, " Thank You. You have successfully updated Student Courses.");
								dispose();
								StudentMenu sm = new StudentMenu();
								sm.setVisible(true);
								sm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
								sm.setSize(400, 150);
								sm.setResizable(false);
								sm.setLocationRelativeTo(null);




					}
					catch(Exception e)
					{
						e.printStackTrace();


					}


			}

	public static void main(String[] args)
	{
		Coursetable frame = new Coursetable();
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setSize(400, 420);
		frame.pack();
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: updating sql field w/JTable data from a different Table?

Posted 19 December 2011 - 10:07 AM

You will have to pass from a basic JTable created by passing to it elements which then creates its own DefaultTableModel to a JTable to which you provide your own customized TableModel

Luck you are I anticipated you question and I wrote a tutorial about it :)

http://www.dreaminco...h-a-tablemodel/
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1