Wednesday 15 September 2010

mysql - JAVA update database on JTable cell change -


I'm Nob for Java (not new) and I can not find a good tutorial for this. I use jTable to display a database with a database filled with data.

So far so good, I get a table:

table Preview

Standard You can click on a table cell and fill it with something new by changing it to a text field. You all know that, but I used to update the value in my database How can i use

My code:

  import [...]; JPanel {public string table in public class table; Private database db; Public Table (String Tablelines, Database DB) {try {table = tablename; This.db = db; // Get more height with the table ResultSet res = db.query ("SELECT COUNT (*) + '+ + + + +" + "); ResultSet res2 = db.query (" Select COUNT (*) from info_schma Columns where table_name = '+ + + + "+" + "" "); int rows = 0; int collums = 0; res.next (); res2.next (); line = res.getInt (1); Collums = Find res2.getInt (1); // table column name, and then set ResultSet clom = db.query ("DESCRIBE`" ++ "+" + ""); string [] columnNames = new string [collums ]; Int s = 0; while (clom.next ()) {columnNames [s] = clom.getString (1); r ++;} // get the table data and array the array Put the [] [] data = new Object [row] [column]: Results = db.query ( "Select * from FROM" + table + " '"); Int q = 0; While (result.next ()) {for (int a = 0; a & lt; = (collums - 1); a ++) {data [q] [a] = result.getString (a + 1); // system.out.printLN (q + "-" + a); } Q ++; } // make the JTB of the DB result the last jetty table of two array = new jetties (data, column-name); Table.setPreferredScrollableViewportSize (new dimension (500, 70)); Table.setFillsViewportHeight (true); // Cell change JScrollPane scrollPane = new Listen to some events for JScrollPane (table); Jeffre Frame = New Jeffram ("Table Editor"); ScrollPane.setOpaque (true); Frame.setContentPane (Scrollpane); Frame.pack (); Frame.Sets (600, 800); Frame.setVisible (true); } Grip (SQLException e1) {e1.printStackTrace (); }}}   

I think that I need to pack any type of table listener and when something changes, then I accept all of the table and update them with the query I am

This is my proxy code:

  table.bindCellEventListner string data = ""; Int f = 0 while (row.next ()) {data. = "+" + Clom [f] + "` = '"+ line [F] +"', "F ++;} data. DelLastChar (); db.query (" UPDATE` "+ +" + + " SET "+ Data +" where `id` =" + line [0] + ";");});    

Step 1: If you create your table then it will be completely easier, the table now extends a table model (if you do not already have it). So if you have to Just ask if you need help.

Step 2: In the table model, you have to make the rooms editable. What do you need to see these simple copies to print the clipboard? Note: Text content in the code block is automatically word-wrapped

  is a public boolean cellEditable {Return true}} Public Zero Set Value (Object Value, Intro, Int Colley) {rowData [row] [col] = value; FireTableCellUpdated (line, call);}  < / Pre> 

Step 3: In the second method you will see that we will fire the method called fireTableCellUpdated They may have. So here we can catch how it is changing.

mytable.getModel (). AddTableModelListener (yourClass);

And do you need this visual plaincopy to print the clipboard in the category that you decide that you will implement TableModelListener? Note: Text content in the code block is automatically word-wrapped

  changed the public null table (TableModelEvent e) {int row = e.getFirstRow (); Int column = e.getColumn (); Table Model Model = (table model) e.getSource (); Object data = model.get value (line, column); // Now you have data in the cell and place in the grid where // is cell so you can use the data}    

No comments:

Post a Comment