2016-06-13 5 views
0

私は、これは私の私は、JTableの使用およびSQL からデータを取得していますJDialogののコードが、毎回iはそれ重複が

上で動作する方法がわからないstackexchangeに質問をして初めてですそれは

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

} 
+1

1)* "別のjframeで" * [複数のJFramesの使用、良い/悪い習慣?](http://stackoverflow.com/q/9554636/418556)2)すべての小文字でタイプされた単語は、読書が難しい、混乱している人の話を聞くなど。文頭に大文字、Iという単語、 'ArrayList'やOracleなどの適切な名前を使用してください。 –

答えて

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

データは、クラスレベルの変数です。

あなたのケースでは、データを格納するためにクラスレベルの変数を持つ必要はありません。

代わりに、FindStudent()コンストラクタ内にローカル変数を持たせることができます。

0
データベースに名前のリストを示し 何回同じデータを複製し、それは私がそれJDialogのロードするためにボタンを押していない重複限りを示し、このJDialogの最初の時間をロードするために、別のJFrameの私の検索ボタンを押してください

次の文をお送りください:

あなたのコンストラクタ内の最初のステートメントとして

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

public FindStudent()し、その結果を参照してください?あなたのクラスは次のようにのように見える上記変更後

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

} 
0

dataを非静的にするか、data.clear()を実行して結果セットを反復処理します。