在这段代码中,我从数据库获取数据,该数据以行和列表示,但我没有从数据库获取第五列数据。当我执行提交操作时,我通过键盘输入该数据 按钮,当时我想删除那些可以填满第五列的记录。如果第五列中没有输入记录,则该行无法删除。但问题是有些记录已删除,有些记录未删除 意味着我没有得到我想要的,并且生成了一些显示数组索引越界异常
public class Add_EmployeeAttendance {
DBConnection connect = new DBConnection();
static ArrayList emp_code,No_Of_Working_Days,Wages,Professional_Tax,Provisional_Tax,Eisc,Canteen,Gross_Salary1;
public static final String[] COLUMN_NAMES = { "CompanyName", "Department",
"Designation", "Emp_Code", "Emp_Name", "working_days " };
public static String Company_name,Department,Designation;
private DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
public static int a = 0;
private JTable table = new JTable(model);
public JComboBox <String>b, b1, b2;
public static String strres = null, getdesignation_id = null,
strdept_id = null;
private JPanel mainPanel = new JPanel(new BorderLayout());
public class TableDataDelete implements ActionListener
{
public void actionPerformed(ActionEvent ee) {
int count=0;
if(table.getCellEditor() != null)
{
int count1=0;
table.getCellEditor().stopCellEditing();
int b = table.getRowCount();
System.out.println("row count" + b);
for (int i = 0; i <b; i++) {
try
{
if(table.getCellEditor(i, 5) != null)
{
System.out.println("hit count is "+count1);
count1++;
String str1 = (String) table.getModel().getValueAt(i, 5);
System.out.println("second value"+model.getValueAt(i,5));
if (!(str1 == null)) {
((DefaultTableModel)table.getModel()).removeRow(i);
table.repaint();
System.out.println("remove row no is"+i);
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array index out of bound exception"+e);
}
}
}
}
}
public Add_EmployeeAttendance() {
JPanel buttonPanel = new JPanel();
JPanel buttonPanel1 = new JPanel();
b = new JComboBox();
b.addItem("Please select Company");
connect.getConnection();
Statement st = null;
try {
st = connect.conn.createStatement();
ResultSet rs1 = st
.executeQuery("select company_name from company_details");
while (rs1.next()) {
b.addItem(rs1.getString(1));
System.out.println(rs1.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
table.setFocusable(true);
b.addItem("Please Select Company");
buttonPanel.add(b);
b1 = new JComboBox();
b1.addItem("Please Select Department");
buttonPanel.add(b1);
b2 = new JComboBox();
b2.addItem("Please Select Designation");
buttonPanel.add(b2);
b.addItemListener(new companyEvents());
b1.addItemListener(new Designation());
b2.addItemListener(new getDesignation_id());
JButton addDataButton = new JButton("Show Data");
buttonPanel.add(addDataButton);
JButton submit = new JButton("View Salary ");
buttonPanel1.add(submit);
JButton submit1=new JButton("submit");
buttonPanel1.add(submit1);
submit.addActionListener(new TableDataRetriving());
submit1.addActionListener(new TableDataDelete());
addDataButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
model.setRowCount(0);
addDataActionPerformed();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.PAGE_START);
mainPanel.add(buttonPanel1, BorderLayout.PAGE_END);
new DefaultListCellRenderer();
emp_code=new ArrayList();
No_Of_Working_Days=new ArrayList();
Wages=new ArrayList();
Professional_Tax=new ArrayList();
Provisional_Tax=new ArrayList();
Eisc=new ArrayList();
Canteen=new ArrayList();
Gross_Salary1=new ArrayList();
}
private void addDataActionPerformed() throws SQLException {
connect.getConnection();
Object[] row = new Object[COLUMN_NAMES.length];
PreparedStatement stmt1 = connect.conn
.prepareStatement("select emp_id,employee_code from employee_master where company_id=? and dept_id=? and designation_id=?");
stmt1.setString(1, strres);
stmt1.setString(2, strdept_id);
stmt1.setString(3, getdesignation_id);
ResultSet resemp = stmt1.executeQuery();
String str1 = null, str2 = null, str3 = null;
while (resemp.next()) {
// for (int j = 0; j < row.length; j++) {
PreparedStatement stmt = connect.conn
.prepareStatement("select * from personal_details where emp_id=? ");
stmt.setString(1, resemp.getString(1));
System.out.println("empid is" + resemp.getString(1));
ResultSet res1 = stmt.executeQuery();
while (res1.next()) {
str1 = res1.getString(3);
str2 = res1.getString(4);
str3 = res1.getString(5);
}
row[0] = String.valueOf(b.getSelectedItem());
row[1] = String.valueOf(b1.getSelectedItem());
row[2] = String.valueOf(b2.getSelectedItem());
row[3] = resemp.getString(2);
row[4] = str1 + " " + str2 + " " + str3;
table.setRowHeight(30);
model.addRow(row);
}
a = table.getModel().getRowCount();
}
public JComponent getComponent() {
return mainPanel;
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Employee Details");
frame.getContentPane().add(
new Add_EmployeeAttendance().getComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 650);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
最佳答案
基于此堆栈跟踪:
array index out of bound exceptionjava.lang.ArrayIndexOutOfBoundsException: 6 >= 6
array index out of bound exceptionjava.lang.ArrayIndexOutOfBoundsException: 7 >= 6
array index out of bound exceptionjava.lang.ArrayIndexOutOfBoundsException: 8 >= 6
您的问题在这里:
int b = table.getRowCount();
for (int i = 0; i < b; i++) {
...
if (!(str1 == null)) {
((DefaultTableModel)table.getModel()).removeRow(i);
...
}
...
}
如果您在 for
循环中从表模型中删除一行,则 b
将不再等于 table.getRowCount()
但更大,导致 ArrayIndexOutOfBoundsException
。
我会将其替换为:
DefaultTableModel model = (DefaultTableModel)table.getModel();
for (int i = model.getRowCount() - 1; i >= 0; i--) { // ends condition is updated dinamically
...
if (str1 != null) {
model.removeRow(i);
...
}
...
}
旁注
当模型数据发生更改(插入/更新/删除)时,无需重新绘制表格。在这种情况下,模型将通知 View ,并且此 View 将会更新。
关于java - 从Jtable中删除非空记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22686736/