📄 personframe.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public abstract class PersonFrame extends JFrame{
public PersonFrame(){
setTitle("Personal Information");
jl_sno=new JLabel("学号");
jl_sname=new JLabel("姓名");
jl_sex=new JLabel("性别");
jl_age=new JLabel("年龄");
jl_class=new JLabel("班级");
jtf_sno=new JTextField("",10);
jtf_sname=new JTextField("",10);
jtf_sex=new JTextField("",10);
jtf_age=new JTextField("",10);
jtf_class=new JTextField("",10);
Container c=this.getContentPane();
c.setLayout(new GridLayout(6,2));
c.add(jl_sno);c.add(jtf_sno);
c.add(jl_sname);c.add(jtf_sname);
c.add(jl_sex);c.add(jtf_sex);
c.add(jl_age);c.add(jtf_age);
c.add(jl_class);c.add(jtf_class);
}
JLabel jl_sno,jl_sname,jl_sex,jl_age,jl_class;
JTextField jtf_sno,jtf_sname,jtf_sex,jtf_age,jtf_class;
}
class QueryStudentFrame extends PersonFrame{
public QueryStudentFrame(Connection connect,String us){
jtf_sno.setEditable(false);
jtf_sname.setEditable(false);
jtf_sex.setEditable(false);
jtf_age.setEditable(false);
jtf_class.setEditable(false);
con=connect;
guest=us;
showValues();
}
protected void showValues(){
try{
String sql="select * from student where sno=?";
PreparedStatement preState=con.prepareStatement(sql);
preState.setString(1, guest);
ResultSet rs=preState.executeQuery();
rs.next();
jtf_sno.setText(rs.getString(1));
jtf_sname.setText(rs.getString(2));
jtf_sex.setText(rs.getString(3));
jtf_age.setText(rs.getInt(4)+"");
jtf_class.setText(rs.getString(5));
this.validate();
rs.close();
preState.close();
con.close();
}catch(SQLException e1){
JOptionPane.showMessageDialog(null, e1.getMessage());
return;
//e1.getMessage()uuuuuuuu
}
}
private Connection con;
private String guest;
}
///////////////////////////////////////////////////
//////////////////////////////////////////////////
//////////////////////////////////////////////////
/////////////////////////////////////////////////////
class StudentFrame extends PersonFrame{
public StudentFrame(Connection connect){
jb_find=new JButton("find");
jb_delete=new JButton("delete");
jb_update=new JButton("alter");
jb_ok=new JButton("insert");
JPanel c=new JPanel(new GridLayout(2,2));
c.add(jb_find);
c.add(jb_delete);
c.add(jb_update);
c.add(jb_ok);
getContentPane().add(c);
con=connect;
////////添加响应
jb_find.addActionListener(new FindListener());
jb_delete.addActionListener(new DeleteListener());
jb_update.addActionListener(new UpdateListener() );
jb_ok.addActionListener(new OKListener());
}
class FindListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
String s=jtf_sno.getText();
String sql="select * from student where sno =?";
PreparedStatement pre=con.prepareStatement(sql);
pre.setString(1, s);
rs=pre.executeQuery();
if(!rs.next()){
JOptionPane.showMessageDialog(null, "Can not find");
return;
}
jtf_sname.setText(rs.getString(2));
jtf_sex.setText(rs.getString(3));
jtf_age.setText(rs.getInt(4)+"");
jtf_class.setText(rs.getString(5));
pre.close();
}catch(SQLException e1){
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
}
class DeleteListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
String s=jtf_sno.getText();
String sql="delete from dept where dno=?";
PreparedStatement pre=con.prepareStatement(sql);
pre.setString(1, s);
if(pre.executeUpdate()!=0)
JOptionPane.showMessageDialog(null, "delete successfully!");
else
JOptionPane.showMessageDialog(null, "delete error!");
jtf_sno.setText("");
jtf_sname.setText("");
}catch(SQLException e1){
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
}
class UpdateListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
s1=jtf_sno.getText();
if(s1.equals("")){
JOptionPane.showMessageDialog(null, "Can not Update!");
return;
}
try{
String s=jtf_sno.getText();
String sql="delete from student where sno=?";
PreparedStatement pre=con.prepareStatement(sql);
pre.setString(1, s);
pre.executeUpdate();
}catch(SQLException e1){
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
}
class OKListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
if(jtf_sno.getText().equals("")){
JOptionPane.showMessageDialog(null, "Can not Update!");
return;
}
String sql="insert into dept values(?,?,?,?,?)";
PreparedStatement pre=con.prepareStatement(sql);
pre.setString(1, jtf_sno.getText());
pre.setString(2, jtf_sname.getText());
pre.setString(3, jtf_sex.getText());
pre.setInt(4, Integer.parseInt(jtf_age.getText()));
pre.setString(5, jtf_class.getText());
if(pre.executeUpdate()!=0)
JOptionPane.showMessageDialog(null, "update successfully!");
}catch(Exception e1){
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
}
private JButton jb_find,jb_delete,jb_update,jb_ok;
Connection con;
String s1;
ResultSet rs;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -