⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xuanke.java

📁 该项目的开发是在学生课程日益增多的压力下设计且用来解决选课的烦恼
💻 JAVA
字号:
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
public class xuanke extends  JFrame implements ActionListener
{
  Connection con;
  ResultSet rs;
  Statement st;
  Connection con1;
  ResultSet rs1;
  Statement st1;
  TextField text1,text3,text4,text5;
  Choice kc;
  JButton button1,button2,button3;
  String kechengming1=new String();
  String kechenghao1=new String();
  String kechengxueshi1=new String();
  String kechengxuefen1=new String();

 
  public void xuanke()
  {
   text1=new TextField(10);
   text1.setEditable(false);
   kc=new Choice();
   text3=new TextField(10);
   text3.setEditable(false);
   text4=new TextField(10);
   text4.setEditable(false);
   text5=new TextField(10);
   text5.setEditable(false);
   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));
   Box box3=Box.createHorizontalBox();
   box3.add(new JLabel("学号:",JLabel.CENTER));
   box3.add(text1);
   Box box4=Box.createHorizontalBox();
   box4.add(new JLabel("课号:",JLabel.CENTER));
   box4.add(kc);
   Box box5=Box.createHorizontalBox();
   box5.add(new JLabel("课名:",JLabel.CENTER));
   box5.add(text3);
   Box box6=Box.createHorizontalBox();
   box6.add(new JLabel("学时:",JLabel.CENTER));
   box6.add(text4);
   Box box7=Box.createHorizontalBox();
   box7.add(new JLabel("学分:",JLabel.CENTER));
   box7.add(text5);
   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(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(200,50,350,250);
   validate();
   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 kecheng");
          }catch(SQLException el){}//连接数据库
      try{//将后台数据库中的课程号显示在kc这个按钮上
          while(rs.next())
           {String kechenghao=new String();
            kechenghao=rs.getString("crid");
            kc.add(kechenghao);
           }
          }catch(Exception exx){}
      try{
          File EmpClaData=new File("f:\\kecheng\\Empclassdata.dat");
          if(!(EmpClaData.exists()&&EmpClaData.isFile()))
          text1.setText("");
          else{
              DataInputStream din=new DataInputStream(new FileInputStream(EmpClaData));
              int ID2=din.readInt();
              text1.setText(Integer.toString(ID2));
              din.close();
             }
         }catch(Exception eo){}
      
  }

  public void actionPerformed(ActionEvent e)
  {

   if(e.getSource()==button2)
   {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 kecheng");
          }catch(SQLException el){}//连接数据库
    String kechenghao=new String();
    kechenghao=kc.getSelectedItem();
    if(kechenghao.length()>0 )
    {
     try{
         while(rs.next())
         {
          kechenghao1=rs.getString("crid");
          if(kechenghao.equals(kechenghao1)) 
           {
            kechengming1=rs.getString("crname");
            kechengxueshi1=rs.getString("crper");
            kechengxuefen1=rs.getString("crcre");
            text3.setText(kechengming1);
            text4.setText(kechengxueshi1);
            text5.setText(kechengxuefen1);
           }
          }
         }catch(Exception ee){}
       }

      
      

   }
   
   if(e.getSource()==button1) 
   {

       String xuehao=new String();
       xuehao=text1.getText();
       String kechengming2=new String();
       kechengming2=text3.getText();
       String kechenghao2=new String();
       kechenghao2=kc.getSelectedItem();
       int count=0;
       int count1=0;

       
       if(kechengming2.length()==0){JOptionPane.showMessageDialog(null,"请选择课程名");}
       else   
           {try{//连接数据库
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               }
               catch(ClassNotFoundException e4)
                {
                 System.out.println(""+e4);
                }
            try{
              con=DriverManager.getConnection("jdbc:odbc:lhm","","");
              st=con.createStatement();
              rs=st.executeQuery("Select * FROM xinxi");
              }catch(SQLException el){}//连接数据库
           try{ 
                 while(rs.next())
                  {String xuehao3=new String();
                   xuehao3=rs.getString("studentid");
                   if(xuehao.equals(xuehao3)){count++;}
                  }
               }catch(Exception exxx){}
           try{//连接数据库
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               }
               catch(ClassNotFoundException e5)
                {
                 System.out.println(""+e5);
                }
           try{
              con1=DriverManager.getConnection("jdbc:odbc:lhm","","");
              st1=con.createStatement();
              rs1=st.executeQuery("Select * FROM xuanke");
              }catch(SQLException ell){}//连接数据库
                try{ 
                 while(rs1.next())
                  {String kechengming3=new String();
                   kechengming3=rs1.getString("crname");
                   String xuehao3=new String();
                   xuehao3=rs1.getString("studentid");
                   if((kechengming2.equals(kechengming3))&&(xuehao.equals(xuehao3))){count1++;}
                  }
               }catch(Exception exxxx){}
           if((count>0)&&(count1==0))
           {
            String strSQL6 ="insert into xuanke(studentid,crid,crname) values ('"+xuehao + "','" + kechenghao2 + "','" + kechengming2+"')";
            try
            {
             st.executeUpdate(strSQL6);
            }catch(Exception exx){}
            JOptionPane.showMessageDialog(null,"选课成功!");
            
            text3.setText("");
            text4.setText("");
            text5.setText("");
           }
           else if(count1>0) {JOptionPane.showMessageDialog(null,"您已选修了这门课!");}
             else {JOptionPane.showMessageDialog(null,"数据库中无该学号,无法选修课程!");}
          }
     }
   if(e.getSource()==button3){setVisible(false);;} 
  }


         
}  
























⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -