java - JTable combined with innerJoin statement -


I have 2 database tables and I want to include them in JTable. First of all, I created 2 jetties for 2 database tables, but I wanted to give it a try and add them to 1 JTb. In my 1 DB table "console" there are 3 columns (id, name and cost) and my 2 table "hardware" consists of 4 columns (id, CPU, memory and HDD).

My problem is to keep informed. When I write

  while (rs.next ()) {a = rs.getString (1); B = rs.getString (2); C = rs.getString (3); While (rss.next ()) {aa = rss.getString (2); Bb = rss.getString (3); Cc = rss.getString (4); } Model.addRow (new object [] {a, b, c, aa, bb, cc}); }  

I get the same price as AA, BB, CC, which is the CPU, memory and HDD of 2D table.

When I write

>
  while (rs.next ()) {a = rs.getString (1); B = rs.getString (2); C = rs.getString (3); While (rss.next ()) {aa = rss.getString (2); Bb = rss.getString (3); Cc = rss.getString (4); Model.addRow (new object [] {a, b, c, aa, bb, cc}); }}  

I get the same values ​​as A, B, C which are IDs, name and cost of the first table

Any ideas?

This is my complete code:

  model = new DefaultTableModel (); Table = new JTable (model); Table.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); Model.fireTableDataChanged (); Model.addColumn ("id"); Model.addColumn ("name"); Model.addColumn ("cost"); Model.addColumn ("CPU"); Model.addColumn ("Memory"); Model.addColumn ("HDD"); Try {conn = DriverManager.getConnection ("jdbc: mysql: // localhost / test1? User = me and password = 12345"); Stmt = conn.createStatement (); Stmtt = conn.createStatement (); Rs = stmt.executeQuery ("Select * consoles from INNER to hardware console.id = hardware.id"); Rss = stmtt.executeQuery ("Select * From Hardware"); Whereas (rsnext ()) {// a = id, b = name, c = cost a = rs.getString (1); B = rs.getString (2); C = rs.getString (3); While (rss.next ()) {// rss.getString (1) ID is not required, // ar = cpu, bb = memory, cc = hdda = rss.getstring (2); Bb = rss.getString (3); Cc = rss.getString (4); Model.addRow (new object [] {a, b, c, aa, bb, cc}); }} Model.fireTableDataChanged (); Table.setCellSelectionEnabled (true); Table.setColumnSelectionAllowed (true); Table.setFillsViewportHeight (true); Table.setSurrendersFocusOnKeystroke (true); Table. Setbound (218, 59, 529, 360); . Add Frame.getContentPane () (Table); Model.fireTableDataChanged (); Conn.close (); Stmt.close (); Stmtt.close (); Rs.close (); Rss.close (); } Hold (SQLException case1) {case1.printStackTrace (); } Hold (exception case2) {case2.printStackTrace (); }    

Your result set should return all fields requiring per-field. You should not use two questions Select the individual field below

  Select c.field1, from c.field2, h.field1 ... to consolas c in inner hardware At hardware h, c.someField = h.someField  

I have never tried what you are trying to do. You may want to do some SQL tutorial.

What happens when you both do it, it is not what you are expecting. Your first query will already return all the fields. This is an issue of joining. You may not get all the results from RSSXx, as you can not expect them.

You can do a simple test using the ResultSetMetaData class to get the number of columns, and you can print all. Values. Something like

  resulting in rs = ... resulted in metadata md = rs.getMetaData (); Int columncount = md.getColumnCount ();  

Then set the loop by using the number of columns as the maximum loop running through the result. For example

  while (rs.next ()) {for (int i = 1; i & lt; = columnCount; i ++) {System.out.print (rs.getObject) (I) + ","); } System.out.println (); }  

UPDATE

A common normal way to accomplish whatever you are trying to do is < / P>

  resulting in rs = ... as a result of metadata md = rs.getMetaData (); Int columncount = md.getColumnCount (); String [] cols = new string [columnCount]; (Int i = 1; i & lt; = columnCount; i ++) for {call [i - 1] = md.getColumnName (i); } DefaultTableModel Model = New DefaultTableModel (cols, 0); While (rsnext ()) {object [] line = new object [columnCount]; (Int i = 1; i & lt; = columnCount; i ++) for {line [i - 1] = rs.getObject (i); } Model.addRow (line); } Table.setModel (model);  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -