📄 student.java.bak
字号:
//调用方法
conDB();
}
//以下是连接数据库
public void conDB(){
try{ //加载数据库驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null,"数据库加载失败!");
}
try{ //连接数据库
conn = DriverManager.getConnection("jdbc:odbc:studb");
st = conn.createStatement();
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,"数据库连接失败!");
}
}
//以下是加载数据库
public void closeDB(){
try{
st.close(); conn.close();
}
catch(SQLException e ){
JOptionPane.showMessageDialog(null,"数据库关闭失败!");
}
}
//以下是针对数据库的各种操作
public void actionPerformed(ActionEvent e){
if(e.getSource()==search){
try{
String id=tf.getText();
// 执行多表查询
String strSQL = "select stunum,stuname,coname,type,grade,teaname,mark"+" from Mark,teacher where Mark.teanum=teacher.teanum and stunum= '"+id+"' ";
ResultSet rs = st.executeQuery(strSQL);
int count = 0;
while(rs.next()){
id = rs.getString("stunum");stuname = rs.getString("stuname");
coname = rs.getString("coname");type = rs.getString("type");
grade = rs.getString("grade");teaname=rs.getString("teaname");mark=rs.getString("mark");
++count;
}
if(count==0)
JOptionPane.showMessageDialog(null,"对不起,没有您要查找的学生!");
else{
Num.setText(id);sName.setText(stuname);cName.setText(coname);
Type1.setText(type);Grade1.setText(grade);teanameT.setText(teaname);
Chengj.setText(mark);
}
}
catch(SQLException ex){
Num.setText(ex.getMessage()); sName.setText(ex.getMessage());
cName.setText(ex.getMessage());Type1.setText(ex.getMessage());
Grade1.setText(ex.getMessage());teanameT.setText(ex.getMessage());
Chengj.setText(ex.getMessage());
}
}
if(e.getSource()==closeT){
closeDB(); markf.dispose();
}
}
}
//实现添加、修改学生成绩记录类
class markfrm2 implements ActionListener{ //throws IOException
String id,stuname,coname,type,grade,teanum,mark;
Connection conn;
ResultSet rs;
Statement st;
public JLabel NumL,stunameL,conameL,typeL,gradeL,teanumL,markL;
public JTextField tf,Num,sName,cName,Type1,Grade1,Tnum,Chengj;
public JFrame markf;
public JPanel p1;
public JButton alter,sj,insertM,closeT;
public markfrm2(){
//以下是设置界面面板、框架
markf= new JFrame ("添加、修改学生成绩记录");markf.setSize(360,400);markf.setResizable(false);
id = new String(); stuname = new String(); coname = new String();
type = new String();grade = new String();teanum=new String();mark=new String();
NumL = new JLabel("学 号:"); stunameL = new JLabel("姓 名:");
conameL = new JLabel("课 程 名:");typeL = new JLabel("课程类型:");
gradeL = new JLabel("年 级:");teanumL = new JLabel("任课教师号:");markL=new JLabel("成 绩:");
tf=new JTextField();Num = new JTextField();sName = new JTextField();
cName = new JTextField();Type1 = new JTextField();Grade1=new JTextField();
Tnum=new JTextField(); Chengj=new JTextField();p1=new JPanel(); alter=new JButton("提交");
tf.setLocation(190,10);tf.setSize(100,20);p1.add(tf,"Center");
alter.setLocation(50,10);alter.setSize(60,20);sj=new JButton("修改");sj.setLocation(120,10);
sj.setSize(60,20); p1.add(alter,"Center");p1.add(sj,"Center");
insertM=new JButton("添加");insertM.setLocation(86,280);insertM.setSize(80,30);
closeT=new JButton("退出"); closeT.setLocation(156,280);
closeT.setSize(80,30);p1.add(closeT,"Center");p1.add(insertM,"Center");
NumL.setLocation(30,40); NumL.setSize(80,20); p1.add(NumL,"Center");
Num.setLocation(130,40); Num.setSize(180,20);
p1.add(Num,"Center"); stunameL.setLocation(30,70); stunameL.setSize(80,20);
p1.add(stunameL,"Center");sName.setLocation(130,70);sName.setSize(180,20);
p1.add(sName,"Center");conameL.setLocation(30,100);conameL.setSize(80,20);
p1.add(conameL,"Center");cName.setLocation(130,100);cName.setSize(180,20);
p1.add(cName,"Center");typeL.setLocation(30,130);typeL.setSize(80,20);
p1.add(typeL,"Center");Type1.setLocation(130,130);Type1.setSize(180,20);
p1.add(Type1,"Center");gradeL.setLocation(30,160);gradeL.setSize(80,20);
p1.add(gradeL,"Center");Grade1.setLocation(130,160);Grade1.setSize(180,20);
p1.add(Grade1,"Center");teanumL.setLocation(30,190);teanumL.setSize(80,20);
p1.add(teanumL,"Center");Tnum.setLocation(130,190); Tnum.setSize(180,20);
p1.add(Tnum,"Center");markL.setLocation(30,220);markL.setSize(80,20);
p1.add(markL,"Center");Chengj.setLocation(130,220); Chengj.setSize(180,20);
p1.add(Chengj,"Center");
p1.setLayout(new BorderLayout());
Container c=markf.getContentPane(); c.add(p1);
markf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});;
markf.setLocation(200,200); markf.show();
insertM.addActionListener(this); alter.addActionListener(this);
sj.addActionListener(this);closeT.addActionListener(this);
conDB();
}
//以下是连接数据库
public void conDB(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null,"数据库加载失败!");
}
try{
conn = DriverManager.getConnection("jdbc:odbc:studb");
st = conn.createStatement();
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,"数据库连接失败!");
}
}
//以下是关闭数据库
public void closeDB(){
try{
st.close(); conn.close();
}
catch(SQLException e ){
JOptionPane.showMessageDialog(null,"数据库关闭失败!");
}
}
//以下是针对数据库的各种操作
public void actionPerformed(ActionEvent e){
if(e.getSource()==insertM)
if((Num.getText().trim()).equals("") || (sName.getText().trim()).equals("") || (cName.getText().trim()).equals("") || (Type1.getText().trim()).equals("") ||(Grade1.getText().trim()).equals("")||(Tnum.getText().trim()).equals("")||(Chengj.getText().trim()).equals(""))
JOptionPane.showMessageDialog(null,"请输入信息再点击提交添加!");
else{
id = Num.getText();stuname = sName.getText();coname = cName.getText();
type = Type1.getText(); grade= Grade1.getText(); teanum = Tnum.getText();mark = Chengj.getText();
String strSQL = "insert into mark ( stunum,stuname,coname,type,grade,teanum,mark)values( '"+ id + "','" + stuname + "','" + coname + "','" + type + "','" + grade + "','" + teanum + "','" + mark+ "')";
try{
st.executeUpdate(strSQL);
}
catch(Exception exx){
JOptionPane.showMessageDialog(null,"数据库中已经存在您要添加的学号!");
return;
}
JOptionPane.showMessageDialog(null,"恭喜您,添加成功了!");
Num.setText("");sName.setText("");cName.setText("");
Type1.setText("");Grade1.setText("");Tnum.setText("");
Chengj.setText("");
}
//执行修改后提交操作
if(e.getSource()==alter){
if((Num.getText().trim()).equals("") ||(sName.getText().trim()).equals("") ||(cName.getText().trim()).equals("") ||(Type1.getText().trim()).equals("") ||(Grade1.getText().trim()).equals("")||(Tnum.getText().trim()).equals("")|| (Chengj.getText().trim()).equals(""))
JOptionPane.showMessageDialog(null,"请输入信息再点击修改按钮!");
else{
id = Num.getText();stuname = sName.getText();coname = cName.getText();
type = Type1.getText(); grade= Grade1.getText(); teanum = Tnum.getText();mark = Chengj.getText();
String strSQL ="update Mark set stuname='"+stuname+"',coname='"+coname+"',type='"+type+"',grade='"+grade+"',teanum='"+teanum+"',mark='"+mark+"'"+"where stunum='"+id+"'";
try{
st.executeUpdate(strSQL);
}
catch(Exception exx){
JOptionPane.showMessageDialog(null,"数据库中已经存在您要修改的学生记录!");
return;
}
JOptionPane.showMessageDialog(null,"恭喜您,修改成功了!");
}
}
//查找要修改学生信息功能的实现
if(e.getSource()==sj){
try{
String id=tf.getText();
String strSQL = "select stunum,stuname,coname,type,grade,teanum,mark"+" from Mark where stunum= '"+id+"' ";
ResultSet rs = st.executeQuery(strSQL);
int count = 0;
while(rs.next()){
id = rs.getString("stunum");stuname = rs.getString("stuname");
coname = rs.getString("coname");type = rs.getString("type");
grade = rs.getString("grade");teanum=rs.getString("teanum");mark=rs.getString("mark");
++count;
}
if(count==0)
JOptionPane.showMessageDialog(null,"对不起,没有您要查找的学生!");
else{
Num.setText(id);sName.setText(stuname);cName.setText(coname);
Type1.setText(type);Grade1.setText(grade);Tnum.setText(teanum);
Chengj.setText(mark);Num.setEnabled(false);
}
}
catch(SQLException ex){
Num.setText(ex.getMessage()); sName.setText(ex.getMessage());
cName.setText(ex.getMessage());Type1.setText(ex.getMessage());
Grade1.setText(ex.getMessage());Tnum.setText(ex.getMessage());
Chengj.setText(ex.getMessage());
JOptionPane.showMessageDialog(null,"抱歉,程序出现异常!"); }
}
if(e.getSource()==closeT){
closeDB(); markf.dispose();
}
}
}
//实现关于窗体类
class AboutFrm implements ActionListener{
static JFrame aboutfrm = new JFrame("关于");
public AboutFrm(){
JLabel j1=new JLabel(" 学生成绩管理系统!");
JTextArea ta1=new JTextArea("设计者:高秀仕");
JTextArea ta2=new JTextArea(" 辽宁省交通高等专科学校");
JButton mbtn = new JButton("谢谢");
JPanel mp = new JPanel();
JPanel mp2 = new JPanel();
aboutfrm.getContentPane().setLayout(new BorderLayout());
mp.setLayout(new GridLayout(3,1));
mp.add(j1);mp.add(ta1);mp.add(ta2);
mp2.add(mbtn);
Container c = aboutfrm.getContentPane();
c.add(mp, "North");
c.add(mp2, "South");
mbtn.addActionListener(this);
aboutfrm.setSize(220,150);
aboutfrm.setLocation( 610/2, 620/2 );
//aboutfrm.setTitle("记事本");
aboutfrm.setResizable(false);
aboutfrm.show();
}
public void actionPerformed(ActionEvent e){
AboutFrm.aboutfrm.dispose();
}
}
//美观图片窗体类
class markfrm3
{
public markfrm3(){
ImageFrame frame=new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ImageFrame extends JFrame{
public ImageFrame(){
setTitle("图片");setLocation(300,300);
setSize(300,260);ImagePanel p=new ImagePanel();
Container c=getContentPane();
c.add(p);
}
}
class ImagePanel extends JPanel{
private Image i;
public ImagePanel(){
//从当前目录下得到一个本地图像文件
i=Toolkit.getDefaultToolkit().getImage("1.jpg ");
//使用MediaTracker 类对图像的载入情况进行跟踪
MediaTracker tr=new MediaTracker(this);
tr.addImage(i,0);
try{
//每个图像都有一个不同的ID号,可通过waitForID方法等待图像加载完毕
tr.waitForID(0);
}
catch(InterruptedException exception){ }
}
public void paintComponent(Graphics g){
super.paintComponent (g);g.drawImage(i,0,0,null);}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -