import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class teaPanel extends JPanel {
private JComboBox comboBox_1;
private JComboBox comboBox;
private JCheckBox checkBox_6;
/**
*
*/
private static final long serialVersionUID = 6678676176512765166L;
private JTextField textField_7;
private JTextField textField_6;
private JButton button_5;
JButton button_2,button,button_1,button_3,button_4 ;
private JTable table;
private ButtonGroup buttonGroup = new ButtonGroup();
private JTextField textField_2;
private JTextField textField_1;
private JTextField textField;
private JCheckBox checkBox,checkBox_1,checkBox_2,checkBox_3,checkBox_4;
private JLabel label,label_1;
private JRadioButton radioButton,radioButton_1;
private JScrollPane scrollPane;
String zhuanye[]={"软件工程","计算机","数字媒体","动漫","数学","俄语","英语"};
String kecheng[]={"Java语言","操作系统","计算机原","数据库","计组","高等数学1","离散数学","数值计算","大学英语"};
String teainf[]={"教工号","姓名","年龄","性别","系别"};
DataBase db;
Connection con=null;
Statement st=null;
ResultSet rs=null;
String sql;
DefaultTableModel teaTableModel;
Teacher tea=null;
/**
* Create the panel
*/
public teaPanel() {
super();
setVisible(true);
setLayout(null);
checkBox = new JCheckBox();
checkBox.setText("教工号:");
checkBox.setBounds(0, 10, 79, 26);
add(checkBox);
checkBox_1 = new JCheckBox();
checkBox_1.setText("姓名:");
checkBox_1.setBounds(178, 10, 64, 26);
add(checkBox_1);
checkBox_2 = new JCheckBox();
checkBox_2.setText("年龄:");
checkBox_2.setBounds(0, 42, 64, 26);
add(checkBox_2);
textField = new JTextField();
textField.setBounds(248, 10, 87, 26);
add(textField);
textField_1 = new JTextField();
textField_1.setBounds(85, 10, 87, 26);
add(textField_1);
textField_2 = new JTextField();
textField_2.setBounds(85, 42, 39, 26);
add(textField_2);
radioButton = new JRadioButton();
buttonGroup.add(radioButton);
radioButton.setText("男");
radioButton.setSelected(true);
radioButton.setBounds(248, 42, 39, 26);
add(radioButton);
checkBox_3 = new JCheckBox();
checkBox_3.setText("性别:");
checkBox_3.setBounds(178, 42, 64, 26);
add(checkBox_3);
radioButton_1 = new JRadioButton();
buttonGroup.add(radioButton_1);
radioButton_1.setText("女");
radioButton_1.setBounds(293, 42, 39, 26);
add(radioButton_1);
checkBox_4 = new JCheckBox();
checkBox_4.setText("系别:");
checkBox_4.setBounds(0, 81, 64, 26);
add(checkBox_4);
button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
db=new DataBase();
con=db.getCon();
st=con.createStatement();
}catch(SQLException e2){
JOptionPane.showMessageDialog(null, "数据连接错误!");
System.exit(0);
}
String hasjiaogonghao="",hasxingming="",hasnianling="",hasxingbie="",hasxihao="",haskecheng="",needshouke="";
if(checkBox.isSelected()){
if(textField_1.getText().equals("")){
JOptionPane.showMessageDialog(null, "请输入要查询的教工号!");
}
hasjiaogonghao=" and teacher.教工号='"+textField_1.getText()+"'";
}
if(checkBox_1.isSelected()){
if(textField.getText().equals("")){
JOptionPane.showMessageDialog(null, "请输入教师姓名!");
}
else{
hasxingming=" and 姓名='"+textField.getText()+"'";
}
}
if(checkBox_2.isSelected()){
if(textField_2.getText().equals("")){
JOptionPane.showMessageDialog(null, "请输入教师的年龄!");
}
else{
hasnianling=" and 年龄='"+textField_2.getText()+"'";
}
}
if(checkBox_3.isSelected()){
if(radioButton.isSelected()){
hasxingbie=" and 性别='"+radioButton.getText()+"'";
}
else{
hasxingbie=" and 性别='"+radioButton_1.getText()+"'";
}
}
if(checkBox_4.isSelected()){
sql="select 系号 from depart where 系别='"+comboBox_1.getSelectedItem()+"'";
System.out.println(sql);
try{
System.out.println(sql);
rs=st.executeQuery(sql);
if(rs.next()){
hasxihao=" and teacher.系号='"+rs.getString(1)+"'";
rs.close();
}
else{
//donothing
}
}catch(SQLException e1){
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "执行SQL语句"+sql+"时出错!");
}
}
if(checkBox_6.isSelected()){
needshouke=",shouke,course ";
haskecheng=" and teacher.教工号=shouke.教工号 and shouke.课程号=course.课程号 and course.课程名='"+comboBox.getSelectedItem()+"'";
}
sql="select teacher.教工号,teacher.姓名,teacher.年龄,teacher.性别,depart.系别 from teacher,depart"+needshouke+" where teacher.系号=depart.系号 "+haskecheng+hasjiaogonghao+hasnianling+hasxingbie+hasxingming+hasxihao ;
try {
String[] s = new String[5];
int rows = teaTableModel.getRowCount();
if (rows != 0){
for (int i = 0; i < rows; i++)
teaTableModel.removeRow(0);}
int i=0;
if(!checkBox.isSelected()&&!checkBox_1.isSelected()&&!checkBox_2.isSelected()&&!checkBox_3.isSelected()&&!checkBox_4.isSelected()&&!checkBox_6.isSelected()){
JOptionPane.showMessageDialog(null, "请选择你要查询的条件和内容!");
}
else if((checkBox.isSelected()&&textField_1.getText().equals(""))||
(checkBox_1.isSelected()&&textField.getText().equals(""))||
(checkBox_2.isSelected()&&textField_2.getText().equals(""))||
(checkBox.isSelected()&&textField.getText().equals(""))
){
//donothing
}
else{
System.out.println(sql);
rs=st.executeQuery(sql);
while (rs.next()) {
for ( i = 1; i <= 5; i++)
s[i - 1] = rs.getString(i);
teaTableModel.addRow(s);
}
if(i==0){
JOptionPane.showMessageDialog(null, "未找到与该条件匹配的教师!");
}
}} catch (SQLException e2) {
JOptionPane.showMessageDialog(null, "执行SQL语句"+sql+"时出错!");
e2.printStackTrace();
}
}
});
button.setText("查询");
button.setBounds(341, 9, 63, 28);
add(button);
button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
db=new DataBase();
con=db.getCon();
st=con.createStatement();
}catch(SQLException e2){
JOptionPane.showMessageDialog(null, "数据连接错误!");
System.exit(0);
}
tea=new Teacher();
if(!checkBox.isSelected()){
JOptionPane.showMessageDialog(null, "教工号必须选择!");
}
else if(checkBox.isSelected()&&textField_1.getText().equals("")){
JOptionPane.showMessageDialog(null, "请填写教工号!");
}
else{
tea.setTno(textField_1.getText());
sql="select * from teacher where 教工号='"+tea.getTno()+"'";
try {
System.out.println(sql);
rs=st.executeQuery(sql);
if(rs.next()){
System.out.println("you");
tea.setTname(rs.getString(2));
tea.setAge(rs.getInt(4));
tea.setSex(rs.getString(3));
tea.setXihao(rs.getString(5));
if(checkBox_1.isSelected()){
if(textField.getText().equals("")){
JOptionPane.showMessageDialog(null, "请输入教师姓名!");
}
else{
tea.set
- 1
- 2
前往页