Solved

Moving ResultSet to a JTable

Posted on 2003-10-28
36
681 Views
Last Modified: 2013-11-23
Hi

This is a snip of a Addressbook program I have written..

Which modifications do I need to make if I would like a record to be displayed in a JTable ?

/Fred

 class FindRecord implements ActionListener {
   private DataPanel scnvar;
   private JTextArea msgout;
   private Connection dbconn;
 
   public FindRecord( Connection dbc, DataPanel scv,
                      JTextArea msg ) {
      dbconn = dbc;
      scnvar = scv;
      msgout = msg;
   }

   public void actionPerformed( ActionEvent e ) {
   try {
  String strg = scnvar.firstname.getText();
  for (int i = 0; i < scnvar.firstname.getText().length(); i++){  
      if (Character.isLetter(scnvar.firstname.getText().charAt(i)));
        Character.isWhitespace(scnvar.firstname.getText().charAt(i));
                   }
      if ( !strg.equals( "" ) ) {
         Statement statement = dbconn.createStatement();
         String query = "SELECT *FROM addressbook4 WHERE name LIKE '" + strg + "'";
       msgout.append( "\nSending query: "   +
                  dbconn.nativeSQL( query ) + "\n" );
       ResultSet rs = statement.executeQuery( query );
       display( rs );
       statement.close();
      }
      else
          scnvar.firstname.setText(
                        "Enter name then press Find" );
      }
   catch ( SQLException sqlex ) {
       msgout.append( sqlex.toString() + sqlex.getMessage() );
       }
}
        public void display( ResultSet rs ) {
          try{
        rs.next();
        int recordNumber = rs.getInt( 1 );
        if ( recordNumber != 0 ) {
          scnvar.emailadr.setText( String.valueOf(recordNumber) );
         scnvar.firstname.setText( rs.getString( 2 ) );
          scnvar.lastname.setText( rs.getString( 3 ) );
          scnvar.alias.setText( rs.getString( 4 ) );
                 }
       else
          msgout.append( "\nNo record found\n" );        
    }
    catch ( SQLException sqlex ) {
       msgout.append( "\n*** name Not In Database ***\n" );
     }
     }
    }
0
Comment
Question by:Euroman_21
  • 18
  • 15
36 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 9634112
You can use this:

// The below just for searches
// TableModel
// ResultSet
resultSet = statement.executeQuery(query);
metaData = resultSet.getMetaData();

int numberOfColumns =  metaData.getColumnCount();
Vector columnNames = new Vector();
for(int column = 0; column < numberOfColumns; column++) {
 columnNames.addElement(metaData.getColumnLabel(column+1));
}
// Get all rows.
Vector rows = new Vector();
while (resultSet.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= getColumnCount(); i++) {
  newRow.addElement(resultSet.getObject(i));
}
rows.addElement(newRow);
}

JTable table = new JTable(rows, columnNames);
0
 

Author Comment

by:Euroman_21
ID: 9634951
Hi Thanks

Where do I insert the code You surgested ?

What else do I need to define to get it to work properly ?

/Fred
0
 

Author Comment

by:Euroman_21
ID: 9635583
Hi

I have inserted the code You surgested into my find statement.

But then I try to compile the entire program I get the errors:

Addressbook.java:163: cannot resolve symbol
symbol  : class MetaData
location: class FindRecord
         MetaData metaData = rs.getMetaData();
         ^
Addressbook.java:175: cannot resolve symbol
symbol  : method getColumnCount ()
location: class FindRecord
for (int i = 1; i <= getColumnCount(); i++)

What am I doing wrong ?

Thanks for all You answers.

Sincerely
Fred

class FindRecord implements ActionListener {

private DataPanel scnvar;
   private JTextArea msgout;
   private Connection dbconn;

   public FindRecord( Connection dbc, DataPanel scv,
                      JTextArea msg ) {
      dbconn = dbc;
      scnvar = scv;
      msgout = msg;
   }

   public void actionPerformed( ActionEvent e ) {
   try {
  String strg = scnvar.firstname.getText();
  for (int i = 0; i < scnvar.firstname.getText().length(); i++){  
      if (Character.isLetter(scnvar.firstname.getText().charAt(i)));
        Character.isWhitespace(scnvar.firstname.getText().charAt(i));
                   }
      if ( !strg.equals( "" ) ) {
         Statement statement = dbconn.createStatement();
         String query = "SELECT *FROM addressbook4 WHERE name LIKE '" + strg + "'";
       msgout.append( "\nSending query: "   +
                  dbconn.nativeSQL( query ) + "\n" );
      
       ResultSet rs = statement.executeQuery( query );
       MetaData metaData = rs.getMetaData();       

int numberOfColumns =  metaData.getColumnCount();
Vector columnNames = new Vector();

for(int column = 0; column < numberOfColumns; column++) {
 columnNames.addElement(metaData.getColumnLabel(column+1));
}
// Get all rows.
Vector rows = new Vector();
while (rs.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= getColumnCount(); i++) {
  newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}

JTable table = new JTable(rows, columnNames);
      
 display( rs );
       statement.close();
      }
      else
          scnvar.firstname.setText(
                        "Enter name then press Find" );
      }
   catch ( SQLException sqlex ) {
       msgout.append( sqlex.toString() + sqlex.getMessage() );
       }
}
        public void display( ResultSet rs ) {
          try{
        rs.next();
        int recordNumber = rs.getInt( 1 );
        if ( recordNumber != 0 ) {
          scnvar.emailadr.setText( String.valueOf(recordNumber) );
         scnvar.firstname.setText( rs.getString( 2 ) );
          scnvar.lastname.setText( rs.getString( 3 ) );
          scnvar.alias.setText( rs.getString( 4 ) );
                 }
       else
          msgout.append( "\nNo record found\n" );        
    }
    catch ( SQLException sqlex ) {
       msgout.append( "\n*** name Not In Database ***\n" );
     }
     }

    }


class FindRecord implements ActionListener {

private DataPanel scnvar;
   private JTextArea msgout;
   private Connection dbconn;

   public FindRecord( Connection dbc, DataPanel scv,
                      JTextArea msg ) {
      dbconn = dbc;
      scnvar = scv;
      msgout = msg;
   }

   public void actionPerformed( ActionEvent e ) {
   try {
  String strg = scnvar.firstname.getText();
  for (int i = 0; i < scnvar.firstname.getText().length(); i++){  
      if (Character.isLetter(scnvar.firstname.getText().charAt(i)));
        Character.isWhitespace(scnvar.firstname.getText().charAt(i));
                   }
      if ( !strg.equals( "" ) ) {
         Statement statement = dbconn.createStatement();
         String query = "SELECT *FROM addressbook4 WHERE name LIKE '" + strg + "'";
       msgout.append( "\nSending query: "   +
                  dbconn.nativeSQL( query ) + "\n" );
      
       ResultSet rs = statement.executeQuery( query );
       MetaData metaData = rs.getMetaData();       

int numberOfColumns =  metaData.getColumnCount();
Vector columnNames = new Vector();

for(int column = 0; column < numberOfColumns; column++) {
 columnNames.addElement(metaData.getColumnLabel(column+1));
}
// Get all rows.
Vector rows = new Vector();
while (rs.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= getColumnCount(); i++) {
  newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}

JTable table = new JTable(rows, columnNames);
      
 display( rs );
       statement.close();
      }
      else
          scnvar.firstname.setText(
                        "Enter name then press Find" );
      }
   catch ( SQLException sqlex ) {
       msgout.append( sqlex.toString() + sqlex.getMessage() );
       }
}
        public void display( ResultSet rs ) {
          try{
        rs.next();
        int recordNumber = rs.getInt( 1 );
        if ( recordNumber != 0 ) {
          scnvar.emailadr.setText( String.valueOf(recordNumber) );
         scnvar.firstname.setText( rs.getString( 2 ) );
          scnvar.lastname.setText( rs.getString( 3 ) );
          scnvar.alias.setText( rs.getString( 4 ) );
                 }
       else
          msgout.append( "\nNo record found\n" );        
    }
    catch ( SQLException sqlex ) {
       msgout.append( "\n*** name Not In Database ***\n" );
     }
     }

    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9635647
It should be ResultSetMetaData sorry

change

>>for (int i = 1; i <= getColumnCount(); i++)

to

for (int i = 1; i <= numberOfColumns; i++)

0
 

Author Comment

by:Euroman_21
ID: 9635866
Hi

Thanks now the program compiles....

But the table is not display in the GUI !

Anything else I need to define to get it to display the table ?

Sincerely

Fred
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9635951
Please just post the code you are using to get the result set into the table. I don't need to see any other code. Hint - the code i posted (with those corrections we just mentioned) is virtually all that's needed to get a query into a JTable
0
 

Author Comment

by:Euroman_21
ID: 9636128
Hi

Thanks for You answers.

The code you requested is below marked by an arrows:

Sincerely

Fred

public FindRecord( Connection dbc, DataPanel scv,
                      JTextArea msg ) {
      dbconn = dbc;
      scnvar = scv;
      msgout = msg;
   }

   public void actionPerformed( ActionEvent e ) {
   try {
  String strg = scnvar.firstname.getText();
  for (int i = 0; i < scnvar.firstname.getText().length(); i++){  
      if (Character.isLetter(scnvar.firstname.getText().charAt(i)));
        Character.isWhitespace(scnvar.firstname.getText().charAt(i));
                   }
      if ( !strg.equals( "" ) ) {
         Statement statement = dbconn.createStatement();
         String query = "SELECT *FROM contacts WHERE firstname LIKE '" + strg + "'";
       msgout.append( "\nSending query: "   +
                  dbconn.nativeSQL( query ) + "\n" );
       ResultSet rs = statement.executeQuery( query );
ResultSetMetaData rsmd = rs.getMetaData();

int numberOfColumns =  rsmd.getColumnCount();
Vector columnNames = new Vector();
for(int column = 0; column < numberOfColumns; column++) {
 columnNames.addElement(rsmd.getColumnLabel(column+1));
}
// Get all rows.
Vector rows = new Vector();
while (rs.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++) {
  newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}

JTable table = new JTable(rows, columnNames);

---->Maybe this is missing something ?

       display( rs );
       statement.close();
      }
      else
          scnvar.firstname.setText(
                        "Enter firstname then press Find" );
      }
   catch ( SQLException sqlex ) {
       msgout.append( sqlex.toString() + sqlex.getMessage() );
       }
}
Do I need to delete this ?-->        public void display( ResultSet rs ) {
          try{
        rs.next();
        int recordNumber = rs.getInt( 1 );
        if ( recordNumber != 0 ) {
          scnvar.emailadr.setText( String.valueOf(recordNumber) );
         scnvar.firstname.setText( rs.getString( 2 ) );
          scnvar.lastname.setText( rs.getString( 3 ) );
          scnvar.alias.setText( rs.getString( 4 ) );
                 }
       else
          msgout.append( "\nNo record found\n" );        
    }
    catch ( SQLException sqlex ) {
       msgout.append( "\n*** firstname Not In Database ***\n" );
     }
     }
    }
0
 

Author Comment

by:Euroman_21
ID: 9636997
Sorry I posted the same code again, but I'm not sure what mean ?


Have I placed Your code surgestion in correct spot ?

/Fred
0
 

Author Comment

by:Euroman_21
ID: 9637178
Hi

I have reading through the articles on java.sun.com, but have been unable to find relevant on how to display a resultset in JTable.

So I would "really" appriciate if there is somebody out there who can show how to get it to work ? Because I'm stuck !!


Sincerely

Fred

0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9638872
Your table should be able to be display using the code suggested by CEHJ, maybe you can show us the coding of the frame where you add your table?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9640807
I've made it into a function that you can now call quite simply by passing a null reference to a JTable and the filled result set. Don't forget to close your db stuff (preferably in a finally block):


      public JTable ResultSetToTable (JTable table, ResultSet rs) {
            try {
                  // The below just for searches
                  ResultSetMetaData metaData = rs.getMetaData();
                  int numberOfColumns =      metaData.getColumnCount();
                  Vector columnNames = new Vector();
                  for(int column = 0; column < numberOfColumns; column++) {
                        columnNames.addElement(metaData.getColumnLabel(column+1));
                  }
                  // Get all rows.
                  Vector rows = new Vector();
                  while (rs.next()) {
                        Vector newRow = new Vector();
                        for (int i = 1; i <= numberOfColumns; i++) {
                              newRow.addElement(rs.getObject(i));
                        }
                        rows.addElement(newRow);
                  }
                  return new JTable(rows, columnNames);
            }
            catch(Exception e) {
                  e.printStackTrace();
                  return null;
            }
      }
0
 

Author Comment

by:Euroman_21
ID: 9645582
Thanks for Your answer....

I have insertet the public JTable ResultSetToTable(JTable table, ResultSet rs)  just above the void display(ResultSet rs), but then I run the program the table is not shown.

Do I need to calll the JTable ResultSetToTable() somewhere ?

Secondly for some reason or another the find function does not pass the sql query correctly to the database, because then I search for a firstname string called "Bill" I get the result back "firstname not found" eventhough there is a record in my mysqldb where the valueof firstname  = 'Bill'.

So Whats wrong ?

/Fred
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9651702
Let's take one question at a time:

>>Do I need to calll the JTable ResultSetToTable() somewhere ?

Obviously. The object of calling the function is to take a ResultSet and put it into a JTable. You don't need to call your display method

0
 

Author Comment

by:Euroman_21
ID: 9651753
Okay....

I have tried to call the function in main, but I get an error !

So where is the best place to call it ?

/Fred
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9657067
Just after the ResultSet's been obtained
0
 

Author Comment

by:Euroman_21
ID: 9660779
Hi again....

I called the function  JTable ResultSetToTable() just after:

ResultSet rs = statement.executeQuery( query );
JTable ResultSetToTable;

under the find function.

Im able to compile this code, but then I run the program the JTable is still not displayed !

/Fred

0
 

Author Comment

by:Euroman_21
ID: 9665475
Hi

It suddenly came to me why the JTable is not displayed as it should be.


My program is Gui-based and not command-line based ?

Can that be reason why the JTable is not displayed then I start the program ?

Sincerely

/Fred
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9665954
Please post the code immediately before and after resultSetToTable is being called. Sorry, the function should start with a small letter
0
 

Author Comment

by:Euroman_21
ID: 9666004
Hi

Thanks for You answer.


Here is the code immediately before the JTable resultSetToTable() function:

class FindRecord implements ActionListener {
   private DataPanel scnvar;
   private JTextArea msgout;
   private Connection dbconn;
 
   public FindRecord( Connection dbc, DataPanel scv,
                      JTextArea msg ) {
      dbconn = dbc;
      scnvar = scv;
      msgout = msg;
   }

   public void actionPerformed( ActionEvent e ) {
   try {
  String strg = scnvar.firstname.getText();
  for (int i = 0; i < strg.length(); i++){  
      if (Character.isLetter(strg.charAt(i)));
        Character.isWhitespace(strg.charAt(i));
                   }
      if ( !strg.equals( "" ) ) {
         Statement statement = dbconn.createStatement();
       String query = "select *from contacts where firstname like '" + strg + "'";
      msgout.append( "\nSending query: "   +
                  dbconn.nativeSQL( query ) + "\n" );
       ResultSet rs = statement.executeQuery( query );
     
       display( rs );
        statement.close();
            }
      else
          scnvar.firstname.setText(
                        "Enter firstname then press Find" );
      }
   catch ( SQLException sqlex ) {
       msgout.append( sqlex.toString() + sqlex.getMessage() );
       }
}

Is my resultset incoded incorrectly ?

/Fred
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9666018
I don't see *any* call to resultSetToTable in there...
0
 

Author Comment

by:Euroman_21
ID: 9666098
Hi again...

Thanks for You answer.

Is this what You mean ?

Then I try to compile it I get the following error:

';' expected
JTable resultSetToTable( rs );

Is this because I'm calling the ResultSet wrongly with the function ?


Sincerely

/Fred

class FindRecord implements ActionListener {
   private DataPanel scnvar;
   private JTextArea msgout;
   private Connection dbconn;
 
   public FindRecord( Connection dbc, DataPanel scv,
                      JTextArea msg ) {
      dbconn = dbc;
      scnvar = scv;
      msgout = msg;
   }

   public void actionPerformed( ActionEvent e ) {
   try {
  String strg = scnvar.firstname.getText();
  for (int i = 0; i < strg.length(); i++){  
      if (Character.isLetter(strg.charAt(i)));
        Character.isWhitespace(strg.charAt(i));
                   }
      if ( !strg.equals( "" ) ) {
         Statement statement = dbconn.createStatement();
       String query = "select *from contacts where firstname like '" + strg + "'";
      msgout.append( "\nSending query: "   +
                  dbconn.nativeSQL( query ) + "\n" );
       ResultSet rs = statement.executeQuery( query );
       JTable resultSetToTable( rs );
        statement.close();
            }
      else
          scnvar.firstname.setText(
                        "Enter firstname then press Find" );
      }
   catch ( SQLException sqlex ) {
       msgout.append( sqlex.toString() + sqlex.getMessage() );
       }
}

public JTable resultSetToTable (JTable table, ResultSet rs) {
          try {
               // The below just for searches
               ResultSetMetaData metaData = rs.getMetaData();
               int numberOfColumns =     metaData.getColumnCount();
               Vector columnNames = new Vector();
               for(int column = 0; column < numberOfColumns; column++) {
                    columnNames.addElement(metaData.getColumnLabel(column+1));
               }
               // Get all rows.
               Vector rows = new Vector();
               while (rs.next()) {
                    Vector newRow = new Vector();
                    for (int i = 1; i <= numberOfColumns; i++) {
                         newRow.addElement(rs.getObject(i));
                    }
                    rows.addElement(newRow);
               }
               return new JTable(rows, columnNames);
          }
          catch(Exception e) {
               e.printStackTrace();
               return null;
          }
     }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9666206
Actually, my code would be better thus with yours:

    public TableModel resultSetToTableModel(ResultSet rs) {
          try {
               // The below just for searches
               ResultSetMetaData metaData = rs.getMetaData();
               int numberOfColumns =     metaData.getColumnCount();
               Vector columnNames = new Vector();
               for(int column = 0; column < numberOfColumns; column++) {
                    columnNames.addElement(metaData.getColumnLabel(column+1));
               }
               // Get all rows.
               Vector rows = new Vector();
               while (rs.next()) {
                    Vector newRow = new Vector();
                    for (int i = 1; i <= numberOfColumns; i++) {
                         newRow.addElement(rs.getObject(i));
                    }
                    rows.addElement(newRow);
               }
               return new DefaultTableModel(rows, columnNames);
          }
          catch(Exception e) {
               e.printStackTrace();
               return null;
          }
     }

/////////////////////////

Then you can do:

ResultSet rs = statement.executeQuery( query );
table.setModel(resultSetToTableModel(rs));

Obviously, you should already have a valid reference to a JTable called 'table'

0
 

Author Comment

by:Euroman_21
ID: 9666561
Hi

Thanks a million for you answer again...

Then I try to compile I get the error:

error 'class' or 'interface' expected.
 
public TableModel resultSetToTableModel(ResultSet rs).
^

Can You the resultset i reaching the table now ?

Sincerely

/Fred

public TableModel resultSetToTableModel(ResultSet rs) {
          try {
               // The below just for searches
               ResultSetMetaData metaData = rs.getMetaData();
               int numberOfColumns =     metaData.getColumnCount();
               Vector columnNames = new Vector();
               for(int column = 0; column < numberOfColumns; column++) {
                    columnNames.addElement(metaData.getColumnLabel(column+1));
               }
               // Get all rows.
               Vector rows = new Vector();
               while (rs.next()) {
                    Vector newRow = new Vector();
                    for (int i = 1; i <= numberOfColumns; i++) {
                         newRow.addElement(rs.getObject(i));
                    }
                    rows.addElement(newRow);
               }
               return new DefaultTableModel(rows, columnNames);
          }
          catch(Exception e) {
               e.printStackTrace();
               return null;
          }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9666771
>>Then I try to compile I get the error:

You must be putting the code in the wrong place then. Make sure you own code is compilable first then place it as a function in your own code. If you're finding it difficult to do even this, you must catch up in your learning as the program is beyond your current ability.
0
 

Author Comment

by:Euroman_21
ID: 9666932
Hi Now the program compiles and runs....

But then I start the program there is a large empty contentpane and no table.

Hi is my entire program about 200 lines.

If I include it all maybe You can see what wrong !

/Fred

p.s. Thank You for all Your kind answers.

import java.sql.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;      
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;

public class Addressbook extends JFrame {
    private Connection dbconn;  
    private JTable table;
   private DataPanel scnvar;
    private JTextArea msgout;
   

     public Addressbook() {
      super( "**Addressbook**" );
       Container cont = getContentPane();
            table = new JTable();
      scnvar = new DataPanel();
      msgout = new JTextArea( 8, 40 );
      cont.setLayout( new FlowLayout() );
        cont.add( new JScrollPane( scnvar ) );
      cont.add( new JScrollPane( table ) );
      try {
          Class.forName( "com.mysql.jdbc.Driver" );
          dbconn = DriverManager.getConnection("jdbc:mysql:///addresstest", "root", "");
          dbconn.setAutoCommit(true);
          msgout.append( "Online with db-host !!\n" );
      }
      catch ( ClassNotFoundException cnfex ) {
          cnfex.printStackTrace();
          msgout.append( "Not online with db-host !!\n" +
                        cnfex.toString() );
      }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
         msgout.append( "Not online with db-host\n" +
                        sqlex.toString() );
      }
      catch ( Exception excp ) {
        excp.printStackTrace();
        msgout.append( excp.toString() );
      }


      ButtonPanel controls = new ButtonPanel( dbconn, scnvar, table, msgout);
      cont.add( controls );
      setSize( 500, 570 );
      show();
   }

    public static void main( String args[] ) {
       Addressbook addr = new Addressbook();

       addr.addWindowListener(
                        new WindowAdapter() {
                          public void windowClosing( WindowEvent e ) {
                              System.exit(0);
                          }
                        }
                        );
    }
 
class FindRecord implements ActionListener {
   private DataPanel scnvar;
    private JTable table;
  private Connection dbconn;

    public FindRecord( Connection dbc, DataPanel scv,
                      JTable table) {
      dbconn = dbc;
      scnvar = scv;
      table = table;
   }

   public void actionPerformed( ActionEvent e ) {
   try {
  String strg = scnvar.firstname.getText();
  for (int i = 0; i < scnvar.firstname.getText().length(); i++){  
      if (Character.isLetter(scnvar.firstname.getText().charAt(i)));
        Character.isWhitespace(scnvar.firstname.getText().charAt(i));
                   }
      if ( !strg.equals( "" ) ) {
         Statement statement = dbconn.createStatement();
         String query = "SELECT *FROM contacts WHERE firstname LIKE '" + strg + "'";
       ResultSet rs = statement.executeQuery( query );
       statement.close();
      }
      else
          scnvar.firstname.setText(
                        "Enter name then press Find" );
      }
   catch ( SQLException sqlex ) {
       msgout.append( sqlex.toString() + sqlex.getMessage() );
       
   }
   }
}

public TableModel resultSetToTableModel(ResultSet rs) {
          try {
               // The below just for searches
               ResultSetMetaData metaData = rs.getMetaData();
               int numberOfColumns =     metaData.getColumnCount();
               Vector columnNames = new Vector();
               for(int column = 0; column < numberOfColumns; column++) {
                    columnNames.addElement(metaData.getColumnLabel(column+1));
               }
               // Get all rows.
               Vector rows = new Vector();
               while (rs.next()) {
                    Vector newRow = new Vector();
                    for (int i = 1; i <= numberOfColumns; i++) {
                         newRow.addElement(rs.getObject(i));
                    }
                    rows.addElement(newRow);
               }
               return new DefaultTableModel(rows, columnNames);
          }
          catch(Exception e) {
               e.printStackTrace();
               return null;
          }
     }  

class DataPanel extends JPanel {
     
   JTextField emailadr, firstname, lastname, alias;

   JLabel lemailadr, lfirstname, llastname, lalias;

   public DataPanel() {
   JPanel labelPanel = new JPanel();
      labelPanel.setLayout(
         new GridLayout( 4, 1 ) );
      lemailadr = new JLabel( "emailadr:", 0 );
      labelPanel.add( lemailadr );
      lfirstname = new JLabel( "firstname:", 0 );
      labelPanel.add( lfirstname);
      llastname = new JLabel( "lastname:", 0 );
      labelPanel.add( llastname);
   lalias = new JLabel( "alias:", 0 );
         labelPanel.add( lalias);

      JPanel scnvarPanel = new JPanel();
      scnvarPanel.setLayout(
         new GridLayout( 4, 1 ) );
      emailadr = new JTextField(20) ;
      scnvarPanel.add( emailadr );
      firstname = new JTextField( 20 );
      scnvarPanel.add( firstname );
       lastname = new JTextField( 20 );
         scnvarPanel.add( lastname );
alias = new JTextField( 20 );
   scnvarPanel.add( alias );

      lfirstname.setLabelFor( firstname );
      lemailadr.setLabelFor( emailadr );

      setLayout( new GridLayout( 1, 2 ) );
      add( labelPanel );
      add( scnvarPanel );
   }
}

class ButtonPanel extends JPanel {
     
   public ButtonPanel( Connection dbc, DataPanel scv,
      JTable table, JTextArea msg ) {
      setLayout( new GridLayout( 1, 5 ) );
     
      JButton findName = new JButton( "Find Entry" );
      findName.addActionListener( new FindRecord( dbc, scv, table) );
      add( findName );


      JButton clear = new JButton( "Clear" );
      clear.addActionListener( new Clearscnvar( scv ) );
      add( clear );
     
   }
}


class Clearscnvar implements ActionListener {
   private DataPanel scnvar;

   public Clearscnvar( DataPanel scv ) {
      scnvar = scv;
   }

   public void actionPerformed( ActionEvent e )
   {
       scnvar.emailadr.setText( "" );
       scnvar.firstname.setText( "" );
       scnvar.lastname.setText( "" );
       scnvar.alias.setText( "" );
         }
}
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9666955
resultSetToTableModel is not called anywhere
0
 

Author Comment

by:Euroman_21
ID: 9666984
Thanks.....

I call just after ResultSet rs = statement.executeQuery( query );

but still then I start program there is only a empty contentpane where the table was suppose to be..

What can be wrong ?

/Fred

class FindRecord implements ActionListener {
   private DataPanel scnvar;
    private JTable table;
  private Connection dbconn;

    public FindRecord( Connection dbc, DataPanel scv,
                      JTable table) {
      dbconn = dbc;
      scnvar = scv;
      table = table;
   }

   public void actionPerformed( ActionEvent e ) {
   try {
  String strg = scnvar.firstname.getText();
  for (int i = 0; i < scnvar.firstname.getText().length(); i++){  
      if (Character.isLetter(scnvar.firstname.getText().charAt(i)));
        Character.isWhitespace(scnvar.firstname.getText().charAt(i));
                   }
      if ( !strg.equals( "" ) ) {
         Statement statement = dbconn.createStatement();
         String query = "SELECT *FROM contacts WHERE firstname LIKE '" + strg + "'";
       ResultSet rs = statement.executeQuery( query );
I call it here ----> table.setModel(resultSetToTableModel(rs));
       statement.close();
      }
      else
          scnvar.firstname.setText(
                        "Enter name then press Find" );
      }
   catch ( SQLException sqlex ) {
       msgout.append( sqlex.toString() + sqlex.getMessage() );
       
   }
   }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9667029
>>String query = "SELECT *FROM contacts WHERE firstname LIKE '" + strg + "'";

Certainly the above statement is wrong because of the typo, so if your actionPerformed is being called at all, you should be seeing an exception
0
 

Author Comment

by:Euroman_21
ID: 9667065
Whats typo ?


/Fred
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9667088
There's a spelling mistake. There's no space after the asterisk
0
 

Author Comment

by:Euroman_21
ID: 9667141
I corrected that, but still for some reason or another the JTable is not display. In its place is a empty ContentPane !

What can be wrong ?

I know It sucks, but as off now I'm starting to think that there is no way of solving this program and getting the JTable to work.

/Fred
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9667163
You need to put some debugging statments in. Make sure the method is getting called, find out how many records are in there, etc.
0
 

Author Comment

by:Euroman_21
ID: 9667187
I'm a bit lost..

So how do I do that ?

Again thanks for all Your kind answers !

/Fred
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9667195
As I said earlier, if you don't know how to do this basic stuff, it suggests that you have not written most of the code yourself and that you're playing around with code that is too advanced for you.

You'll have to take a step back and do some reading up.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 45
pairstar challenge 2 42
stringclean challenge 26 58
Image decoding from Camera 3 48
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now