我第一次在 stackexchange 上提问,所以不知道如何处理
这是我的 jdialog 代码,我使用 jtable 并从 sql 获取数据 但是每次我在另一个 jframe 中按下 FIND 按钮来加载此 jdialog 时,它都会显示没有重复,就像我按下按钮加载 jdialog 一样,它会多次重复相同的数据 它显示数据库中的名称列表
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);
}
}
}
}
最佳答案
static Vector<Vector<String>> data = new Vector<Vector<String>>();
数据是类级别变量。
在您的情况下,您不需要使用类级别变量来存储数据。
相反,您可以在 FindStudent() 构造函数中使用局部变量。
关于java - jtable 条目重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37787213/