package reader;
import java.awt.Choice;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import System_set.dbhelp;
import System_set.information_management;
import System_set.remind;
public class reader_update_delete extends information_management{
public reader_update_delete(){
id.setText("读者编号:");
deparment.setText("年 龄");
major.setText("有 效 证 件");
dorm.setText("证件号 :");
Class.setText("职 业");
J.setVisible(false);
b1.setVisible(false);
b2.setVisible(false);
final String[] columnname = { "读者编码", "姓名", "性别", "年龄", "职业", "证件",
"证件号", "电话","最大借书量","会员证有效日期","办证日期","押金"};
String sql = "select * from reader";
PreparedStatement pstm = null;
int count = 0;
ResultSet rs = null;
try {
pstm = dbhelp.xianjie().prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()) {
count++;
}
} catch (SQLException e) {
e.printStackTrace();
}
final Object[][] info = new Object[count][12];
try {
rs = pstm.executeQuery();
count = 0;
while (rs.next()) {
info[count][0] = rs.getString("id");
info[count][1] = rs.getString("name");
info[count][2] = rs.getString("sex");
info[count][3] = rs.getString("age");
info[count][4] = rs.getString("profession");
info[count][5] = rs.getString("prove");
info[count][6] = rs.getString("prove_id");
info[count][7] = rs.getString("telephone");
info[count][8] = rs.getString("max_borrow");
info[count][9] = rs.getString("effective_date");
info[count][10] = rs.getString("product_date");
info[count][11] = rs.getString("money");
count++;
}
final DefaultTableModel model = new DefaultTableModel(info,
columnname);
newtable = new JTable(model);
newtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
newtable.setRowSorter(new TableRowSorter<>(model));
newtable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int selectedRow = newtable.getSelectedRow();
Object oa = model.getValueAt(selectedRow, 0);
Object ob = model.getValueAt(selectedRow, 1);
Object oc = model.getValueAt(selectedRow, 2);
Object od = model.getValueAt(selectedRow, 3);
Object oe = model.getValueAt(selectedRow, 4);
Object of = model.getValueAt(selectedRow, 5);
Object og = model.getValueAt(selectedRow, 6);
Object oh = model.getValueAt(selectedRow, 7);
id_text.setText(oa.toString());
name_text.setText(ob.toString());
System.out.println(oc.toString().length());
String fandm = oc.toString();
if (fandm.equals("m"))// 放着判断f/
male.setSelected(true);
else
female.setSelected(true);
deparment_text.setText(od.toString());
major_text.setText(oe.toString());
Class_text.setText(of.toString());
dorm_text.setText(og.toString());
tel_text.setText(oh.toString());
}
});
rs.close();
pstm.close();
dbhelp.xianjie().close();
} catch (SQLException e) {
e.printStackTrace();
}
newJ = new JScrollPane(newtable);
newJ.setBounds(0, 25, 730, 200);
frame.add(newJ);
b11 = new JButton("修改");
b11.setBounds(240, 400, 80, 40);
frame.add(b11);
/*b11.setVisible(true);*/
b11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int aa = newtable.getSelectedRow();
if ((Integer) aa != null) {
info[aa][0] = id_text.getText();
info[aa][1] = name_text.getText();
/* info[aa][2]=sex_text.getText(); */
info[aa][3] = deparment_text.getText();
info[aa][4] = major_text.getText();
info[aa][5] = Class_text.getText();
info[aa][6] = dorm_text.getText();
info[aa][7] = tel_text.getText();
DefaultTableModel newmodel = (DefaultTableModel) newtable
.getModel();
newmodel.setDataVector(info, columnname);
newtable.updateUI();
String sql = "update reader set name='"
+ name_text.getText() + "'," + " age='"
+ deparment_text.getText() + "',profession='"
+ major_text.getText() + "'," + "prove='"
+ dorm_text.getText() + "',prove_id='"
+ Class_text.getText() + "'," + "telephone='"
+ tel_text.getText() + "' where id='"
+ id_text.getText() + "'";
Statement pstm = dbhelp.xianjie().createStatement();
pstm.executeUpdate(sql);
System.out.println(pstm.executeUpdate(sql));
JOptionPane.showMessageDialog(null, "修改成功");
}
} catch (ArrayIndexOutOfBoundsException ex) {
/*new remind("Select the user that needs to be modified",
"close",
"Select the user that needs to be modified");*/
ex.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
b2 = new JButton("删除");
b2.setBounds(430, 400, 80, 40);
frame.add(b2);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int aa = newtable.getSelectedRow();
try {
if (id_text.getText().length() == 0
|| deparment_text.getText().length() == 0
|| name_text.getText().length() == 0
|| major_text.getText().length() == 0
|| dorm_text.getText().length() == 0
|| dorm_text.getText().length() == 0)
new remind("Select the deleted user", "close",
"Select the deleted user");
String sql = "delete from reader where id='"
+ id_text.getText() + "'";
Statement pstm = dbhelp.xianjie().createStatement();
pstm.executeUpdate(sql);
} catch (SQLException e1) {
e1.printStackTrace();
}
if (id_text.getText().length() != 0
&& deparment_text.getText().length() != 0
&& name_text.getText().length() != 0
&& major_text.getText().length() != 0
&& dorm_text.getText().length() != 0
&& dorm_text.getText().length() != 0) {
frame.dispose();
new reader_update_delete();
}
}
});
}
public static void main(String[] args) {
new reader_update_delete();
}
}