title: Retrieve Data from MySQL DB and show in JTable
body: <p>I am using the following code for retrieving the data from MySQL Database. The code connects to the database properly. My problem is in retrieving the data from MySQL DB and showing it in the JTable.</p>
  
  <pre><code>package student;
  import java.awt.*;
  import javax.swing.*;
  import java.awt.*;
  import java.sql.*;
  import java.util.*;
  import javax.swing.*;
  import java.awt.event.*;
  import javax.swing.table.*;
  
  public class student1 extends javax.swing.JFrame {
      // public Connection Conn;
      Vector data = new Vector() ;
      Vector columnNames= new Vector();
  
      public student1() {
          initComponents();
          Connection conn;
      }
  
      public void Connection(){
          try
          {
              Class.forName("com.mysql.jdbc.Driver");
              String username = "root";
              String password = "root";
              String Database = "jdbc:mysql://localhost:3306/project";
              Connection conn = DriverManager.getConnection(Database, username, password);
              System.out.println("*** Connect to the database ***");
              String Query = "Select * from  StudentMaster";
              Statement smnt = conn.createStatement();
              ResultSet results = smnt.executeQuery( Query );
              ResultSetMetaData metaDt = results.getMetaData();
              System.out.println(metaDt);
  
              int cols = metaDt.getColumnCount();
              System.out.println("database" +cols);
              for(int i=1;i&lt;cols;i++){
                  columnNames.addElement (metaDt.getColumnName(i));
              }
              while(results.next()){
                  Vector row= new Vector(cols);
                  for(int i=1;i&lt;=cols;i++){
                      row.addElement(results.getObject(i));
                  }
                  data.addElement(row);
              }
              results.close();
              smnt.close();
              conn.close();
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
      }
  
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;
      private void initComponents() {
          jScrollPane1 = new javax.swing.JScrollPane();
          jTable1 = new javax.swing.JTable();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          jTable1.setModel(new javax.swing.table.DefaultTableModel(
              new Object [][] {
                  {null, null, null, null, null, null, null, null},
                  {null, null, null, null, null, null, null, null},
                  {null, null, null, null, null, null, null, null},
                  {null, null, null, null, null, null, null, null}
              },
              new String [] {
                  "StudId", "StudNo", "StudName", "StudClass", "StudMrk1", "StudMrk2", "StudTot", "StudRes"
              }
          ){
              Class[] types = new Class [] {
                  java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
              };
  
              public Class getColumnClass(int columnIndex) {
                  return types [columnIndex];
              }
          });
          jScrollPane1.setViewportView(jTable1);
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
                  .addContainerGap())
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addContainerGap(212, Short.MAX_VALUE))
          );
  
          pack();
      }// &lt;/editor-fold&gt;
  
      public static void main(String args[]) {
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new student1().setVisible(true);
                 student1 s1=new student1();
                 s1.Connection();
  
              }
          });
      }
  
      // Variables declaration - do not modify
      private javax.swing.JScrollPane jScrollPane1;
      private javax.swing.JTable jTable1;
      // End of variables declaration
  }
  </code></pre>
  
  <p>When this code is I executed, I get the following output. How can I solve my problem?</p>
  
  <pre><code>*** Connect to the database ***
  com.mysql.jdbc.ResultSetMetaData@1194a4e - Field level information: 
          com.mysql.jdbc.Field@15d56d5[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudId,originalColumnName=StudId,mysqlType=3(FIELD_TYPE_LONG),flags= PRIMARY_KEY, charsetIndex=63, charsetName=US-ASCII]
          com.mysql.jdbc.Field@efd552[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudNo,originalColumnName=StudNo,mysqlType=3(FIELD_TYPE_LONG),flags=, charsetIndex=63, charsetName=US-ASCII]
          com.mysql.jdbc.Field@19dfbff[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudName,originalColumnName=StudName,mysqlType=253(FIELD_TYPE_VAR_STRING),flags=, charsetIndex=8, charsetName=Cp1252]
          com.mysql.jdbc.Field@10b4b2f[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudClass,originalColumnName=StudClass,mysqlType=253(FIELD_TYPE_VAR_STRING),flags=, charsetIndex=8, charsetName=Cp1252]
          com.mysql.jdbc.Field@750159[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudMrk1,originalColumnName=StudMrk1,mysqlType=3(FIELD_TYPE_LONG),flags=, charsetIndex=63, charsetName=US-ASCII]
          com.mysql.jdbc.Field@1abab88[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudMrk2,originalColumnName=StudMrk2,mysqlType=3(FIELD_TYPE_LONG),flags=, charsetIndex=63, charsetName=US-ASCII]
          com.mysql.jdbc.Field@18a7efd[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudTot,originalColumnName=StudTot,mysqlType=3(FIELD_TYPE_LONG),flags=, charsetIndex=63, charsetName=US-ASCII]
          com.mysql.jdbc.Field@1971afc[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudRes,originalColumnName=StudRes,mysqlType=253(FIELD_TYPE_VAR_STRING),flags=, charsetIndex=8, charsetName=Cp1252]
  database8
  BUILD SUCCESSFUL (total time: 14 seconds)
  </code></pre>