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();

(学生成绩管理系统)&spm=1001.2101.3001.5002&articleId=113586726&d=1&t=3&u=65a6285defba4f128a749ce48b8e72ee)
5093

被折叠的 条评论
为什么被折叠?



