After login (if succeed) a user will see the result of statement

"select test_column from table_test " execution.

table_test is the only table of database 'test' which i have created in NetBeans 7.0.1.
test_column is the only column.

class 'MySQLTest' creates the gui for login , connects with the database , and finally put the results into a JTable using class 'data'.

I have tried every possible way to retrieve username and password from a gui (class A) and use them in a class (Class B) to connect to the database but i did not succeed, so i ended up with this way. The connection to the database is done within

JButton jbSubmit ActionListener implementation.

Any ideas ?

package mysqltest;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.table.*;
/**
 *
 * @author FOSSES
 */
public class MySQLTest {
    
  public static class Components extends javax.swing.JFrame  implements ActionListener{    
     JTextField jtUserName = new JTextField(20);// JTextField takes the number of columns (width) as a constructor parameter 
     JPasswordField passwordField = new JPasswordField(10);
       Components(){
           setTitle("ESTABLISH CONNECTION AND DISPLAY DATA FROM MYSQL DB");
       this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        /* GridLayout with 3 rows and 1 column */
        GridLayout gl = new GridLayout(3,1);
        setLayout(gl);       
        /*JPanel defaults to a Flow Layout and so components added will have their
         *preferred size respected*/        
        JPanel jpRow1 = new JPanel();
        JPanel jpRow2 = new JPanel();
        JPanel jpRow3 = new JPanel();        
        JLabel jlName = new JLabel("username");   
        jpRow1.add(jlName);
        jpRow1.add(jtUserName);/* add the panel to the row within the GridLayout */
        add(jpRow1);
        JLabel jlPassword = new JLabel("password");      
        /* add the components to the panel */      
        jpRow2.add(jlPassword);
        jpRow2.add( passwordField);
        add(jpRow2);        
        JButton jbSubmit = new JButton("Submit");  
        jbSubmit.addActionListener(this);  
        jpRow3.add(jbSubmit);
        add(jpRow3);     
        setSize(300,300);      
        setVisible(true);      
    }
        @Override
            public void actionPerformed(ActionEvent ae) {
            String uname = jtUserName.getText();
            char[] pwd = passwordField.getPassword();
   
     String USERNAME = uname; //MySQL Username
     String PASSWORD = new String(pwd);   
            
     String url = "jdbc:mysql://localhost:3307/test";//Location of Database
try
{
Class.forName ("com.mysql.jdbc.Driver").newInstance();

Connection connection ; // Instantiate a new connection
connection = DriverManager.getConnection(url, USERNAME, PASSWORD);
System.out.println ("Success! Database connection has been established");
Statement statement = connection.createStatement(); //create SQL statement wrapper
statement.execute("drop table table_test");
statement.execute("create table table_test ( test_column integer )"); //create a sample table in the database
for (int i =1; i<11; i++)
statement.execute("insert into table_test values(" + i +")"); //create some test data
statement.execute("select test_column from table_test "); //select data from the newly created table
ResultSet resultset = statement.getResultSet(); //get the resultset
if (resultset != null){ //if the result set exists
    int i = 0;
while ( resultset.next() ) //step through the result set 
{
   i++;//count raws   
}
//Define TableModel
TableModel tmodel = new DefaultTableModel(i+1, 1){ };//i+1 raws , 1 column
// JTable Creation
JTable Table = new JTable(tmodel);
                 //Table.setValueAt("NUMBERS",0,0);
              int j = 0;
                resultset.beforeFirst();
              while (resultset.next()) {
                  String number = resultset.getString(1);                  
                  Table.setValueAt(number,j,0);
                  j++;
              }
      Table.getColumnModel().getColumn(0).setHeaderValue("NUMBERS");             
     data dt = new data(Table); 
     dt.display();
}//end if

statement.close(); // close the Statement

connection.close(); // close the Connection
setVisible(false); 
}
catch (Throwable e){
            System.out.println("exception thrown:");
            if (e instanceof SQLException)
                printSQLError((SQLException) e);
            else
                e.printStackTrace();
        }
        }         
}  
    public static void printSQLError(SQLException e)
    {
        while (e != null)
        {
            System.out.println(e.toString());
            e = e.getNextException();
        }
    }
    public static void main(String[] args) {
     Components comp = new Components(); 
     
    }
}
package mysqltest;
/*
 *
 * @author FOSSES
 */
import java.awt.*;
import javax.swing.*;
public class data extends JFrame {
  
   public JTable table = new JTable();
   
   data(JTable Table)
   {
       table = Table;
   }
  public data() {
    super("Database Test Frame");
  }  
  public void display() {
   
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(350, 200); 
   
    JScrollPane scrollpane = new JScrollPane(table);
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3, 2));
    p1.add(new JLabel("Table Data"));
   
    getContentPane().add(p1, BorderLayout.NORTH);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
    this.setVisible(true);
  }
}

You could easily separate the SQL part (around lines 52-68) into a method with the signature
public ResultSet getDataFor(String user, String password)
and that method could be in a different class. That would make the code easier to understand and maintain.

But ideally though it would be better to separate the GUI aspects from the SQL aspects completely. How this usually is done is by looking at the "meaning" of the data that is being returned - for example the result set may represent a number of Students, or CompactDisks, or Vehicles or whatever. In that case there will usually be a class that represents those things. Now you can have an SQL-based class that does the select and creates (eg) Student objects from the rows of the result set
public List<Student> getStudents(String userID, String password)
the GUI class can call that method, then populate the table with the data in those returned Student objects.
That way the SQL is kept entirely in one class, the GUI is kept entirely in another, and what links them is the Student class, which is independent of both the SQL and the GUI.

This question has already been answered. Start a new discussion instead.