一创建基本表
create table SC (
Sno char(9),
Cno char(9),
Grade smallint,
primary key (Sno,Cno)
)
alter table SC add constraint from_stu_sc foreign key (Sno) references Student (Sno) on delete cascade
alter table Sc add constraint from_course_sc foreign key(Cno) references Course(Cno) on delete cascade
这个是为了实现级联的操作
二 连接数据库实现基本功能
选择
public void select (){
this.get_sql_connection();
//连接数据库
try{
this.ps=this.ct.prepareStatement("select * from Student");
this.rs=this.ps.executeQuery();
while(this.rs.next()) {
this.con.add(rs.getString(1));
System.out.println(rs.getString(1));
this.sno.add(rs.getString(2));
this.Cname.add(rs.getString(3));
}
System.out.println("数据成功加入");
}
catch (Exception e){
e.printStackTrace();
}
finally {
this.close();
}
}
插入
public void insert_values(String a,String b,String c){
this.get_sql_connection();
try{
Integer num=Integer.parseInt(c);
this.ps=this.ct.prepareStatement("use [Stu1] insert into Student(Sno,Sname,Sage) values(?,?,?)");
this.ps.setString(1,a); this.ps.setString(2,b); this.ps.setInt(3,num);
int i=ps.executeUpdate();
System.out.println("数据成功加入"+i+"行");
}
catch (Exception e){
e.printStackTrace();
System.out.println("插入失败");
}
finally {
this.close();
}
}
删除
public void delete ( String a ){
//这个是按照主码删除元组
this.get_sql_connection();
try{
String sql ="delete from Student where Sno=?";
this.ps=this.ct.prepareStatement(sql);
this.ps.setString(1,a);
int i= ps.executeUpdate();
System.out.println(i+"行 修改");
}
catch (Exception e){
e.printStackTrace();
}
finally {
this.close();
}
}
修改
public void update_data (String a,String b,String c){
this.get_sql_connection();
try{
int num=Integer.parseInt(c);
String sql ="update Student set Sname= ? ,Sage=? where Sno =?";
this.ps=this.ct.prepareStatement(sql);
this.ps.setString(1,b);
this.ps.setInt(2,num);
this.ps.setString(3,a);
int i=this.ps.executeUpdate();
System.out.println(i+"行 update");
}
catch (Exception e){
e.printStackTrace();
System.out.println("ok update ok!!");
}
finally {
this.close();
}
}
处理存储过程
public void call_produce(){
String Sno,Cname,Sage;
this.get_sql_connection();
try{
//尝试做
this.callableStatement= this.ct.prepareCall("{call pro_tran(?,?)}");
this. callableStatement.registerOutParameter(1, Types.SMALLINT);
this.callableStatement.setString(2,"1");
this.callableStatement.execute();
System.out.println("是最大值"+this.callableStatement.getInt(1));
}catch (Exception e){
e.printStackTrace();
System.out.println("sorry!!1");
}
finally {
this.close();
}
}
在数据库中实现的代码
% 建立存储过程
if exists(select * from sys.objects where name='pro_tran')
drop procedure pro_tran
create procedure pro_tran
@Sno char(9)
as
begin
select SC.Sno, Course.Cno ,Course.Cname
from SC ,Course
where SC.Sno=@Sno and SC.Cno=Course.Cno
end
在数据库中创建基本表 SC,Student ,Course
create table Course(
Cno char(9) primary key,
Cname char(9) ,
Grade smallint
)
create table SC (
Sno char(9),
Cno char(9),
Grade smallint,
foreign key (Sno) references Student(Sno),
foreign key(Cno) references Course(Cno),
primary key (Sno,Cno)
)
------------ Course 插入数据
insert into Course values('1','高数',4)
insert into Course values('2','概率',4)
insert into Course values('3','线代',4)
insert into Course values('4','java',2)
insert into Course values('5','c++',2)
insert into Course values('6','python',2)
insert into Course values('7','毛概',4)
insert into Course values('8','马原',4)
---- SC 插入数据
在数据库中建立触发器
create trigger Check_Course_grade
on Course
for insert ,update
as
declare @num smallint,
@cno char(9),
@cname char(9)
select @num= Grade from inserted
select @cno=Cno from inserted
select @cname= Cname from inserted
begin
if(@num>4)
if(exists( select * from Course where Cno=@cno))
update Course set Grade =4
if not exists(select * from Course where Cno=@cno)
insert into Course values(@cno,@cname,@num)
end
--更新或者是插入都会更新到inserted 表中
SQLserver 中的触发器有三种,分别是DML,DDL,和登录触发器。DML是数据操作触发器
学生的功能
//按钮
public JButton jb1_insert;
public JButton jb3_delete;
public JButton jb4_select;
public JButton jb5_return ;
// 输入框
public JTextField insert,insert1,delete,delete1,select,select1;
public JPanel jPanel_all;
public JPanel jPanel; public JPanel jPanel1,jPanel2;
public JLabel jLabel1;//提示信息
//连接数据的 类
public Test test;
//提示输入信息
public JLabel jl,jl1,jl2,jl3,jl4,jl5;
public TableText(Login_in login_in){
this.login_in= login_in;
this.test= new Test();
this.test.select("Course");
String [] str1={ "Cno","Cname","grade"};
this.table= new MyDefaultTable(str1,22);
//表格中添加信息
for(int i=1;i<=this.test.con.size();i++){
this.table.setValueAt(this.test.con.get(i-1),i-1,0);
this.table.setValueAt(this.test.sno.get(i-1),i-1,1);
this.table.setValueAt(this.test.Cname.get(i-1),i-1,2);
}
this.test.vector_free();
this.jTable= new JTable(this.table);this.jScrollPane= new JScrollPane(this.jTable);
//输入框
this.insert= new JTextField(20); this.insert1=new JTextField(20);this.delete=new JTextField(20);this.delete1= new JTextField(20);
this.select= new JTextField(20);this.select1=new JTextField(20);
//提示输入信息
this.jl= new JLabel("学号"); this.jl1= new JLabel("选择的课程");
this.jl2= new JLabel("学号");this.jl3= new JLabel("选择的课程");
this.jl4= new JLabel("学号");this.jl5= new JLabel("选择的课程");
//按钮
this.jb1_insert= new JButton("插入"); this.jb3_delete= new JButton("删除");
this.jb4_select= new JButton("查看"); this.jb5_return= new JButton("返回");
this.jb1_insert.addActionListener(this); this.jb3_delete.addActionListener(this);
this.jb4_select.addActionListener(this); this.jb5_return.addActionListener(this);
this.jPanel= new JPanel(); this.jPanel1= new JPanel();this.jPanel2=new JPanel();//四个不同的
this.jLabel1= new JLabel("以上是可以选择的选课信息,你可以进行查询,修改,添加,删除操作,输入你学号以及要选择的课程!!");
this.jPanel_all= new JPanel();
this.jPanel_all.setLayout(new GridLayout(4,1));
//分为4行1列添加组件
this.jPanel.add(this.jb1_insert); this.jPanel.add(this.jl);this.jPanel.add(this.insert);this.jPanel.add(this.jl1);this.jPanel.add(this.insert1);
this.jPanel1.add(this.jb3_delete);this.jPanel1.add(this.jl2); this.jPanel1.add(this.delete);this.jPanel1.add(this.jl3);this.jPanel1.add(this.delete1);
this.jPanel2.add(this.jb4_select);this.jPanel2.add(this.jl4);this.jPanel2.add(this.select);//this.jPanel2.add(this.jl5);this.jPanel2.add(this.select1);
this.jPanel2.add(this.jb5_return);
this.setLayout(new GridLayout(2,1));
this.setSize(800,600);
this.setLocationRelativeTo(null);
this.getContentPane().add(this.jScrollPane);
this.jPanel_all.add(this.jLabel1);this.jPanel_all.add(this.jPanel); this.jPanel_all.add(this.jPanel1);this.jPanel_all.add(this.jPanel2);
this.getContentPane().add(this.jPanel_all);
this.setVisible(true);
this.setDefaultCloseOperation(Windows.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
// insert按钮
if(actionEvent.getSource().equals(this.jb1_insert)){
String a=this.insert.getText();String b=this.insert1.getText();
try {
this.test.insert_SC_by_Stu(a,b);
} catch (SQLException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(this,"insert 成功!!");
}
//select 按钮
if(actionEvent.getSource().equals(this.jb4_select)){
if(this.se==null){
this.se= new select_table(this);
}
this.se.into(this.select.getText());
this.setVisible(false);
this.se.setVisible(true);
}
//delete 按钮
if(actionEvent.getSource().equals(this.jb3_delete)){
String a =this.delete.getText(); String b= this.delete1.getText();
try {
this.test.delete_SC(a,b);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("ok 删除成功!!!");
JOptionPane.showMessageDialog(this,"delete 成功!!!");
}
//return 按钮
if(actionEvent.getSource().equals(this.jb5_return)){
this.setVisible(false);
this.login_in.setVisible(true);
}
}
}
教师代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Base64;
import javax.swing.event.*;
public class Windows extends JFrame implements ActionListener {
public Test test;
public Login_in login_in =null;
protected Cour_tea cour_tea;
private Stu_tea stu_tea;
private SC_tea sc_tea;
public JButton jb_back;
public JButton jb_Student,jb_SC,jb_Course;
// 容器
public JPanel jp_Student,jp_SC,jp_Course;
public JPanel jp_all ;public JPanel jp_back;
//图片
public ImageIcon Icon ; public JLabel jl1;
// 提示信息
public JLabel jl_Student,jl_SC,jl_Course;
public Windows(Login_in login_in ,Test test){
super("SERVER FOR TEACHER");
this.login_in=login_in;
this.test= test;
//定义图片
this.Icon= new ImageIcon(getClass().getResource("/photo/5.jpg"));//使用的是getResource
this.Icon.setImage(this.Icon.getImage().getScaledInstance(500,300,Image.SCALE_DEFAULT));
this.jl1= new JLabel();
this.jl1.setIcon(this.Icon);//设置图片位置
//按钮
this.jb_back= new JButton("return");
this.jb_back.addActionListener(this);
this.jb_SC= new JButton("SC "); this.jb_Student= new JButton("Student");this.jb_Course= new JButton("Course");
this.jb_SC.addActionListener(this); this.jb_Course.addActionListener(this); this.jb_Student.addActionListener(this);
this.jb_back.addActionListener(this);
this.jp_all= new JPanel(); this.jp_back= new JPanel();
this.jp_Course= new JPanel();this.jp_SC= new JPanel();this.jp_Student= new JPanel();
//提示信息
this.jl_Course= new JLabel("Course 功能"); this.jl_SC= new JLabel("SC 功能");this.jl_Student= new JLabel("Student 功能");
//设置尺寸大小 布局方式
this.setSize(500,550);
this.setLayout(new FlowLayout());
this.jp_all.setLayout(new GridLayout(4,1));
// 容器添加组件
this.getContentPane().add(this.jl1);
this.getContentPane().add(this.jb_SC);this.getContentPane().add(this.jb_Student);this.getContentPane().add(this.jb_Course);this.getContentPane().add(this.jb_back);
this.jp_all.add(this.jp_Student);this.jp_all.add(this.jp_SC) ;this.jp_all.add(this.jp_Course);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource().equals(this.jb_back)){
// 返回主页面
this.setVisible(false);
this.login_in.setVisible(true);
}
if(actionEvent.getSource().equals(this.jb_SC)){
if(this.sc_tea==null){
this.sc_tea=new SC_tea(this,this.test);
}
else
this.sc_tea.setVisible(true);
this.setVisible(false);
}
if(actionEvent.getSource().equals(this.jb_Student)){
if(this.stu_tea==null){
this.stu_tea=new Stu_tea(this,this.test);
}
else this.stu_tea.setVisible(true);
this.setVisible(false);
}
if(actionEvent.getSource().equals(this.jb_Course)){
if(this.cour_tea==null){
this.cour_tea= new Cour_tea(this,this.test);
}
else this.cour_tea.setVisible(true);
this.setVisible(false);
}
}
}
SC 表对应的类
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class SC_tea extends JFrame implements ActionListener {
//表格信息
public Windows windows;
private MyDefaultTable table;
private JTable jTable;
private JScrollPane jScrollPane;
private Test test;
//按钮
protected JButton jb_insert ,jb_select ,jb_update,jb_delete,jb_back,jb_order,jb_select2,jb_select_sc;
//布局容器
protected JPanel jp_all,jp_insert ,jp_update,jp_delete,jp_select,jp_back;
//输入信息
protected JTextField jt_insert,jt_insert2,jt_insert3;
protected JTextField jt_update,jt_update2,jt_update3;
protected JTextField jt_delete,jt_delete2,jt_delete3;
protected JTextField jt_select,jt_order,jt_select2;
//提示信息
protected JLabel jl_insert,jl_insert2,jl_insert3;
protected JLabel jl_update,jl_update2,jl_update3;
protected JLabel jl_select,jl_order,jl_select2;
protected JLabel jl_delete,jl_delete2,jl_delete3;
//构造
public SC_tea(Windows windows, Test test){
//表格
super("SC");
this.windows=windows;
this.test=test;
String []str={"Sno","Cno","Grade"};
this.table=new MyDefaultTable(str,22);
this.jb_select2= new JButton("查询课程");
this.jb_select_sc=new JButton("查看所有");
this.jTable= new JTable(this.table);
this.jScrollPane= new JScrollPane(this.jTable);
//按钮
this.jb_delete= new JButton("删除");this.jb_insert= new JButton("插入");
this.jb_select= new JButton("查看学生");this.jb_update= new JButton("更新");
this.jb_update.addActionListener(this); this.jb_select.addActionListener(this);
this.jb_insert.addActionListener(this);this.jb_delete.addActionListener(this);
this.jb_back= new JButton("return"); this.jb_back.addActionListener(this);
this.jb_order= new JButton("排序");this.jb_order.addActionListener(this);
this.jb_select_sc.addActionListener(this);
this.jb_select2.addActionListener(this);
//输入信息 提示信息
// insert
this.jt_insert= new JTextField(10);this.jt_insert2= new JTextField(10);this.jt_insert3= new JTextField(10);
this.jl_insert = new JLabel("学号"); this.jl_insert2= new JLabel("选课编号"); this.jl_insert3= new JLabel("成绩");
// update
this.jt_update= new JTextField(10);this.jt_update2= new JTextField(10);this.jt_update3= new JTextField(10);
this.jl_update = new JLabel("学号"); this.jl_update2= new JLabel("选课编号");this.jl_update3= new JLabel("成绩");
// select
this.jt_select = new JTextField(10); this.jl_select2= new JLabel("输入课程编号"); this.jt_select2= new JTextField(10);
this.jl_select= new JLabel("学号"); this.jt_order= new JTextField(10); this.jl_order=new JLabel("输入课程编号");
// delete
this.jt_delete= new JTextField(10);this.jt_delete2= new JTextField(10);this.jt_delete3= new JTextField(10);
this.jl_delete= new JLabel("学号");this.jl_delete2= new JLabel("选课编号");this.jl_delete3= new JLabel("成绩");
//容器
this.jp_all= new JPanel();this.jp_insert= new JPanel();this.jp_select= new JPanel();this.jp_update= new JPanel();
this.jp_delete= new JPanel(); this.jp_back=new JPanel();
//布局
this.setSize(600,500);
this.setLayout(new GridLayout(2,1));
this.getContentPane().add(this.jScrollPane);
this.jp_all.setLayout(new GridLayout(5,1));
// jp_insert
this.jp_insert.add(this.jb_insert);this.jp_insert.add(this.jl_insert);this.jp_insert.add(this.jt_insert);
this.jp_insert.add(this.jl_insert2);this.jp_insert.add(this.jt_insert2);this.jp_insert.add(this.jl_insert3);this.jp_insert.add(this.jt_insert3);
// jp_update
this.jp_update.add(this.jb_update);this.jp_update.add(this.jl_update);this.jp_update.add(this.jt_update);this.jp_update.add(this.jl_update2);
this.jp_update.add(this.jt_update2);this.jp_update.add(this.jl_update3);this.jp_update.add(this.jt_update3);
// jp_delete
this.jp_delete.add(this.jb_delete);this.jp_delete.add(this.jl_delete);this.jp_delete.add(this.jt_delete);
this.jp_delete.add(this.jl_delete2);this.jp_delete.add(this.jt_delete2);
this.jp_delete.add(this.jl_delete3);this.jp_delete.add(this.jt_delete3);
//jp_select
this.jp_select.add(this.jb_select);this.jp_select.add(this.jl_select);this.jp_select.add(this.jt_select);
this.jp_select.add(this.jb_select2);this.jp_select.add(this.jl_select2);this.jp_select.add(this.jt_select2);
//
this.jp_back.add(this.jb_select_sc);
this.jp_back.add(this.jb_order); this.jp_back.add(this.jl_order);this.jp_back.add(this.jt_order);this.jp_back.add(this.jb_back);
//布局
this.jp_all.add(this.jp_insert);this.jp_all.add(this.jp_update);this.jp_all.add(this.jp_delete);this.jp_all.add(this.jp_select);this.jp_all.add(this.jp_back);
//设置
this.getContentPane().add(this.jp_all);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(Windows.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void into (){
try{
int i=0;
this.test.get_sql_connection();
this.test.ps=this.test.ct.prepareStatement("select * from SC ");
this.test.rs= this.test.ps.executeQuery();
while (this.test.rs.next()){
this.table.setValueAt(this.test.rs.getString(1),i,0);
this.table.setValueAt(this.test.rs.getString(2),i,1);
this.table.setValueAt(this.test.rs.getString(3),i,2);
i++;
}
for (int j=i;j<22;j++){
this.table.setValueAt(null,i,0);
this.table.setValueAt(null,i,1);
this.table.setValueAt(null,i,2);
}
}catch (Exception e){
e.printStackTrace();
}
finally {
this.test.close();
}
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
// insert
if(actionEvent.getSource().equals(this.jb_insert)){
String a=this.jt_insert.getText();
String b= this.jt_insert2.getText();
String c= this.jt_insert3.getText();
try {
this.test.insert_SC_by_tea(a,b,c);
} catch (SQLException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(this,"加入成功");
}
//delete
if(actionEvent.getSource().equals(this.jb_delete)){
String a=this.jt_delete.getText();
String b=this.jt_delete2.getText();
try {
this.test.delete_SC(a,b);
} catch (SQLException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(this,"删除成功!!");
}
// update
if(actionEvent.getSource().equals(this.jb_update)){
String a=this.jt_update.getText();
String b=this.jt_update2.getText();
String c=this.jt_update3.getText();
System.out.println(c);
this.test.update_sc_by_tea(a,b,c);
JOptionPane.showMessageDialog(this,"更新成功!");
}
//select
if(actionEvent.getSource().equals(this.jb_select)){
String a=this.jt_select.getText();
try{
int i=0;
this.test.get_sql_connection();
this.test.ps=this.test.ct.prepareStatement("select * from SC where Sno ="+a);
this.test.rs= this.test.ps.executeQuery();
while (this.test.rs.next()){
this.table.setValueAt(this.test.rs.getString(1),i,0);
this.table.setValueAt(this.test.rs.getString(2),i,1);
this.table.setValueAt(this.test.rs.getString(3),i,2);
i++;
}
for(int j=i;j<22;j++){
this.table.setValueAt(null,j,0);
this.table.setValueAt(null,j,1);
this.table.setValueAt(null,j,2);
}
}catch (Exception e){
e.printStackTrace();
}
finally {
this.test.close();
}
}
if(actionEvent.getSource().equals(this.jb_select2)){
String a=this.jt_select2.getText();
try{
int i=0;
this.test.get_sql_connection();
this.test.ps=this.test.ct.prepareStatement("select * from SC where Cno ="+a);
this.test.rs= this.test.ps.executeQuery();
while (this.test.rs.next()){
this.table.setValueAt(this.test.rs.getString(1),i,0);
this.table.setValueAt(this.test.rs.getString(2),i,1);
this.table.setValueAt(this.test.rs.getString(3),i,2);
i++;
}
for(int j=i;j<22;j++){
this.table.setValueAt(null,j,0);
this.table.setValueAt(null,j,1);
this.table.setValueAt(null,j,2);
}
}catch (Exception e){
e.printStackTrace();
}
finally {
this.test.close();
}
}
if(actionEvent.getSource().equals(this.jb_order)){
String a=this.jt_order.getText();
try{
int i=0;
this.test.get_sql_connection();
this.test.ps=this.test.ct.prepareStatement("select * from SC where Cno ="+a+" order by Grade");
this.test.rs= this.test.ps.executeQuery();
while (this.test.rs.next()){
this.table.setValueAt(this.test.rs.getString(1),i,0);
this.table.setValueAt(this.test.rs.getString(2),i,1);
this.table.setValueAt(this.test.rs.getString(3),i,2);
i++;
}
for(int j=i;j<22;j++){
this.table.setValueAt(null,j,0);
this.table.setValueAt(null,j,1);
this.table.setValueAt(null,j,2);
}
}catch (Exception e){
e.printStackTrace();
}
finally {
this.test.close();
}
}
if(actionEvent.getSource().equals(this.jb_select_sc)){
this.into();
}
//返回
if(actionEvent.getSource().equals(this.jb_back)){
this.windows.setVisible(true);
this.setVisible(false);
}
}
}