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

📄 manage_course.java

📁 可以用的成绩管理系统
💻 JAVA
字号:
import java.awt.*;
import java.sql.*;
import java.util.*;  
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.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.sql.PreparedStatement;
import javax.swing.*;
public class Manage_course extends JFrame implements ActionListener
{
	private static final int DEFAULT_WIDTH = 500;
    private static final int DEFAULT_HEIFHT = 400; 
    
    ResultSet rs;
	Connection con;
	PreparedStatement ps; 
	
	String course_no,course_name,course_hour,course_credit;
	int hour;
	int credit;
	
	GridBagLayout gbl=new GridBagLayout();
    GridBagConstraints gbc=new GridBagConstraints();
    
    JPanel toppanel;
    JTextField shuru;
    JLabel xuehaowei;
    JButton queding;
  
    JPanel butpanel;
    JLabel kehao,keming,keshi,xuefen;
    JLabel la1,la2,la3,la4,la5,la6,la7,la8,la9,la0;
    JTextField tf_kehao,tf_keming,tf_keshi,tf_xuefen;
	JButton tianjia;
	JButton xiugai;
	JButton shanchu;
	JButton qingkong;
	JButton exit;
	public Manage_course()
	{    
		super.setLocation(300,80);
		super.setSize(DEFAULT_WIDTH,DEFAULT_HEIFHT);
    	super.setTitle("学生基本信息管理!");
    	toppanel=new JPanel();  //////////////////////////顶面板....
    	toppanel.setBackground(Color.WHITE);
    	xuehaowei=new JLabel("课程编号为:");
        shuru=new JTextField(11);
    	queding=new JButton("查询");
    	queding.addActionListener(this);
    	butpanel=new JPanel(); ///////////////////////////底面板.........
    	butpanel.setBackground(Color.WHITE);
    	kehao=new JLabel("课号");
    	keming=new JLabel("课名");
    	keshi=new JLabel("学时");
    	xuefen=new JLabel("学分");
    	la1=new JLabel("    ");
    	la2=new JLabel("    ");
    	la3=new JLabel("    ");
    	la4=new JLabel("    ");
    	la5=new JLabel("    ");
    	la6=new JLabel("    ");
    	la7=new JLabel("    ");
    	la8=new JLabel("    ");
    	la9=new JLabel("    ");	
    	la0=new JLabel("    ");
    	tf_kehao=new JTextField(7);  
    	tf_keming=new JTextField(7);
    	tf_keshi=new JTextField(7);
    	tf_xuefen=new JTextField(7);
    	tianjia=new JButton("添加");
    	tianjia.addActionListener(this);
    	xiugai=new JButton("修改");
    	xiugai.addActionListener(this);
    	shanchu=new JButton("删除");
    	shanchu.addActionListener(this);
    	qingkong=new JButton("清空");
    	qingkong.addActionListener(this); 
    	exit=new JButton("返回");
    	exit.addActionListener(this); 
    	set(0,0,1,1,butpanel,kehao);set(1,0,1,1,butpanel,tf_kehao);set(2,0,1,1,butpanel,la1);set(3,0,1,1,butpanel,keming);set(4,0,1,1,butpanel,tf_keming);
    	set(0,1,1,1,butpanel,keshi);set(1,1,1,1,butpanel,tf_keshi);set(2,1,1,1,butpanel,la2);set(3,1,1,1,butpanel,xuefen);set(4,1,1,1,butpanel,tf_xuefen);
    	set(0,2,1,1,butpanel,la5);set(1,2,1,1,butpanel,la6);set(2,2,1,1,butpanel,la3);set(3,2,1,1,butpanel,la4);set(4,2,1,1,butpanel,la7);
    	set(0,3,1,1,butpanel,tianjia);set(1,3,1,1,butpanel,xiugai);set(2,3,1,1,butpanel,la0);set(3,3,1,1,butpanel,shanchu);set(4,3,1,1,butpanel,qingkong);
    	
    	set(0,0,1,1,toppanel,xuehaowei);
    	set(1,0,1,1,toppanel,shuru);
    	set(2,0,1,1,toppanel,queding);
    	set(3,0,1,1,toppanel,exit);
    	
    	    
    	toppanel.add(butpanel);
    	add(toppanel,BorderLayout.NORTH);
    	butpanel.setLayout(gbl);
 	 	setLayout(new GridLayout()); 
 	 	
 	 	String url="jdbc:odbc:StudentScore";     
	    String username="sa";     
	    String password="";  
	    try 
	    {         
	    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");     
	        con=DriverManager.getConnection(url,username,password);     
	    }     
	    catch(ClassNotFoundException cnfex)  
	    {  
	    	System.err.println( "装载JDBC/ODBC驱动程序失败。");     
	        //cnfex.printStackTrace();     
	        System.exit(1);    
	    }     
	    catch(SQLException sqlex)  
	    {
	    	System.err.println("无法连接数据库");     
	        sqlex.printStackTrace();     
	        System.exit(1);
	    }   
	}
	public void set(int x,int y,int w,int h,JPanel panel,Component com)
    {
   	 gbc.anchor=GridBagConstraints.NORTH;
   	 gbc.gridx=x;
   	 gbc.gridy=y;
   	 gbc.gridwidth=w;
   	 gbc.gridheight=h;
   	 gbl.setConstraints(com, gbc);
   	 panel.add(com);
    }
	public void actionPerformed(ActionEvent e)
	{
		Object obj=e.getSource();
		if(obj==queding)
		{
			try
			{
				String no=shuru.getText().trim();
				if(no.length()==0)
				{
					 JOptionPane.showMessageDialog((Component)null,"请输入教师编号!","提示信息",JOptionPane.INFORMATION_MESSAGE);
				}
				else
				{
					ps=con.prepareStatement("select * from course where course_no=?");
					ps.setString(1,no);
					rs=ps.executeQuery(); 
					rs.next();
					course_no=rs.getString(1).trim();
					course_name=rs.getString(2).trim();
					course_hour=""+rs.getInt(3);
					course_credit=""+rs.getInt(4);
					
					tf_kehao.setText(course_no);
					tf_keming.setText(course_name);
					tf_keshi.setText(course_hour);
					tf_xuefen.setText(course_credit);
				}
			}
			catch(SQLException sql)  
			 {   
				sql.getMessage();
				 //sql.printStackTrace();  
				 JOptionPane.showMessageDialog((Component)null,"该课程不存在!","提示信息",JOptionPane.QUESTION_MESSAGE);
				 shuru.requestFocus();
		     }
		}
		if(obj==tianjia)
		{
			course_no=tf_kehao.getText().trim();
			course_name=tf_keming.getText().trim();
			hour=Integer.parseInt(tf_keshi.getText().trim());
			credit=Integer.parseInt(tf_xuefen.getText().trim());
			if(course_no.length()==0||course_name.length()==0||course_hour.length()==0||course_credit.length()==0)
			{
				JOptionPane.showMessageDialog((Component)null,"请添加完整信息!","提示信息",JOptionPane.QUESTION_MESSAGE);
			}
			else
			{
				try
				{
					ps=con.prepareStatement("insert into course values(?,?,?,?)");
					ps.setString(1, course_no);
					ps.setString(2, course_name);
					ps.setInt(3, hour);
					ps.setInt(4,credit);
					JOptionPane.showMessageDialog((Component)null,"确定添加吗?","提示信息",JOptionPane.QUESTION_MESSAGE);
					tf_kehao.setText("");
					tf_keming.setText("");
					tf_keshi.setText("");
					tf_xuefen.setText("");
					rs=ps.executeQuery();
				}
				catch(SQLException sql)
				{
					sql.getMessage();
					//sql.printStackTrace();
					JOptionPane.showMessageDialog((Component)null,"添加成功!","提示信息",JOptionPane.QUESTION_MESSAGE);
				}
			}
		}   
		if(obj==xiugai)
		{
			String no=shuru.getText().trim();
			course_no=tf_kehao.getText().trim();
			course_name=tf_keming.getText().trim();
			hour=Integer.parseInt(tf_keshi.getText().trim());
			credit=Integer.parseInt(tf_xuefen.getText().trim());
			try
			{
				ps=con.prepareStatement("update course set course_no=?,course_name=?,course_hour=?,course_credit=? where course_no=?");
				ps.setString(1, course_no);
				ps.setString(2, course_name);
				ps.setInt(3,hour);
				ps.setInt(4,credit);
				ps.setString(5,no);
				JOptionPane.showMessageDialog((Component)null,"确定修改吗?","提示信息",JOptionPane.QUESTION_MESSAGE);
				tf_kehao.setText("");
				tf_keming.setText("");
				tf_keshi.setText("");
				tf_xuefen.setText("");
				JOptionPane.showMessageDialog((Component)null,"修改成功!","提示信息",JOptionPane.INFORMATION_MESSAGE);
				rs=ps.executeQuery();
				rs.next();
				
			}
			catch(SQLException sql)
			{
				sql.getMessage();
				//sql.printStackTrace();
				//JOptionPane.showMessageDialog((Component)null,sql.getMessage(),"提示信息",JOptionPane.QUESTION_MESSAGE);
			}
		}
		if(obj==shanchu)
		{
			try
			{
				String no=shuru.getText().trim();
				ps=con.prepareStatement("delete from course where course_no=?");
				ps.setString(1,no);
				JOptionPane.showMessageDialog((Component)null,"确定删除吗?","提示信息",JOptionPane.QUESTION_MESSAGE);
				tf_kehao.setText("");
				tf_keming.setText("");
				tf_keshi.setText("");
				tf_xuefen.setText("");
				rs=ps.executeQuery();
				JOptionPane.showMessageDialog((Component)null,"删除成功!","提示信息",JOptionPane.INFORMATION_MESSAGE);
			}
			catch(SQLException sqlex)  
			 {   
				 sqlex.getMessage();        
		     }
		}
		if(obj==qingkong)
		{
			shuru.setText("");
			tf_kehao.setText("");
			tf_keming.setText("");
			tf_keshi.setText("");
			tf_xuefen.setText("");
		}
		if(obj==exit)
		{
			this.dispose();
			Admin_manage admin=new Admin_manage();
			admin.setVisible(true);
		}
	}
	public static void main(String[] args) 
	{
		Manage_course course=new Manage_course();
		course.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		course.setVisible(true);
	}

}

⌨️ 快捷键说明

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