package com.njit202150903;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import javax.swing.table.*;import com.njit.personadd.personadd;import db.db;
import entity.PersonEntity;import java.util.ArrayList;public class personselect extends JFrame{
private JTable table;
private MyTableModel tablemodel;
private JButton b1;
private JButton b2;
private JButton b3;
private JToolBar tool;

public personselect(){
this.setSize(600,600);
this.setTitle("员工信息");
this.setLocationRelativeTo(getOwner());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

tablemodel = getModel();
table = new JTable(tablemodel);
table.setPreferredScrollableViewportSize(new Dimension(500,250));
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll,BorderLayout.CENTER);

b1 = new JButton("添加");
b1.setFocusable(false);
b1.setHorizontalTextPosition(SwingConstants.CENTER);
b1.setVerticalTextPosition(SwingConstants.BOTTOM);
b2 = new JButton("修改");
b2.setFocusable(false);
b2.setHorizontalTextPosition(SwingConstants.CENTER);
b2.setVerticalTextPosition(SwingConstants.BOTTOM);
b3=new JButton("删除");
b3.setFocusable(false);
b3.setHorizontalTextPosition(SwingConstants.CENTER);
b3.setVerticalTextPosition(SwingConstants.BOTTOM);
tool = new JToolBar();
tool.add(b1);
tool.add(b2);
tool.add(b3);
tool.setRollover(true);
getContentPane().add(tool,BorderLayout.NORTH);

b1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
personadd pdd = new personadd();
pdd.setVisible(true);
dispose();

}
});

b2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int i,j,index=0,count;
db dbcon = new db();
if(table.getCellEditor()!=null){
table.getCellEditor().stopCellEditing();

}
try{
String sql="update person set name=?,sex=?,birthday=?,professor=?,deptno=?,nation=? where no=?";
PreparedStatement presta=dbcon.PreparedStatement(sql);
count = tablemodel.getEditedIndex().size();
if(count>0){
for(i=0;i<count;i++){
for(j=0;j<table.getColumnCount();j++){
index = tablemodel.getEditedIndex().get(i);
presta.setString(j,table.getValueAt(index, j).toString());
}
presta.setString(j, table.getValueAt(index, 0).toString());
presta.addBatch();
}
}
presta.executeBatch();
}catch(SQLException sqle){
System.out.println(sqle.toString());
}
}
});

b3.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub


}
});

pack();

}

private MyTableModel getModel(){
MyTableModel tableModel = new MyTableModel();

db dbcon;

try{
dbcon = new db();
ResultSet rs = dbcon.executeQuery("select no as 工号, name as 姓名, sex as 性别, birthday as 出生日期, professor as 职称, deptno as 部门编号, nation as 国籍 from person");
ResultSetMetaData rsmd = rs.getMetaData();
int Colnum = rsmd.getColumnCount();
int i;
for(i=1;i<=Colnum;i++)
tableModel.addColumn(rsmd.getColumnName(i));
ArrayList<PersonEntity> v = new ArrayList<PersonEntity>();
while(rs.next()){
PersonEntity person = new PersonEntity();
person.setNo(rs.getString("工号"));
person.setName(rs.getString("姓名"));
person.setSex(rs.getString("性别"));
person.setBirthday(rs.getDate("出生日期"));
person.setProfessor(rs.getString("职称"));
person.setDeptno(rs.getString("部门编号"));
person.setNation(rs.getString("国籍"));

v.add(person);
}
rs.close();

for(i=0;i<v.size();i++){
tableModel.addRow(new Object[]{v.get(i).getNo(),v.get(i).getName(),
                       v.get(i).getSex(),v.get(i).getBirthday(),
                       v.get(i).getProfessor(),v.get(i).getDeptno(),v.get(i).getNation()});
}
dbcon.closeConn();
}catch(SQLException sqle){
System.out.println(sqle.toString());
}catch(Exception e){
System.out.println(e.getMessage());
}
return tableModel;
}

public static void main(String[] args){
personselect w = new personselect();
w.setVisible(true);
}
}class MyTableModel extends DefaultTableModel{
private ArrayList<Integer> editedIndex = new ArrayList<Integer>();

public MyTableModel(){
super();
}
public boolean isCellEditable(int row,int column){
if(column==0){
return false;
}else
return true;
}

public void setValueAt(Object aValue, int row, int column){
super.setValueAt(aValue, row, column);
int i,count=editedIndex.size();
if(count==0)
editedIndex.add(row);
else{
for(i=0;i<count;i++){
if(editedIndex.get(i).intValue()>row){
editedIndex.add(i+1,row);
break;
}
}
if(i>=count)
editedIndex.add(row);
}
}
public ArrayList<Integer> getEditedIndex(){
return editedIndex;
}
}修改时出现错误
Connection Successful!
Connection Successful!
com.microsoft.sqlserver.jdbc.SQLServerException: 索引 0 超出范围。求助怎么改才能成功