0

i am first time asking question on stackexchange so don't know how to work on it

this is my code of jdialog where i am using jtable and getting data from sql but each time i press my FIND button in another jframe to load this jdialog first time it show no duplication as much as i press button to load jdialog it duplicate the same data as many times it shows the list of names in data base

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.GridBagLayout;

import javax.swing.JTextField;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JTable;

import java.util.Vector;

import javax.swing.JLabel;

import java.awt.Color;


public class FindStudent extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField textField;
    private JTextField textField_1;
    private JTable table;
    Connection conn = null;
    Statement stmt = null;
    static Vector<Vector<String>> data = new Vector<Vector<String>>();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            FindStudent dialog = new FindStudent();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public FindStudent() {
        Vector<String> columnNames = new Vector<String>();
       columnNames.add("RegNo");
       columnNames.add("StudentName");
       columnNames.add("FatherName");
       columnNames.add("Class");

        String query = "Select RegNo, StudentName, FatherName, Class from SchoolDB.dbo.StudentProfile";
        try{
            conn = DriverManager.getConnection("jdbc:sqlserver:" + "//" +
                    "localhost;1433" + "Database=SchooDB"+";integratedSecurity=true;");
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {

                Vector<String> vstring = new Vector<String>();


                vstring.add(rs.getString("RegNo"));
                vstring.add(rs.getString("StudentName"));
                vstring.add(rs.getString("FatherName"));
                vstring.add(rs.getString("Class"));
                data.add(vstring);
        }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        /*catch (SQLException e) {
            e.printStackTrace();
            }*/
        finally{
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }

        setBounds(100, 100, 430, 350);
        this.setAlwaysOnTop(true);
        this.setModal(true);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        GridBagLayout gbl_contentPanel = new GridBagLayout();
        gbl_contentPanel.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        gbl_contentPanel.rowHeights = new int[]{0, 0, 35, 0};
        gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        contentPanel.setLayout(gbl_contentPanel);
        {
            textField = new JTextField();
            GridBagConstraints gbc_textField = new GridBagConstraints();
            gbc_textField.anchor = GridBagConstraints.NORTH;
            gbc_textField.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField.gridwidth = 25;
            gbc_textField.insets = new Insets(0, 0, 10, 5);
            gbc_textField.gridx = 1;
            gbc_textField.gridy = 0;
            contentPanel.add(textField, gbc_textField);
            textField.setColumns(10);
        }
        {
            textField_1 = new JTextField();
            GridBagConstraints gbc_textField_1 = new GridBagConstraints();
            gbc_textField_1.gridwidth = 25;
            gbc_textField_1.insets = new Insets(0, 0, 10, 5);
            gbc_textField_1.anchor = GridBagConstraints.NORTH;
            gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField_1.gridx = 1;
            gbc_textField_1.gridy = 1;
            contentPanel.add(textField_1, gbc_textField_1);
            textField_1.setColumns(10);
        }
        {
            table = new JTable();
            GridBagConstraints gbc_table = new GridBagConstraints();
            gbc_table.insets = new Insets(0, 0, 0, 5);
            gbc_table.anchor = GridBagConstraints.NORTH;
            gbc_table.gridwidth = 3;
            gbc_table.gridx = 1;
            gbc_table.gridy = 2;

             DefaultTableModel model = new DefaultTableModel(data, columnNames);
                final JTable table = new JTable(model);/*{
                    @Override
                    public Dimension getPreferredScrollableViewportSize()
                    {
                        return new Dimension(100, 100);
                    }
                };*/
                table.setPreferredScrollableViewportSize(new Dimension(425, 200));
                table.setFillsViewportHeight(true);
                table.getColumnModel().getColumn(0).setPreferredWidth(90);
                table.getColumnModel().getColumn(1).setPreferredWidth(120);
                table.getColumnModel().getColumn(2).setPreferredWidth(120);
                table.getColumnModel().getColumn(3).setPreferredWidth(40);
                JScrollPane jsp = new JScrollPane(table);
                GridBagConstraints jsp_scroll = new GridBagConstraints();
                jsp_scroll.insets = new Insets(0, 0, 0, 5);
                jsp_scroll.fill = GridBagConstraints.BOTH;
                jsp_scroll.gridwidth = 25;
                jsp_scroll.gridx = 1;
                jsp_scroll.gridy = 3;

                contentPanel.add(jsp, jsp_scroll);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setBackground(Color.WHITE);
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setBackground(Color.WHITE);
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setBackground(Color.WHITE);
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

}
faisal
  • 19
  • 1
  • 8
  • 1
    1) *"in another jframe"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Words typed in all lower case are hard to read, like trying to listen to someone who is mumbling. Please use an upper case letter at the start of sentences, for the word I, and proper names like `ArrayList` or Oracle. – Andrew Thompson Jun 13 '16 at 10:47

3 Answers3

1
static Vector<Vector<String>> data = new Vector<Vector<String>>();

the data is a class level variable.

In your case you need not have a class level variable to store data.

instead you can have a local variable inside FindStudent() constructor.

Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21
0

Could you please move following statement:

Vector<Vector<String>> data = new Vector<Vector<String>>();

as the first statement inside your constructor:

public FindStudent() and see the result?

After the above change your class looks like as follows:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;


public class FindStudent extends JDialog {

private final JPanel contentPanel = new JPanel();
private JTextField textField;
private JTextField textField_1;
private JTable table;
Connection conn = null;
Statement stmt = null;    
/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        FindStudent dialog = new FindStudent();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Create the dialog.
 */
public FindStudent() {
    Vector<Vector<String>> data = new Vector<Vector<String>>();
    Vector<String> columnNames = new Vector<String>();
   columnNames.add("RegNo");
   columnNames.add("StudentName");
   columnNames.add("FatherName");
   columnNames.add("Class");

    String query = "Select RegNo, StudentName, FatherName, Class from SchoolDB.dbo.StudentProfile";
    try{
        conn = DriverManager.getConnection("jdbc:sqlserver:" + "//" +
                "localhost;1433" + "Database=SchooDB"+";integratedSecurity=true;");
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {

            Vector<String> vstring = new Vector<String>();


            vstring.add(rs.getString("RegNo"));
            vstring.add(rs.getString("StudentName"));
            vstring.add(rs.getString("FatherName"));
            vstring.add(rs.getString("Class"));
            data.add(vstring);
    }
    }
    catch (Exception e){
        e.printStackTrace();
    }
    /*catch (SQLException e) {
        e.printStackTrace();
        }*/
    finally{
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    setBounds(100, 100, 430, 350);
    this.setAlwaysOnTop(true);
    this.setModal(true);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBackground(Color.WHITE);
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    GridBagLayout gbl_contentPanel = new GridBagLayout();
    gbl_contentPanel.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    gbl_contentPanel.rowHeights = new int[]{0, 0, 35, 0};
    gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
    contentPanel.setLayout(gbl_contentPanel);
    {
        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.anchor = GridBagConstraints.NORTH;
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridwidth = 25;
        gbc_textField.insets = new Insets(0, 0, 10, 5);
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 0;
        contentPanel.add(textField, gbc_textField);
        textField.setColumns(10);
    }
    {
        textField_1 = new JTextField();
        GridBagConstraints gbc_textField_1 = new GridBagConstraints();
        gbc_textField_1.gridwidth = 25;
        gbc_textField_1.insets = new Insets(0, 0, 10, 5);
        gbc_textField_1.anchor = GridBagConstraints.NORTH;
        gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_1.gridx = 1;
        gbc_textField_1.gridy = 1;
        contentPanel.add(textField_1, gbc_textField_1);
        textField_1.setColumns(10);
    }
    {
        table = new JTable();
        GridBagConstraints gbc_table = new GridBagConstraints();
        gbc_table.insets = new Insets(0, 0, 0, 5);
        gbc_table.anchor = GridBagConstraints.NORTH;
        gbc_table.gridwidth = 3;
        gbc_table.gridx = 1;
        gbc_table.gridy = 2;

         DefaultTableModel model = new DefaultTableModel(data, columnNames);
            final JTable table = new JTable(model);/*{
                @Override
                public Dimension getPreferredScrollableViewportSize()
                {
                    return new Dimension(100, 100);
                }
            };*/
            table.setPreferredScrollableViewportSize(new Dimension(425, 200));
            table.setFillsViewportHeight(true);
            table.getColumnModel().getColumn(0).setPreferredWidth(90);
            table.getColumnModel().getColumn(1).setPreferredWidth(120);
            table.getColumnModel().getColumn(2).setPreferredWidth(120);
            table.getColumnModel().getColumn(3).setPreferredWidth(40);
            JScrollPane jsp = new JScrollPane(table);
            GridBagConstraints jsp_scroll = new GridBagConstraints();
            jsp_scroll.insets = new Insets(0, 0, 0, 5);
            jsp_scroll.fill = GridBagConstraints.BOTH;
            jsp_scroll.gridwidth = 25;
            jsp_scroll.gridx = 1;
            jsp_scroll.gridy = 3;

            contentPanel.add(jsp, jsp_scroll);
    }
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setBackground(Color.WHITE);
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            JButton okButton = new JButton("OK");
            okButton.setBackground(Color.WHITE);
            okButton.setActionCommand("OK");
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);
        }
        {
            JButton cancelButton = new JButton("Cancel");
            cancelButton.setBackground(Color.WHITE);
            cancelButton.setActionCommand("Cancel");
            buttonPane.add(cancelButton);
        }
    }
 }

}
Sanjeev Saha
  • 2,632
  • 1
  • 12
  • 19
0

Either make data non-static, or do data.clear() before you start iterating thru your result set.

Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21