Source Code : database ResultSet into JTable

database ResultSet into JTable

"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);
  }
}