SQL Server数据库设计

Course表:
(1)设计
在这里插入图片描述
(2)数据
在这里插入图片描述
属性依次为:课程号、课程名、先修课课程号、学分
主键为课程号

Student表:
(1)设计
在这里插入图片描述
(2)数据
在这里插入图片描述
属性依次为:学号、姓名、性别、年龄、所在系系名
主键为学号

SC表:表示学生选课关系
(1)设计
在这里插入图片描述
(2)数据
在这里插入图片描述
属性依次为学号、课程号、分数
主键为学号、课程号

触发器:
1)在Student表上定义一个DELETE触发器,当删除一个学生记录时,自动删除SC表上这个学生的所有选课记录 。
2)在Course表上定义一个UPDA TE触发器,当更新一门课程的课程号时,自动修改SC表上这门课程的课程号 。
3) 在SC表上定义一个DELETE触发器,当删除一条选课记录时,若这条记录是这门课程的唯一被选记录,则同时删除Course表中的这门课程 。

数据库与java结合形成图形化界面。
以下为java程序:

完成数据库中数据的增删改查(Add、Del、Update、Sel)
我的数据库中只有三个表,所以完成这些功能需要12个类:

AddC

/*
AddC
*/
package sc;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;

public class AddC extends JPanel implements ActionListener{
	JTextField 课程号,课程名,先修课课程号,学分;
	JButton 录入;
	
public AddC(){
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch(Exception e){System.err.println("不能设置外观:   "+e);}
	
	课程号=new JTextField(12);
	课程名=new JTextField(12);
	先修课课程号=new JTextField(12);
	学分=new JTextField(12);
	录入=new JButton("录入");
	录入.addActionListener(this);
	
	Box box1=Box.createHorizontalBox();//横放box
	Box box2=Box.createHorizontalBox();
	Box box3=Box.createHorizontalBox();
	Box box4=Box.createHorizontalBox();
	Box box5=Box.createHorizontalBox();
	Box box6=Box.createHorizontalBox();
	box1.add(new JLabel("课程号:"));
	box1.add(课程号);
	box2.add(new JLabel("课程名:"));
	box2.add(课程名);
	box3.add(new JLabel("先修课课程号:"));
	box3.add(先修课课程号);
	box4.add(new JLabel("学分:"));
	box4.add(学分);
	box6.add(录入);
	Box boxH=Box.createVerticalBox();//竖放box
	boxH.add(box1);
	boxH.add(box2);
	boxH.add(box3);
	boxH.add(box4);
	boxH.add(box5);
	boxH.add(box6);
	boxH.add(Box.createVerticalGlue());
	JPanel messPanel=new JPanel();
	messPanel.add(boxH);
	setLayout(new BorderLayout());
	add(messPanel,BorderLayout.CENTER);
	validate();
}
public void actionPerformed(ActionEvent c){
	Object obj=c.getSource();
	if(obj==录入){
		if(课程号.getText().equals("")||课程名.getText().equals("")||先修课课程号.getText().equals("")||学分.getText().equals("")){
			JOptionPane.showMessageDialog(this,"学生信息请填满再录入!" );
		}
		Statement stmt=null;
		ResultSet rs=null,rs1=null;
		String sql,sql1;
		    sql1="select * from Course where Cno='"+课程号.getText()+"'";
		    sql="insert into Course values('"+课程号.getText()+"','"+课程名.getText()+"','"+先修课课程号.getText()+"','"+学分.getText()+"')";
	   try{
		   Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs1=stmt.executeQuery(sql1);
			if(rs1.next()){JOptionPane.showMessageDialog(this,"该课号以存在,无法添加");}
			else{
			stmt.executeUpdate(sql);	
			JOptionPane.showMessageDialog(this,"添加成功");
			}		
			rs1.close();
			
			stmt.close();
	   }
	   catch(SQLException e){
		   System.out.print("SQL Exception occur.Message is:"+e.getMessage());
		   }
	}
}
public static Connection CONN(){
	   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
	   String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
	   Connection dbConn=null;

	   try {
	   Class.forName(driverName);
	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
	   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
	   } catch (Exception e) {
	   e.printStackTrace();
	   }
	   return dbConn;
}

}

AddS

/*
AddS
*/
package sc;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;


public  class AddS extends JPanel implements ActionListener{
	JTextField 学号,姓名,性别,年龄,所在系系名;
	JButton 录入;
	
public AddS(){
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch(Exception e){System.err.println("不能设置外观:   "+e);}
	
	学号=new JTextField(12);
	姓名=new JTextField(12);
	性别=new JTextField(12);
	年龄=new JTextField(12);
	所在系系名=new JTextField(12);
	录入=new JButton("录入");
	录入.addActionListener(this);
	
	Box box1=Box.createHorizontalBox();//横放box
	Box box2=Box.createHorizontalBox();
	Box box3=Box.createHorizontalBox();
	Box box4=Box.createHorizontalBox();
	Box box5=Box.createHorizontalBox();
	Box box6=Box.createHorizontalBox();
	box1.add(new JLabel("学号:"/*,JLabel.CENTER*/));
	box1.add(学号);
	box2.add(new JLabel("姓名:"/*,JLabel.CENTER*/));
	box2.add(姓名);
	box3.add(new JLabel("性别:"/*,JLabel.CENTER*/));
	box3.add(性别);
	box4.add(new JLabel("年龄:"/*,JLabel.CENTER*/));
	box4.add(年龄);
	box5.add(new JLabel("所在系系名:"/*,JLabel.CENTER*/));
	box5.add(所在系系名);
	box6.add(录入);
	Box boxH=Box.createVerticalBox();//竖放box
	boxH.add(box1);
	boxH.add(box2);
	boxH.add(box3);
	boxH.add(box4);
	boxH.add(box5);
	boxH.add(box6);
	boxH.add(Box.createVerticalGlue());
	JPanel messPanel=new JPanel();
	messPanel.add(boxH);
	setLayout(new BorderLayout());
	add(messPanel,BorderLayout.CENTER);
	validate();
	}
public void actionPerformed(ActionEvent c){
	Object obj=c.getSource();
	if(obj==录入){
		if(学号.getText().equals("")||姓名.getText().equals("")||性别.getText().equals("")||年龄.getText().equals("")||所在系系名.getText().equals("")){
			JOptionPane.showMessageDialog(this,"学生信息请填满再录入!" );
		}
		Statement stmt=null;
		ResultSet rs1=null;
		String sql,sql1;
		    sql1="select * from Student where Sno='"+学号.getText()+"'";
		    sql="insert into Student values('"+学号.getText()+"','"+姓名.getText()+"','"+性别.getText()+"','"+年龄.getText()+"','"+所在系系名.getText()+"')";
	   try{
		   Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs1=stmt.executeQuery(sql1);
			if(rs1.next()){JOptionPane.showMessageDialog(this,"该学号已经存在,无法添加");}
			else{
			stmt.executeUpdate(sql);	
			JOptionPane.showMessageDialog(this,"添加成功");
			}		
			rs1.close();
			stmt.close();
	   }
	   catch(SQLException e){
		   System.out.print("SQL Exception occur.Message is:"+e.getMessage());
		   }
	}
}
public static Connection CONN(){
	   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
	    String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
	   Connection dbConn=null;

	   try {
	   Class.forName(driverName);
	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
	   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
	   } catch (Exception e) {
	   e.printStackTrace();
	   }
	   return dbConn;
}

}



AddSC

/*
AddSC
*/
package sc;

import java.awt.*;

import javax.swing.*;

import java.sql.*;
import java.util.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.awt.event.*;

public class AddSC extends JPanel implements ActionListener{
	JTextField 课程号,学号,成绩;
	JButton 录入;
	
public AddSC(){
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch(Exception e){System.err.println("不能设置外观:   "+e);}
	
	课程号=new JTextField(12);
	学号=new JTextField(12);
	成绩=new JTextField(12);
	录入=new JButton("录入");
	录入.addActionListener(this);
	
	Box box1=Box.createHorizontalBox();//横放box
	Box box2=Box.createHorizontalBox();
	Box box3=Box.createHorizontalBox();
	Box box4=Box.createHorizontalBox();
	box1.add(new JLabel("课号:"));
	box1.add(课程号);
	box2.add(new JLabel("学号:"));
	box2.add(学号);
	box3.add(new JLabel("成绩:"));
	box3.add(成绩);
	box4.add(录入);
	Box boxH=Box.createVerticalBox();//竖放box
	boxH.add(box1);
	boxH.add(box2);
	boxH.add(box3);
	boxH.add(box4);
	boxH.add(Box.createVerticalGlue());
	JPanel messPanel=new JPanel();
	messPanel.add(boxH);
	setLayout(new BorderLayout());
	add(messPanel,BorderLayout.CENTER);
	validate();
}
public void actionPerformed(ActionEvent c){
	Object obj=c.getSource();
	if(obj==录入){
		if(课程号.getText().equals("")||学号.getText().equals("")){
			JOptionPane.showMessageDialog(this,"填写课号与学号才能录入!" );
		}
		else
		{
		Statement stmt=null;
		ResultSet rs=null,rs1=null,rsC=null,rsS=null;
		String sql,sql1,sqlS,sqlC;
		    sqlC="select * from Course where Cno='"+课程号.getText()+"'";
		    sqlS="select * from Student where Sno='"+学号.getText()+"'";
		    sql1="select * from SC where Cno='"+课程号.getText()+"' and Sno='"+学号.getText()+"'";
		    sql="insert into SC values('"+学号.getText()+"','"+课程号.getText()+"','"+成绩.getText()+"')";
	   try{
		   Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rsC=stmt.executeQuery(sqlC);
			if(rsC.next()){
				rsS=stmt.executeQuery(sqlS);
				if(rsS.next()){
			rs1=stmt.executeQuery(sql1);
			if(rs1.next()){JOptionPane.showMessageDialog(this,"该学生以选该课程号,无法添加");}
			else{
			stmt.executeUpdate(sql);	
			JOptionPane.showMessageDialog(this,"添加成功");
			}
			rs1.close();
			}
				else{JOptionPane.showMessageDialog(this,"该学生不存在,无法添加");}
				rsS.close();
			}
			else{JOptionPane.showMessageDialog(this,"该课程不存在,无法添加");}
			rsC.close();
			stmt.close();
	   }
	   catch(SQLException e){
		   System.out.print("SQL Exception occur.Message is:"+e.getMessage());
		   }
	   }
	}
}
	public static Connection CONN(){
		   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
		    String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
		   Connection dbConn=null;

		   try {
		   Class.forName(driverName);
		   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
		   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
		   } catch (Exception e) {
		   e.printStackTrace();
		   }
		   return dbConn;
	}

}

DelC

/*
DelC
*/
package sc;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class DelC extends JPanel implements ActionListener{
	String save=null;
	JTextField 课号1,课程号,课程名,先修课课程号,学分;
	JButton 删除,查找;
	
public DelC(){
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch(Exception e){System.err.println("不能设置外观:   "+e);}
	
	课号1=new JTextField(12);
	课程号=new JTextField(12);
	课程名=new JTextField(12);
	先修课课程号=new JTextField(12);
	学分=new JTextField(12);
	删除=new JButton("删除");
	查找=new JButton("查找");
	
	Box box1=Box.createHorizontalBox();//横放box
	Box box2=Box.createHorizontalBox();
	Box box3=Box.createHorizontalBox();
	Box box4=Box.createHorizontalBox();
	Box box5=Box.createHorizontalBox();
	Box box6=Box.createHorizontalBox();
	Box box7=Box.createHorizontalBox();
	box1.add(new JLabel("课程号:",JLabel.CENTER));
	box1.add(课程号);
	box2.add(new JLabel("课程名:",JLabel.CENTER));
	box2.add(课程名);
	box3.add(new JLabel("先修课课程号:",JLabel.CENTER));
	box3.add(先修课课程号);
	box4.add(new JLabel("学分:",JLabel.CENTER));
	box4.add(学分);
	box6.add(删除);
	box7.add(new JLabel("课号:",JLabel.CENTER));
	box7.add(课号1);
	box7.add(查找);
	Box boxH=Box.createVerticalBox();//竖放box
	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());
	
	删除.addActionListener(this);
    查找.addActionListener(this);
	
	JPanel picPanel=new JPanel();
	JPanel messPanel=new JPanel();
	messPanel.add(box7);
	picPanel.add(boxH);
	setLayout(new BorderLayout());
	JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割
	add(splitV,BorderLayout.CENTER);
	validate();

}
public void actionPerformed(ActionEvent e){
	Object obj=e.getSource();
	Statement stmt=null;
	ResultSet rs=null,rs1=null;
    String sql=null,sql1=null,sqlSC=null;
	if(obj==查找){if(课号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写查询的课号!" );
	else{
	    sql1="select * from Course where Cno='"+课号1.getText()+"'";
	    try{
	    Connection dbConn1=CONN();
		stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		rs1=stmt.executeQuery(sql1);
	    if(rs1.next()){课程号.setText(rs1.getString("Cno").trim());
	                   课程名.setText(rs1.getString("Cname").trim());
	                   先修课课程号.setText(rs1.getString("Cpno").trim());
	                   学分.setText(rs1.getString("Ccredit").trim());
	                   save=课号1.getText().trim();	    	
	    }
	    else{JOptionPane.showMessageDialog(this,"没有这个课号的课程" );}
	    stmt.close();
	    rs1.close();
	    }catch(SQLException e1){
			   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
		   }
	    }
	}
	else{
		if(obj==删除){if(save==null)JOptionPane.showMessageDialog(this,"还没查找需要修改的课程" );
		else{sql="delete from Course where Cno='"+save+"'";
		sqlSC="delete from SC where Cno='"+save+"'";
		try{
		    Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			stmt.executeUpdate(sql);
			stmt.executeUpdate(sqlSC);
			save=null;
			JOptionPane.showMessageDialog(this,"删除完成" );
			课程号.setText("");
            课程名.setText("");
            先修课课程号.setText("");
            学分.setText("");
			stmt.close();
		    }catch(SQLException e1){
				   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
			   }
		}
		}
}
}

//	连接数据库方法
public static Connection CONN(){
	   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
	    String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
	   Connection dbConn=null;

	   try {
	   Class.forName(driverName);
	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
	   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
	   } catch (Exception e) {
	   e.printStackTrace();
	   }
	   return dbConn;
}

}

DelS

/*
DelS
*/
package sc;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;


public class DelS extends JPanel implements ActionListener{
String save=null;
JTextField 学号1,学号,姓名,性别,年龄,所在系系名;
JButton 删除,查找;
	
public DelS(){
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch(Exception e){System.err.println("不能设置外观:   "+e);}
		
	学号1=new JTextField(12);
	学号=new JTextField(12);
	姓名=new JTextField(12);
	性别=new JTextField(12);
	年龄=new JTextField(12);
	所在系系名=new JTextField(12);
	删除=new JButton("删除");
	查找=new JButton("查找");
	
	Box box1=Box.createHorizontalBox();//横放box
	Box box2=Box.createHorizontalBox();
	Box box3=Box.createHorizontalBox();
	Box box4=Box.createHorizontalBox();
	Box box5=Box.createHorizontalBox();
	Box box6=Box.createHorizontalBox();
	Box box7=Box.createHorizontalBox();
	
	box1.add(new JLabel("学号:",JLabel.CENTER));
	box1.add(学号);
	box2.add(new JLabel("姓名:",JLabel.CENTER));
	box2.add(姓名);
	box3.add(new JLabel("性别:",JLabel.CENTER));
	box3.add(性别);
	box4.add(new JLabel("年龄:",JLabel.CENTER));
	box4.add(年龄);
	box5.add(new JLabel("所在系系名:",JLabel.CENTER));
	box5.add(所在系系名);
	box6.add(删除);
	box7.add(new JLabel("学号:",JLabel.CENTER));
	box7.add(学号1);
	box7.add(查找);
	Box boxH=Box.createVerticalBox();//竖放box
	boxH.add(box1);
	boxH.add(box2);
	boxH.add(box3);
	boxH.add(box4);
	boxH.add(box5);
	boxH.add(box6);
	
	boxH.add(Box.createVerticalGlue());
	
	删除.addActionListener(this);
	    查找.addActionListener(this);
	
	JPanel picPanel=new JPanel();
	JPanel messPanel=new JPanel();
	messPanel.add(box7);
	picPanel.add(boxH);
	setLayout(new BorderLayout());
	JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割
	add(splitV,BorderLayout.CENTER);
	validate();
}
public void actionPerformed(ActionEvent e){
	Object obj=e.getSource();
	Statement stmt=null;
	ResultSet rs=null,rs1=null;
	    String sql=null,sql1=null,sqlSC=null;
	
	if(obj==查找){if(学号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写查询的学号!" );
	else{
	    sql1="select * from Student where Sno='"+学号1.getText()+"'";
	    try{
	    Connection dbConn1=CONN();
	    stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
	    rs1=stmt.executeQuery(sql1);
	    if(rs1.next()){学号.setText(rs1.getString("Sno").trim());
	                   姓名.setText(rs1.getString("Sname").trim());
	                   性别.setText(rs1.getString("Ssex").trim());
	                		  年龄.setText(rs1.getString("Sage").trim());
	                				   所在系系名.setText(rs1.getString("Sdept").trim());
	                   save=学号1.getText().trim();    
	    }
	    else{JOptionPane.showMessageDialog(this,"没有这个学号的学生" );}
	    stmt.close();
	    rs1.close();
	    }catch(SQLException e1){
	   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
	   }
	    }
	}
	else{
	if(obj==删除){if(save==null)JOptionPane.showMessageDialog(this,"还没查找需要修改的学生" );
	else{sql="delete from Student where Sno='"+save+"'";
	     sqlSC="delete from SC where Sno='"+save+"'";
	try{
	    Connection dbConn1=CONN();
	stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
	stmt.executeUpdate(sql);
	stmt.executeUpdate(sqlSC);
	save=null;
	JOptionPane.showMessageDialog(this,"删除完成" );
	学号.setText("");
	            姓名.setText("");
	            性别.setText("");
	            年龄.setText("");
	            所在系系名.setText("");
	
	stmt.close();
	    }catch(SQLException e1){
	   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
	   }
	}
	}
	}
}
public static Connection CONN(){
	   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
	    String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
	   Connection dbConn=null;

	   try {
	   Class.forName(driverName);
	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
	   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
	   } catch (Exception e) {
	   e.printStackTrace();
	   }
	   return dbConn;
}

}

DelSC

/*
DelSC
*/
package sc;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class DelSC extends JPanel implements ActionListener{
	String saveC=null;
	String saveS=null;
	JTextField 课号1,学号1,学号,课程号,成绩;
	JButton 删除,查找;
	
public DelSC(){
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch(Exception e){System.err.println("不能设置外观:   "+e);}
	
	学号1=new JTextField(12);
	课号1=new JTextField(12);
	课程号=new JTextField(12);
	学号=new JTextField(12);
	成绩=new JTextField(12);
	删除=new JButton("删除");
	查找=new JButton("查找");
	
	Box box1=Box.createHorizontalBox();//横放box
	Box box2=Box.createHorizontalBox();
	Box box3=Box.createHorizontalBox();
	Box box4=Box.createHorizontalBox();
	Box box5=Box.createHorizontalBox();
	box1.add(new JLabel("课程号:",JLabel.CENTER));
	box1.add(课程号);
	box2.add(new JLabel("学号:",JLabel.CENTER));
	box2.add(学号);
	box3.add(new JLabel("成绩:",JLabel.CENTER));
	box3.add(成绩);
	box4.add(删除);
	box5.add(new JLabel("课号:",JLabel.CENTER));
	box5.add(课号1);
	box5.add(new JLabel("学号:",JLabel.CENTER));
	box5.add(学号1);
	box5.add(查找);
	Box boxH=Box.createVerticalBox();//竖放box
	boxH.add(box1);
	boxH.add(box2);
	boxH.add(box3);
	boxH.add(box4);
	boxH.add(Box.createVerticalGlue());
	
	删除.addActionListener(this);
    查找.addActionListener(this);
	
	JPanel picPanel=new JPanel();
	JPanel messPanel=new JPanel();
	messPanel.add(box5);
	picPanel.add(boxH);
	setLayout(new BorderLayout());
	JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割
	add(splitV,BorderLayout.CENTER);
	validate();

}
public void actionPerformed(ActionEvent e){
	Object obj=e.getSource();
	Statement stmt=null;
	ResultSet rs=null,rs1=null;
    String sql=null,sql1=null;
	
	if(obj==查找){if(课号1.getText().equals("")||学号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写完成查询的信息!" );
	else{
	     
	    sql1="select * from SC where Cno='"+课号1.getText()+"' and Sno='"+学号1.getText()+"'";
	    try{
	    Connection dbConn1=CONN();
		stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		rs1=stmt.executeQuery(sql1);
	    if(rs1.next()){课程号.setText(rs1.getString("Cno").trim());
	                   学号.setText(rs1.getString("Sno").trim());
	                   成绩.setText(rs1.getString("grade").trim());
	                   saveC=课号1.getText().trim();	
	                   saveS=学号1.getText().trim();
	    }
	    else{JOptionPane.showMessageDialog(this,"没有这个课号的学生" );}
	    stmt.close();
	    rs1.close();
	    }catch(SQLException e1){
			   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
		   }
	    }
	}
	else{
		if(obj==删除){if(saveC==null||saveS==null)JOptionPane.showMessageDialog(this,"还没查找需要修改的学生/课程" );
		else{sql="delete from SC where Cno='"+saveC+"' and Sno='"+saveS+"'";
		try{
		    Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			stmt.executeUpdate(sql);
			saveC=null;
			saveS=null;
			JOptionPane.showMessageDialog(this,"删除完成" );
			课程号.setText("");
            学号.setText("");
            成绩.setText("");
			stmt.close();
		    }catch(SQLException e1){
				   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
			   }
		}
		}
}
}
public static Connection CONN(){
	   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
	    String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
	   Connection dbConn=null;

	   try {
	   Class.forName(driverName);
	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
	   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
	   } catch (Exception e) {
	   e.printStackTrace();
	   }
	   return dbConn;
}

}

UpdateC

/*
UpdateC
*/
package sc;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class UpdateC extends JPanel implements ActionListener{
	String save=null;
	JTextField 课号1,课程号,课程名,先修课课程号,学分;
	JButton 修改,查找;
	
public UpdateC(){
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch(Exception e){System.err.println("不能设置外观:   "+e);}
	
	课号1=new JTextField(12);
	课程号=new JTextField(12);
	课程名=new JTextField(12);
	先修课课程号=new JTextField(12);
	学分=new JTextField(12);
	修改=new JButton("修改");
	查找=new JButton("查找");
	
	Box box1=Box.createHorizontalBox();//横放box
	Box box2=Box.createHorizontalBox();
	Box box3=Box.createHorizontalBox();
	Box box4=Box.createHorizontalBox();
	Box box5=Box.createHorizontalBox();
	Box box6=Box.createHorizontalBox();
	Box box7=Box.createHorizontalBox();
	box1.add(new JLabel("课程号:",JLabel.CENTER));
	box1.add(课程号);
	box2.add(new JLabel("课程名:",JLabel.CENTER));
	box2.add(课程名);
	box3.add(new JLabel("先修课课程号:",JLabel.CENTER));
	box3.add(先修课课程号);
	box4.add(new JLabel("学分:",JLabel.CENTER));
	box4.add(学分);
	box6.add(修改);
	box7.add(new JLabel("课号:",JLabel.CENTER));
	box7.add(课号1);
	box7.add(查找);
	Box boxH=Box.createVerticalBox();//竖放box
	boxH.add(box1);
	boxH.add(box2);
	boxH.add(box3);
	boxH.add(box4);
	boxH.add(box5);
	boxH.add(box6);
	
	
	修改.addActionListener(this);
    查找.addActionListener(this);
	
	
	boxH.add(Box.createVerticalGlue());
	JPanel picPanel=new JPanel();
	JPanel messPanel=new JPanel();
	messPanel.add(box7);
	picPanel.add(boxH);
	setLayout(new BorderLayout());
	JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割
	add(splitV,BorderLayout.CENTER);
	validate();
}

public void actionPerformed(ActionEvent e){
	Object obj=e.getSource();
	Statement stmt=null;
	ResultSet rs=null,rs1=null;
    String sql=null,sql1=null,sqlSC=null;
	
	if(obj==查找){if(课号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写查询的课号!" );
	else{
	    sql1="select * from Course where Cno='"+课号1.getText()+"'";
	    try{
	    Connection dbConn1=CONN();
		stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		rs1=stmt.executeQuery(sql1);
	    if(rs1.next()){课程号.setText(rs1.getString("Cno").trim());
        课程名.setText(rs1.getString("Cname").trim());
        先修课课程号.setText(rs1.getString("Cpno").trim());
        学分.setText(rs1.getString("Ccredit").trim());
	                   save=课号1.getText();	    	
	    }
	    else{JOptionPane.showMessageDialog(this,"没有这个课号的课程" );}
	    stmt.close();
	    rs1.close();
	    }catch(SQLException e1){
			   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
		   }
	    }
	}
	else{
	if(obj==修改){if(save==null){JOptionPane.showMessageDialog(this,"还没查找需要修改的课程" );}
	else{
		if(课程号.getText().equals("")||课程名.getText().equals("")||先修课课程号.getText().equals("")||学分.getText().equals("")){
			JOptionPane.showMessageDialog(this,"课程信息填满才能修改!" );
		}
		else{sql="update Course set Cno='"+课程号.getText()+"',Cname='"+课程名.getText()+"',Cpno='"+先修课课程号.getText()+"',Ccredit='"+学分.getText()+"' where Cno='"+save+"'";
		if(save.trim().equals(课程号.getText().trim())){
		try{
		    Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			stmt.executeUpdate(sql);
			save=null;
			JOptionPane.showMessageDialog(this,"修改完成" );
			课程号.setText("");
            课程名.setText("");
            stmt.close();
		    }catch(SQLException e1){
				   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
			   }
	}
		else{sql1="select * from Course where Cno='"+课程号.getText()+"'";
		try{
		    Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs1=stmt.executeQuery(sql1);
		    if(rs1.next()){  	JOptionPane.showMessageDialog(this,"已存在此课号课程" );
		    }
		    else{sqlSC="update SC set Cno='"+课程号.getText()+"' where Cno='"+save+"'";
		    stmt.executeUpdate(sql);
		    stmt.executeUpdate(sqlSC);
		    	save=null;
			JOptionPane.showMessageDialog(null,"修改完成" );
			课程号.setText("");
            课程名.setText("");
            先修课课程号.setText("");
            学分.setText("");}
		    stmt.close();
		    rs1.close();
		    }catch(SQLException e1){
				   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
			   }
		}
	}}}}
}
public static Connection CONN(){
	   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
	    String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
	   Connection dbConn=null;

	   try {
	   Class.forName(driverName);
	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
	   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
	   } catch (Exception e) {
	   e.printStackTrace();
	   }
	   return dbConn;
}

}



UpdateS

/*
UpdateS
*/
package sc;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class UpdateS extends JPanel implements ActionListener{
	String save=null;
	JTextField 学号1,学号,姓名,性别,年龄,所在系系名;
	JButton 修改,查找;
	
public UpdateS(){
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch(Exception e){System.err.println("不能设置外观:   "+e);}
	
	学号1=new JTextField(12);
	学号=new JTextField(12);
	姓名=new JTextField(12);
	性别=new JTextField(12);
	年龄=new JTextField(12);
	所在系系名=new JTextField(12);
	修改=new JButton("修改");
	查找=new JButton("查找");
	
	Box box1=Box.createHorizontalBox();//横放box
	Box box2=Box.createHorizontalBox();
	Box box3=Box.createHorizontalBox();
	Box box4=Box.createHorizontalBox();
	Box box5=Box.createHorizontalBox();
	Box box6=Box.createHorizontalBox();
	Box box7=Box.createHorizontalBox();
	box1.add(new JLabel("学号:",JLabel.CENTER));
	box1.add(学号);
	box2.add(new JLabel("姓名:",JLabel.CENTER));
	box2.add(姓名);
	box3.add(new JLabel("性别:",JLabel.CENTER));
	box3.add(性别);
	box4.add(new JLabel("年龄:",JLabel.CENTER));
	box4.add(年龄);
	box5.add(new JLabel("所在系系名:",JLabel.CENTER));
	box5.add(所在系系名);
	box6.add(修改);
	box7.add(new JLabel("学号:",JLabel.CENTER));
	box7.add(学号1);
	box7.add(查找);
	
	修改.addActionListener(this);
    查找.addActionListener(this);
	
	Box boxH=Box.createVerticalBox();//竖放box
	boxH.add(box1);
	boxH.add(box2);
	boxH.add(box3);
	boxH.add(box4);
	boxH.add(box5);
	boxH.add(box6);
	boxH.add(Box.createVerticalGlue());
	JPanel picPanel=new JPanel();
	JPanel messPanel=new JPanel();
	messPanel.add(box7);
	picPanel.add(boxH);
	setLayout(new BorderLayout());
	JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割
	add(splitV,BorderLayout.CENTER);
	validate();
}

public void actionPerformed(ActionEvent e){
	Object obj=e.getSource();
	Statement stmt=null;
	ResultSet rs=null,rs1=null;
    String sql=null,sql1=null,sqlSC;
	
	if(obj==查找){if(学号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写查询的学号!" );
	else{
	     
	    sql1="select * from Student where Sno='"+学号1.getText()+"'";
	    try{
	    Connection dbConn1=CONN();
		stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		rs1=stmt.executeQuery(sql1);
	    if(rs1.next()){学号.setText(rs1.getString("Sno").trim());
	                   姓名.setText(rs1.getString("Sname").trim());
	                   性别.setText(rs1.getString("Ssex").trim());
	                   年龄.setText(rs1.getString("Sage").trim());
	                   所在系系名.setText(rs1.getString("Sdept").trim());
	                   save=学号1.getText();	    	
	    }
	    else{JOptionPane.showMessageDialog(this,"没有这个学号的学生" );}
	    stmt.close();
	    rs1.close();
	    }catch(SQLException e1){
			   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
		   }
	    }
	}
	else{
	if(obj==修改){if(save==null){JOptionPane.showMessageDialog(this,"还没查找需要修改的学生" );}
	else{
		if(学号.getText().equals("")||姓名.getText().equals("")||性别.getText().equals("")||年龄.getText().equals("")||所在系系名.getText().equals("")){
			JOptionPane.showMessageDialog(this,"学生信息填满才能修改!" );
		}
		else{sql="update Student set Sno='"+学号.getText()+"',Sname='"+姓名.getText()+"',Ssex='"+性别.getText()+"',Sage='"+年龄.getText()+"',Sdept='"+所在系系名.getText()+"'where Sno='"+save+"'";
		if(save.trim().equals(学号.getText().trim())){
		try{
		    Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			stmt.executeUpdate(sql);
			save=null;
			JOptionPane.showMessageDialog(this,"修改完成" );
			学号.setText("");
            姓名.setText("");
            性别.setText("");
            年龄.setText("");
            所在系系名.setText("");
			stmt.close();
		    }catch(SQLException e1){
				   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
			   }
	}
		else{sql1="select * from Student where Sno='"+学号.getText()+"'";
		try{
		    Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs1=stmt.executeQuery(sql1);
		    if(rs1.next()){  	JOptionPane.showMessageDialog(this,"已存在此学号学生" );
		    }
		    else{
		    	sqlSC="update SC set Sno='"+学号.getText()+"' where Sno='"+save+"'";
		    	stmt.executeUpdate(sql);
		    	stmt.executeUpdate(sqlSC);
		    	save=null;
			JOptionPane.showMessageDialog(null,"修改完成" );
			学号.setText("");
            姓名.setText("");
            性别.setText("");
            年龄.setText("");
            所在系系名.setText("");}
		    stmt.close();
		    rs1.close();
		    }catch(SQLException e1){
				   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
			   }
		
		}
		
	}}}}
}
public static Connection CONN(){
	   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
	    String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
	   Connection dbConn=null;

	   try {
	   Class.forName(driverName);
	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
	   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
	   } catch (Exception e) {
	   e.printStackTrace();
	   }
	   return dbConn;
}

}

UpdateSC

/*
UpdateSC
*/
package sc;


import java.awt.*;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;
public class UpdateSC extends JPanel implements ActionListener{
	String saveC=null;
	String saveS=null;
	JTextField 课号1,学号1,学号,课程号,成绩;
	JButton 修改,查找;
	
public UpdateSC(){
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch(Exception e){System.err.println("不能设置外观:   "+e);}
	
	学号1=new JTextField(12);
	课号1=new JTextField(12);
	课程号=new JTextField(12);
	学号=new JTextField(12);
	成绩=new JTextField(12);
	修改=new JButton("修改");
	查找=new JButton("查找");
	
	Box box1=Box.createHorizontalBox();//横放box
	Box box2=Box.createHorizontalBox();
	Box box3=Box.createHorizontalBox();
	Box box4=Box.createHorizontalBox();
	Box box5=Box.createHorizontalBox();
	box1.add(new JLabel("课程号:",JLabel.CENTER));
	box1.add(课程号);
	box2.add(new JLabel("学号:",JLabel.CENTER));
	box2.add(学号);
	box3.add(new JLabel("成绩:",JLabel.CENTER));
	box3.add(成绩);
	box4.add(修改);
	box5.add(new JLabel("课号:",JLabel.CENTER));
	box5.add(课号1);
	box5.add(new JLabel("学号:",JLabel.CENTER));
	box5.add(学号1);
	box5.add(查找);
	Box boxH=Box.createVerticalBox();//竖放box
	boxH.add(box1);
	boxH.add(box2);
	boxH.add(box3);
	boxH.add(box4);
	boxH.add(Box.createVerticalGlue());
	
	修改.addActionListener(this);
    查找.addActionListener(this);
	
	JPanel picPanel=new JPanel();
	JPanel messPanel=new JPanel();
	messPanel.add(box5);
	picPanel.add(boxH);
	setLayout(new BorderLayout());
	JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割
	add(splitV,BorderLayout.CENTER);
	validate();
}
public void actionPerformed(ActionEvent e){
	Object obj=e.getSource();
	Statement stmt=null;
	ResultSet rs=null,rs1=null,rsC=null,rsS=null;
	String sql,sql1,sqlS,sqlC;
	
	if(obj==查找){if(课号1.getText().equals("")||学号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写完成查询的信息!" );
	else{
	    sql1="select * from SC where Cno='"+课号1.getText()+"' and Sno='"+学号1.getText()+"'";
	    try{
	    Connection dbConn1=CONN();
		stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		rs1=stmt.executeQuery(sql1);
	    if(rs1.next()){课程号.setText(rs1.getString("Cno").trim());
	                   学号.setText(rs1.getString("Sno").trim());
	                   成绩.setText(rs1.getString("grade").trim());
	                   saveC=课号1.getText().trim();	
	                   saveS=学号1.getText().trim();
	    }
	    else{JOptionPane.showMessageDialog(this,"没有这个课号的学生" );}
	    stmt.close();
	    rs1.close();
	    }catch(SQLException e1){
			   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
		   }
	    }
	}
	
	else{
		if(obj==修改){if(saveC==null||saveS==null)JOptionPane.showMessageDialog(this,"还没查找需要修改的学生/课程" );
		else{
			if(课程号.getText().equals("")||学号.getText().equals("")){
				JOptionPane.showMessageDialog(this,"课程信息填满才能修改!" );
			}
		else{
			sqlC="select * from Course where Cno='"+课程号.getText()+"'";
	    sqlS="select * from Student where Sno='"+学号.getText()+"'";
	    sql1="select * from SC where Cno='"+课程号.getText()+"' and Sno='"+学号.getText()+"'";
	    sql="update SC set Cno='"+课程号.getText()+"',Sno='"+学号.getText()+"',grade='"+成绩.getText()+"' where Cno='"+saveC+"' and Sno='"+saveS+"'";
   try{
	   Connection dbConn1=CONN();
		stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		rsC=stmt.executeQuery(sqlC);
		if(rsC.next()){
			rsS=stmt.executeQuery(sqlS);
			if(rsS.next()){
			if(课程号.getText().trim().equals(saveC)&& 学号.getText().trim().equals(saveS)){
			stmt.executeUpdate(sql);	
			JOptionPane.showMessageDialog(this,"添加成功");
			saveC=null;
			saveS=null;
				}
			else{rs1=stmt.executeQuery(sql1);
			if(rs1.next()){JOptionPane.showMessageDialog(this,"学生与课程号以存在,无法修改");}
			else{
				stmt.executeUpdate(sql);	
				JOptionPane.showMessageDialog(this,"添加成功");
				saveC=null;
				saveS=null;				
			}
			rs1.close();
			}
		}						
			else{JOptionPane.showMessageDialog(this,"该学生不存在,无法修改");}
			rsS.close();
		}
		else{JOptionPane.showMessageDialog(this,"该课程不存在,无法修改");}
		rsC.close();
		stmt.close();
   }
   catch(SQLException e1){
	   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
	   }
		}
		}
}
}
}
public static Connection CONN(){
	   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
	   String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
	   Connection dbConn=null;

	   try {
	   Class.forName(driverName);
	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
	   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
	   } catch (Exception e) {
	   e.printStackTrace();
	   }
	   return dbConn;
}

}



SelC

/*
SelC
*/
package sc;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.util.*;

public class SelC extends JFrame {
	Vector rowData, columnNames;
	Statement stmt=null;
	    String sql=null;
	JTable jt = null;
	JScrollPane jsp = null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	public SelC(String sql1){
		columnNames = new Vector();
		// 设置列名
		columnNames.add("课程号");
		columnNames.add("课程名");
		columnNames.add("先修课课程号");
		columnNames.add("学分");
		rowData=new Vector();
		sql=sql1;
		try{
		    Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs=stmt.executeQuery(sql);
		
			while(rs.next()){
				Vector hang = new Vector();
				hang.add(rs.getString("Cno"));System.out.print(rs.getString("Cno"));
				hang.add(rs.getString("Cname"));System.out.print(rs.getString("Cname"));
				hang.add(rs.getString("Cpno"));System.out.print(rs.getString("Cpno"));
				hang.add(rs.getString("Ccredit"));System.out.print(rs.getString("Ccredit"));
				rowData.add(hang);}
				jt=new JTable(rowData,columnNames);
				jsp=new JScrollPane(jt);
				this.add(jsp);
				this.setSize(400,300);
				this.setVisible(true);
	}catch(SQLException e1){
	   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
	   }
}
public static Connection CONN(){
	   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
	    String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
	   Connection dbConn=null;

	   try {
	   Class.forName(driverName);
	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
	   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
	   } catch (Exception e) {
	   e.printStackTrace();
	   }
	   return dbConn;
}

}


SelS

/*
SelS
*/
package sc;


import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class SelS extends JPanel implements ActionListener{
	JTextField 学号,姓名,性别,年龄,所在系系名;
	JButton 查找;
	JTextField 课程号,课程名,先修课课程号,学分;
	JButton 查找1;
	JTextField 课号1,学号1,成绩;
	JButton 查找2;

	public SelS (){
		学号=new JTextField(12);
		姓名=new JTextField(12);
		性别=new JTextField(12);
		年龄=new JTextField(12);
		所在系系名=new JTextField(12);
		课程号=new JTextField(12);
		课程名=new JTextField(12);
		先修课课程号=new JTextField(12);
		学分=new JTextField(12);
		课号1=new JTextField(12);
		学号1=new JTextField(12);
		成绩=new JTextField(12);
		查找=new JButton("查找学生信息");
		查找1=new JButton("查找课程信息");
		查找2=new JButton("查找选课信息");
	
		
		Box box1=Box.createHorizontalBox();//横放box
		Box box2=Box.createHorizontalBox();
		Box box4=Box.createHorizontalBox();
		Box box5=Box.createHorizontalBox();
		Box box6=Box.createHorizontalBox();
		Box box7=Box.createHorizontalBox();
		box1.add(new JLabel("学号:",JLabel.CENTER));
		box1.add(学号);
		box1.add(new JLabel("姓名:",JLabel.CENTER));
		box1.add(姓名);
		box1.add(new JLabel("性别:",JLabel.CENTER));
		box1.add(性别);
		box1.add(new JLabel("年龄:",JLabel.CENTER));
		box1.add(年龄);
		box1.add(new JLabel("所在系系名:",JLabel.CENTER));
		box1.add(所在系系名);
		box2.add(查找);
		
		box4.add(new JLabel("课程号:",JLabel.CENTER));
		box4.add(课程号);
		box4.add(new JLabel("课程名:",JLabel.CENTER));
		box4.add(课程名);
		box4.add(new JLabel("先修课课程号:",JLabel.CENTER));
		box4.add(先修课课程号);
		box4.add(new JLabel("学分:",JLabel.CENTER));
		box4.add(学分);
		box6.add(查找1);
		
		box5.add(new JLabel("课程号:",JLabel.CENTER));
		box5.add(课号1);
		box5.add(new JLabel("学号:",JLabel.CENTER));
		box5.add(学号1);
		box5.add(new JLabel("成绩:",JLabel.CENTER));
		box5.add(成绩);
		box7.add(查找2);

		Box boxH1=Box.createVerticalBox();//竖放box
		boxH1.add(box1);
		boxH1.add(box2);
		boxH1.add(Box.createVerticalGlue());
		Box boxH2=Box.createVerticalBox();//竖放box
		boxH2.add(box4);
		boxH2.add(box6);
		boxH2.add(Box.createVerticalGlue()); 
		Box boxH3=Box.createVerticalBox();//竖放box
		boxH3.add(box5);
		boxH3.add(box7);
		boxH3.add(Box.createVerticalGlue()); 
               
		查找.addActionListener(this);
		查找1.addActionListener(this);
		查找2.addActionListener(this);
        
		JPanel messPanel=new JPanel();
		JPanel picPanel=new JPanel();
		JPanel threePanel=new JPanel();
		messPanel.add(boxH1);
		picPanel.add(boxH2);
		threePanel.add(boxH3);
		setLayout(new BorderLayout());
		JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割
		add(splitV,BorderLayout.CENTER);
		JSplitPane splitV1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,splitV,threePanel);//分割
		add(splitV1,BorderLayout.CENTER);
		validate();
		
	
	}
	
	public void actionPerformed(ActionEvent c){
		Object obj=c.getSource();
		Statement stmt=null;
		ResultSet rs=null;
		int row=0;
		int i=0;
	    String sql=null;
	    //Student K;
	    SelC K1;
	    SelSC K2;
		if(obj==查找){			
			if(学号.getText().equals("")&&姓名.getText().equals("")&&性别.getText().equals("")&&年龄.getText().equals("")&&所在系系名.getText().equals("")){
				sql="select * from Student ";System.out.print("00");//00
			}
			else if(学号.getText().equals("")){
					if(姓名.getText().equals("")){
						if(性别.getText().equals("")){
							if(年龄.getText().equals("")) {
							sql="select * from Student where Sdept like'%"+所在系系名.getText()+"%'";System.out.print("10");
						}
							else {
								if(所在系系名.getText().equals("")) {
									sql="select * from Student where Sage like'%"+年龄.getText()+"%'";System.out.print("10");
								}
								else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";}
								}
						}
						else {
							if(年龄.getText().equals("")) {
								if(所在系系名.getText().equals("")) {
									sql="select * from Student where Ssex like'%"+性别.getText()+"%'";System.out.print("10");
								}
								else {sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";}
								
								
							}
								else {
									if(所在系系名.getText().equals("")) {
										sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'";System.out.print("10");
									}
									else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";}
									}
					}
					
					}
					else{
						if(性别.getText().equals("")){
							if(年龄.getText().equals("")) {
								if(所在系系名.getText().equals("")) {
									sql="select * from Student where Sname like'%"+姓名.getText()+"%'";System.out.print("10");
								}
								else {sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";}
								}
						
							else {
								if(所在系系名.getText().equals("")) {
									sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sage like '%"+年龄.getText()+"%'";System.out.print("10");
								}
								else {sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sage like '%"+年龄.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";}
								}
						}
						
						else {
							if(年龄.getText().equals("")) {
								if(所在系系名.getText().equals("")) {
									sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sname like '%"+姓名.getText()+"%'";System.out.print("10");
								}
								else {sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sname like '%"+姓名.getText()+"%'";}
								
								
							}
								else {
									if(所在系系名.getText().equals("")) {
										sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sname like '%"+姓名.getText()+"%'";System.out.print("10");
									}
									else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sname like '%"+姓名.getText()+"%'";}
									}
					}
					}
			}
					else{
						if(姓名.getText().equals("")){
							if(性别.getText().equals("")){
								if(年龄.getText().equals("")) {
									if(所在系系名.getText().equals("")) {
										sql="select * from Student where Sno like'%"+学号.getText()+"%'";System.out.print("10");
									}
									else {sql="select * from Student where Sno like'%"+学号.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";}
									
							}
								else {
									if(所在系系名.getText().equals("")) {
										sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");
									}
									else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+姓名.getText()+"%'";}
									}
							}
							else {
								if(年龄.getText().equals("")) {
									if(所在系系名.getText().equals("")) {
										sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");
									}
									else {sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+学号.getText()+"%'";}
									
									
								}
									else {
										if(所在系系名.getText().equals("")) {
											sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");
										}
										else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+学号.getText()+"%'";}
										}
						}
						
						}
						else{
							if(性别.getText().equals("")){
								if(年龄.getText().equals("")) {
									if(所在系系名.getText().equals("")) {
										sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");
									}
									else {sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+学号.getText()+"%'";}
									}
							
								else {
									if(所在系系名.getText().equals("")) {
										sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sage like '%"+年龄.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");
									}
									else {sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sage like '%"+年龄.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+学号.getText()+"%'";}
									}
							}
							
							else {
								if(年龄.getText().equals("")) {
									if(所在系系名.getText().equals("")) {
										sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sname like '%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");
									}
									else {sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sname like '%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";}
									
									
								}
									else {
										if(所在系系名.getText().equals("")) {
											sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sname like '%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");
										}
										else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sname like '%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";}
										}
						}
						}
					}
			
			
			//K=new Student(sql);
		 }
		else{if(obj==查找1){
			if(课程号.getText().equals("")&&课程名.getText().equals("")&&先修课课程号.getText().equals("")&&学分.getText().equals("")){
				sql="select * from Course ";System.out.print("00");//00
			}
			else if(课程号.getText().equals("")){
					if(课程名.getText().equals("")){
						if(先修课课程号.getText().equals("")){
							sql="select * from Course where Ccredit like'%"+学分.getText()+"%'";System.out.print("10");
						}
						else {
							if(学分.getText().equals("")) {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'";System.out.print("10");}
							else {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Ccredit like '%"+学分.getText()+"%'";}
							}
					}
					else {
						if(先修课课程号.getText().equals("")&&学分.getText().equals("")){
							sql="select * from Course where Cno like'%"+课程号.getText()+"%'";System.out.print("10");
						}
						else {
							if(先修课课程号.getText().equals("")){
								sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10");
							}
							else {
								if(学分.getText().equals("")) {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10");}
								else {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'and Ccredit like '%"+学分.getText()+"%'";}
						}
					}
				}
			}
				else {
						if(课程名.getText().equals("")){
							if(先修课课程号.getText().equals("")){
								if(学分.getText().equals("")) {
								sql="select * from Course where Cno like'%"+课程号.getText()+"%'";System.out.print("10");
							}
							else {sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10");}
						}
							else {
								if(学分.getText().equals("")) {
									sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10");
								}
								else {sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cpno like '%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10");}
							
							}
						}
						else {
							if(先修课课程号.getText().equals("")){
								if(学分.getText().equals("")) {
								sql="select * from Course where Cno like'%"+课程号.getText()+"%'and Cname like '%"+课程名.getText()+"%'";System.out.print("10");
							}
							else {sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cno like '%"+课程号.getText()+"%'and Cname like '%"+课程名.getText()+"%'";System.out.print("10");}
						}
							else {
								if(学分.getText().equals("")) {
									sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'and Cname like '%"+课程名.getText()+"%'";System.out.print("10");
								}
								else {sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cpno like '%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'and Cname like '%"+课程名.getText()+"%'";System.out.print("10");}
							
							}
						}
				}					
			
			 K1=new SelC(sql);}
		else{if(obj==查找2){
			if(课号1.getText().equals("")&&学号1.getText().equals("")&&成绩.getText().equals("")){
				sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("000");//000
			}
			else{
				if(课号1.getText().equals("")){
					if(学号1.getText().equals("")){
						sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where grade like'%"+成绩.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("001");}
					else{if(成绩.getText().equals("")){sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Sno like'%"+学号1.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("010");}
					     else{sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Sno like'%"+学号1.getText()+"%'and grade like'%"+成绩.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("011");}}}
				else{if(学号1.getText().equals("")){
					      if(成绩.getText().equals("")){sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Cno like'%"+课号1.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("100");}
				          else{sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Cno like'%"+课号1.getText()+"%' and grade like'%"+成绩.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("101");}}
				     else{if(成绩.getText().equals("")){sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where  SC.Cno like'%"+课号1.getText()+"%' and SC.Sno like'%"+学号1.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("110");}
				           else{sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where  SC.Cno like'%"+课号1.getText()+"%' and SC.Sno like'%"+学号1.getText()+"%' and grade like'%"+成绩.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("111");}}}
			}
			K2=new SelSC(sql);
			}
		}
		}
		}
}

SelSC

/*
SelSC
*/
package sc;


import java.sql.*;
import javax.swing.*;
//import connection.java;
import java.util.*;

public class SelSC extends JFrame {
	Vector rowData, columnNames;
	Statement stmt=null;
    String sql=null;
	JTable jt = null;
	JScrollPane jsp = null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	public SelSC(String sql1){
		columnNames = new Vector();
		// 设置列名
		columnNames.add("课程号");
	
		columnNames.add("学号");
		
		columnNames.add("成绩");

		rowData=new Vector();
		sql=sql1;
		try{
		    Connection dbConn1=CONN();
			stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs=stmt.executeQuery(sql);
			
			while(rs.next()){
				Vector hang = new Vector();
				hang.add(rs.getString("Sno"));System.out.print(rs.getString("Sno"));
				hang.add(rs.getString("Cno"));System.out.print(rs.getString("Cno"));
				
				
				hang.add(rs.getString("grade"));System.out.println(rs.getString("grade"));
				rowData.add(hang);}
				jt=new JTable(rowData,columnNames);
				jsp=new JScrollPane(jt);
	this.add(jsp);
	this.setSize(400,300);
	this.setVisible(true);
		}catch(SQLException e1){
				   System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
			   }
		}
	public static Connection CONN(){
		   String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动
		    String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名";   //连接服务器和数据库test
	   String userName = "sa";   //默认用户名
	   String userPwd = "密码";   //密码
		   Connection dbConn=null;

		   try {
		   Class.forName(driverName);
		   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
		   System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
		   } catch (Exception e) {
		   e.printStackTrace();
		   }
		   return dbConn;
	}

}

Logo

快速构建 Web 应用程序

更多推荐