📄 xueshengguanli.java
字号:
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class xueshengguanli extends JFrame implements ActionListener
{
Connection con;
ResultSet rs;
Statement st;
JButton button1,button2,button3;
JTextField text1,text2,text3,text4,text5,text6,text7;
String sid1=new String();
String sname1=new String();
String ssex1=new String();
String sage1=new String();
String stel1=new String();
String saddress1=new String();
String sclass1=new String();
public void xueshengguanli()
{
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
text4=new JTextField(10);
text5=new JTextField(10);
text6=new JTextField(10);
text7=new JTextField(10);
button1=new JButton("查询学生信息");
button1.addActionListener(this);
button2=new JButton("修改学生信息");
button2.addActionListener(this);
button3=new JButton("退出学生管理");
button3.addActionListener(this);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("学生个人信息",JLabel.CENTER));
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("学号:",JLabel.CENTER));
box2.add(text1);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("姓名:",JLabel.CENTER));
box3.add(text2);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("性别:",JLabel.CENTER));
box4.add(text3);
Box box5=Box.createHorizontalBox();
box5.add(new JLabel("年龄:",JLabel.CENTER));
box5.add(text4);
Box box6=Box.createHorizontalBox();
box6.add(new JLabel("电话:",JLabel.CENTER));
box6.add(text5);
Box box7=Box.createHorizontalBox();
box7.add(new JLabel("住址:",JLabel.CENTER));
box7.add(text6);
Box box8=Box.createHorizontalBox();
box8.add(new JLabel("班级:",JLabel.CENTER));
box8.add(text7);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(box7);
boxH.add(box8);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(button1);
pSouth.add(button2);
pSouth.add(button3);
add(pSouth,BorderLayout.SOUTH);
setVisible(true);
setBounds(100,50,400,300);
validate();
}
public void actionPerformed(ActionEvent e)
{ try{//连接数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e2)
{
System.out.println(""+e2);
}
try{
con=DriverManager.getConnection("jdbc:odbc:lhm","","");
st=con.createStatement();
rs=st.executeQuery("Select * FROM xinxi");
}
catch(SQLException el){}//连接数据库
if(e.getSource()==button2)
{
String id1=new String();//提取学生学号
id1=text1.getText().trim();
int count=0;
try{
while(rs.next())
{ sid1=rs.getString("studentid");
if(id1.equals(sid1))
{++count;}
}
}catch(Exception ee){}
if(count==0){JOptionPane.showMessageDialog(null,"数据库中无该学生信息,无法修改!");}
else
{sname1=text2.getText().trim();
ssex1=text3.getText().trim();
sage1=text4.getText().trim();
stel1=text5.getText().trim();
saddress1=text6.getText().trim();
sclass1=text7.getText().trim();
String strSQL3 ="update xinxi set studentname='"+sname1+"',studentsex='"+ssex1+"',studentage='"+sage1+"',studenttel='"+stel1+"',studentaddress='"+saddress1+"',studentclass='"+sclass1+"'"+"where studentid='"+id1+"'";
try
{
st.executeUpdate(strSQL3);
}catch(Exception exx)
{}
JOptionPane.showMessageDialog(null,"恭喜您,修改成功!");
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
text5.setText("");
text6.setText("");
text7.setText("");
}
}
if(e.getSource()==button3){setVisible(false);}
if(e.getSource()==button1)
{try{//连接数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e2)
{
System.out.println(""+e2);
}
try{
con=DriverManager.getConnection("jdbc:odbc:lhm","","");
st=con.createStatement();
rs=st.executeQuery("Select * FROM xinxi");
}
catch(SQLException el){}//连接数据库
String id1=new String();//提取学生学号
id1=text1.getText();
int count=0;
try{
while(rs.next())
{sid1=rs.getString("studentid");
if(id1.equals(sid1))
{sname1=rs.getString("studentname");
ssex1=rs.getString("studentsex");
sage1=rs.getString("studentage");
stel1=rs.getString("studenttel");
saddress1=rs.getString("studentaddress");
sclass1=rs.getString("studentclass");
text2.setText(sname1);
text3.setText(ssex1);
text4.setText(sage1);
text5.setText(stel1);
text6.setText(saddress1);
text7.setText(sclass1);
count++;
}
}
}catch(Exception ee){}
if(count==0){JOptionPane.showMessageDialog(null,"数据库中无该学生信息,请查证后在查找!");text1.setText("");}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -