该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
第二段
package z1;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class StudentInfo extends JFrame implements ActionListener{
JButton displayBtn,addBtn,searchBtn,deleteBtn,updateBtn;
JTextArea displayJta;
JScrollPane jsp;
public StudentInfo(){
super("学生信息管理小工具");
displayBtn=new JButton("浏览");
displayBtn.addActionListener(this);
addBtn=new JButton("增加");
searchBtn=new JButton("查看");
deleteBtn=new JButton("删除");
updateBtn=new JButton("修改");
displayJta=new JTextArea(10,40);
jsp=new JScrollPane(displayJta);
Container con=this.getContentPane();
con.setLayout(new FlowLayout());
con.add(displayBtn);
con.add(addBtn);
con.add(searchBtn);
con.add(deleteBtn);
con.add(updateBtn);
con.add(jsp);
setSize(480,280);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String s="";
if(e.getSource()==displayBtn){
flag=1;
Conn cc=new Conn();
try{
s=cc.display();
}catch(SQLException|ClassNotFoundException e1){
e1.printStackTrace();
}
displayJta.setText(s);
}
if(e.getSource()==addBtn){
flag=2;
new AddOrUpdate(this,"增加学生信息",true);
}
}
}
class AddOrUpdate extends JDialog implements ActionListener{
JLabel idLb,nameLb,sexLb,ageLb;
JTextField idTf,nameTf,sexTf,ageTf;
JButton OkBtn,CancelBtn;
JFrame jf;
public AddOrUpdate(JFrame jf,String title,boolean model){
super(jf,title,model);
this.jf=jf;
idLb=new JLabel("学号:");
nameLb=new JLabel("姓名:");
sexLb=new JLabel("性别:");
ageLb=new JLabel("年龄:");
if(flag==2){
idTf=new JTextField(10);
nameTf=new JTextField(10);
sexTf=new JTextField(10);
ageTf=new JTextField(10);
}else{
idTf=new JTextField(id,10);
nameTf=new JTextField(name,10);
sexTf=new JTextField(sex,10);
ageTf=new JTextField(age+"",10);
idTf.setEditable(false);
}
OkBtn=new JButton("确定");
OkBtn.addActionListener(this);
CancelBtn=new JButton("取消");
CancelBtn.addActionListener(this);
Container con=this.getContentPane();
con.setLayout(new FlowLayout());
con.add(idLb);
con.add(idTf);
con.add(nameLb);
con.add(nameTf);
con.add(sexLb);
con.add(sexTf);
con.add(ageLb);
con.add(ageTf);
con.add(OkBtn);
con.add(CancelBtn);
this.setLocation(jf.getLocation().x+(jf.getWidth()-180)/2,jf.getLocation().y+(jf.getHeight()-210)/2);
this.setSize(180, 210);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0){
String sql,tid,tname,tsex,tage;
tid=idTf.getText();
tname=nameTf.getText();
tsex=sexTf.getText();
tage=ageTf.getText();
if(flag==2){
sql="insert into studeninfo values('"+tid+"','"+tname+"','"+tsex+"',"+tage+")";
}else{
sql="update studeninfo set studetnname='"+tname+"',studentsex='"+tsex+"',studentage="+"tage+where studentID='"+tid+"'";
}
if(arg0.getSource()==OkBtn){
this.hide();
try{
Connection cc=new Conn().getDBConnection();
Statement sm=cc.createStatement();
sm.executeUpdate(sql);
}catch(SQLException|ClassNotFoundException e1){
e1.printStackTrace();
JOptionPane.showMessageDialog(this,"该学生已经存在","提示",1);
}
}else{
this.hide();
}
}
public static void main(String[]args){
StudentInfo stu=new StudentInfo();
}
}