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

📄 del.java

📁 管理学生信息
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class Del extends JFrame implements ActionListener{
	Choice text1;
	JButton ok,cancel;
	JRadioButton o1,o2,o3;
	Del(){
		super("删除窗口");
		JLabel a=new JLabel("请输入删除的条件:");
		text1=new Choice();
		text1.add("请先选择下面的选项");
		setLayout(new BorderLayout());
		setVisible(true);

		JPanel p1=new JPanel();
		p1.add(a);p1.add(text1);
		add("North",p1);

		JPanel p2=new JPanel();
		o1=new JRadioButton("学号");
		o2=new JRadioButton("课程号");
		o3=new JRadioButton("教师名");
		ButtonGroup o=new ButtonGroup();
		o.add(o1);o.add(o2);o.add(o3);
		p2.add(o1);p2.add(o2);p2.add(o3);
		add("Center",p2);

		JPanel p3=new JPanel();
		ok=new JButton("确定");
		cancel=new JButton("取消");
		p3.add(ok);p3.add(cancel);
		add("South",p3);

		o1.addActionListener(this);
		o2.addActionListener(this);
		o3.addActionListener(this);
		ok.addActionListener(this);
		cancel.addActionListener(this);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		pack();
		validate();
	}
	public void actionPerformed(ActionEvent e)
	{
		String str1,str2;
		int flag;
		if(o1.isSelected())
		{
			str1="sno";
			str2="st";
			flag=2;
		}
		else if(o3.isSelected())
		{
			str1="teacher";
			str2="ch";
			flag=1;
		}
		else
		{
			str1="cno";
			str2="ch";
			flag=3;
		}
		if(e.getSource()==ok)
		{
			Connection con=null;
			Statement sm=null;
			String command=null;
			try{
				//DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				con=DriverManager.getConnection("jdbc:odbc:student");
			}catch(Exception ex){
			System.out.println(ex.getMessage());
			return;
			}
			try{
				sm=con.createStatement();
				String str3=text1.getSelectedItem();
				command="select * from "+str2+" where "+str1+"='"+str3+"'";
				ResultSet re=sm.executeQuery(command);
				if (re.next())
				{
					int k=JOptionPane.showConfirmDialog(this,"你确认要删除吗?");
					if(k==0){
							command="delete from "+str2+" where "+str1+"='"+str3+"'";
							sm.executeUpdate(command);
							command="delete from yonghu where arth="+flag+" and username='"+str3+"'";
							sm.executeUpdate(command);
					JOptionPane.showMessageDialog(this,"删除成功","删除成功",JOptionPane.INFORMATION_MESSAGE);
					}
				} 
				else
				{
					JOptionPane.showMessageDialog(this,"warring","无符合要求的记录!",JOptionPane.WARNING_MESSAGE);
				}
			}catch(Exception ey){
					JOptionPane.showMessageDialog(this,"请填入删除条件","warring",JOptionPane.WARNING_MESSAGE);
			}
			try{
				sm.close();
				con.close();
				dispose();
			}catch(Exception ez){}
			setVisible(false);
			}
		if(e.getSource()==cancel)
		{
			dispose();
			setVisible(false);
		}
		if(e.getSource()==o1||e.getSource()==o2||e.getSource()==o3)
		{
			text1.removeAll();
			Connection con=null;
			Statement sm=null;
			String command=null;
			try{
				//DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				con=DriverManager.getConnection("jdbc:odbc:student");
			}catch(Exception ex){
				System.out.println(ex.getMessage());
			return;
			}
			try{
				sm=con.createStatement();
				command="select "+str1+" from "+str2;
				ResultSet rs=sm.executeQuery(command);
				while(rs.next()){
					text1.add(rs.getString(1).trim());
				}
				sm.close();
				con.close();
			}catch(SQLException ee){
				System.out.println(ee.getMessage());
			}
		}	
	}
}

⌨️ 快捷键说明

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