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

📄 insert3.java

📁 管理学生信息
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class Insert3 extends JFrame implements ActionListener//插入课程
{
	Choice text2;
	JLabel label2;
	String s;
	JRadioButton select,undo;
	JButton ok,cancel;
	Insert3(String s){
		super("添加/删除选课");
		this.s=s;
		setSize(400,400);
		setLayout(null);
		setVisible(true);
		Connection con=null;
		Statement sm=null;
		String command=null;
		ResultSet rs=null;

		setBackground(Color.green);
		label2=new JLabel("*所选的课程名:");
		text2=new Choice();
		try{
			try{
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				con=DriverManager.getConnection("jdbc:odbc:student");
			}catch(Exception mm){System.out.println(mm.getMessage());}
			sm=con.createStatement();
			command="select cname from ch order by cno";
			rs=sm.executeQuery(command);
			while(rs.next())
				text2.add(rs.getString("cname"));
			rs.close();
			sm.close();
			con.close();
		}catch(SQLException k){System.out.println(k.getMessage());}
		add(label2);add(text2);
		label2.setBounds(50,100,110,30);
		text2.setBounds(180,100,80,30);

		select=new JRadioButton("选课",true);
		undo=new JRadioButton("撤消某课程");
		ButtonGroup bb=new ButtonGroup();
		bb.add(select);
		bb.add(undo);
		add(select);add(undo);
		select.setBounds(50,150,80,20);
		undo.setBounds(150,150,100,20);

		ok=new JButton("确定");
		cancel=new JButton("取消");
		add(cancel);
		add(ok);
		ok.setBounds(40,250,80,20);
		cancel.setBounds(170,250,80,20);
		cancel.addActionListener(this);
		ok.addActionListener(this);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==ok)
		{
			Connection con=null;
			Statement sm=null;
			String command=null;
			try{
				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 str1="'"+s+"'";
				String str2="'"+text2.getSelectedItem()+"'";
				command="select sno,cno from sc where sno="+str1+" and cno in( select cno from ch where cname="+str2+")";
				ResultSet re=sm.executeQuery(command);
				if(select.isSelected())
				{
					if(re.next())
					{
						JOptionPane.showMessageDialog(this,"warring","该记录已存在!",JOptionPane.WARNING_MESSAGE);
					}
					else{
						re.close();
						command="select cno from ch where cname="+str2;
						re=sm.executeQuery(command);
						if(re.next())
						{
							command="insert into sc(sno,cno) values("+str1+",'"+re.getString("cno")+"')";
						}
						re.close();
						sm.executeUpdate(command);
						JOptionPane.showMessageDialog(this,"插入成功","插入成功",JOptionPane.INFORMATION_MESSAGE);
					}
				}
				else
				{
					if(re.next()){
						String s1=re.getString(1);
						String s2=re.getString(2);
						re.close();
						command="delete from sc where sno='"+s1+"' and cno='"+s2+"'";
						sm.executeUpdate(command);
						JOptionPane.showMessageDialog(this,"删除成功","删除成功",JOptionPane.INFORMATION_MESSAGE);
					}
					else
						JOptionPane.showMessageDialog(this,"该记录不存在!","warning",JOptionPane.INFORMATION_MESSAGE);
				}
			}catch(Exception ey){
					JOptionPane.showMessageDialog(this,"有带*的空未填入数据","warring",JOptionPane.WARNING_MESSAGE);
			}
			try{
				sm.close();
				con.close();
				dispose();
			}catch(Exception ez){}
			setVisible(false);
		}
		else
		{
			dispose();
			setVisible(false);
		}
	}
}	

⌨️ 快捷键说明

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