JAVA数据库课程设计--学生选课管理系统的

2018-11-12 20:31

add3.addActionListener(this); delete3.addActionListener(this); updata3.addActionListener(this); 学生查询.addActionListener(this); card=new CardLayout(); pCenter=new JPanel(); pCenter.setLayout(card);

增加学生界面=new Addstu(); 修改学生界面=new Updatastu(); 删除学生界面=new Delstu(); 增加课程界面=new AddC(); 删除课程界面=new DelC(); 修改课程界面=new UpdateC(); 增加选课界面=new AddSC(); 删除选课界面=new DelSC(); 修改选课界面=new UpdateSC(); 学生查询界面=new Selstu();

pCenter.add(\欢迎界面\ pCenter.add(\增加学生界面\增加学生界面); pCenter.add(\修改学生界面\修改学生界面); pCenter.add(\删除学生界面\删除学生界面); pCenter.add(\增加课程界面\增加课程界面); pCenter.add(\删除课程界面\删除课程界面); pCenter.add(\修改课程界面\修改课程界面); pCenter.add(\增加选课界面\增加选课界面); pCenter.add(\删除选课界面\删除选课界面); pCenter.add(\修改选课界面\修改选课界面); pCenter.add(\学生查询界面\学生查询界面); add(pCenter,BorderLayout.CENTER); validate(); setVisible(true); setBounds(400,150,400,280); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

addWindowListener(new WindowAdapter(){//关闭程序时的操作 public void windowClosing(WindowEvent e){System.exit(0);} }); validate();}

private void addJMenuBar() { mb.add(m1);mb.add(m2);mb.add(m3);mb.add(m4);mb.add(m5);} private void addMenu4() { m4.add(学生查询);m4.setFont(t);} private void addMenu3() { m3.add(add3);m3.add(updata3);m3.add(delete3);m3.setFont(t); } private void addMenu2() {//将菜单加入到菜单栏中 m2.add(add2);m2.add(updata2);m2.add(delete2);m2.setFont(t); }

private void addMenu1() { m1.add(add1);m1.add(updata1);m1.add(delete1);m1.setFont(t); } public void actionPerformed(ActionEvent e){ Object obj=e.getSource(); if(obj==m5){System.exit(0);} else{if(obj==add1){card.show(pCenter,\增加学生界面\ else{if(obj==updata1){ card.show(pCenter,\修改学生界面\ else{if(obj==delete1){ card.show(pCenter, \删除学生界面\ else{if(obj==add2){ card.show(pCenter, \增加课程界面\ else{if(obj==delete2){ card.show(pCenter, \删除课程界面\ else{if(obj==updata2){ card.show(pCenter, \修改课程界面\ else{if(obj==add3){ card.show(pCenter, \增加选课界面\ else{if(obj==delete3){ card.show(pCenter, \删除选课界面\ else{if(obj==updata3){ card.show(pCenter, \修改选课界面\ else{if(obj==学生查询){ card.show(pCenter, \学生查询界面\ }}}}}}}}}}}} public static void main(String[] args) {new Menu();}} 效果图:

添加界面:AddC.java/AddSC.java/Addstu.java

添加数据时,若遇到必须信息未填写、不能重复的信息在数据库中已存在,都会提示无法添加及其错误原因。

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 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(课名); 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();}

效果图:

添加界面:UpdateC.java/UpdateSC.java /Updatestu.java

先查找你想要修改的项目,若修改的信息包括号码(学号、课号),则会进行查询,如果修改后的号码已存在,以及修改后必须的信息变成空都会提示无法修改和其错误原因。此外,若修改C表或则S表,会连同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 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(修改);

box5.add(new JLabel(\课号:\,JLabel.CENTER)); box5.add(课号1); box5.add(查找);

修改.addActionListener(this);

Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4);

boxH.add(Box.createVerticalGlue()); JPanel picPanel=new JPanel(); JPanel messPanel=new JPanel(); messPanel.add(box5); picPanel.add(boxH);

setLayout(new BorderLayout()); JSplitPane splitV=new

查找.addActionListener(this);

JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割 add(splitV,BorderLayout.CENTER);

validate();}

删除界面:DelC.java/DelSC.java /Deltu.java

先查找你想要删除的项目,然后删除,此外,若删除C表或则S表的内容,会连同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 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(课名); box4.add(删除);

box5.add(new JLabel(\课号:\,JLabel.CENTER)); box5.add(课号1);

一、课程设计目的

通过这次的设计,主要是做出一个小型的管理系统,来加强对JAVA所学知识的巩固和融会贯通,可以说是对一个学期所学知识的一个小结,加深对JAVA数据库的理解。

二、需求分析 功能需求分析:

该系统具备管理学生信息、课程信息、选课信息的功能:用户通过输入账号和密码进下该系统后,可以进行一些基础维护(学生信息维护、课程信息维护、选课信息维护)。全部都可以进行增加、修改、删除、模糊查询。

三、数据项:

1表admin(用户表)

Field Username password Name

2表S(学生信息表)

Field Sno Sname Sx

3表C(课程信息表)

Field Cno Cname

Type

Null

Key PRI ——

Comment 课号 课名

Type

Null

Key PRI —— ——

Comment 学号 姓名 系别

Type char(10) char(10) Char(10)

Null —— —— ——

Key PRI —— ——

Comment 用户名 密码 用户昵称

nvarchar(50) —— nvarchar(50) —— nvarchar(50) ——

nvarchar(50) —— nvarchar(50) ——

4表SC(学生信息表)

Field Cno Sno C

Type

Null

Key PRI PRI ——

Comment 课号 学号 成绩

nvarchar(50) —— nvarchar(50) —— nvarchar(50) ——

四、系统功能描述

该小型系统主要是用于管理学生及课程的基本信息,主要功能包括四方面的: 1.管理学生信息,其中包括添加,删除,修改等操作。 2.管理课程信息,其中包括添加,删除,修改等操作。 3.管理选课信息,其中包括添加,删除,修改等操作。

4.查询信息,其中包括查询学生信息,查询课程信息,查询选课信息等操作。 学生信息管理系统 学课选 生程课 管管管 理 理 理 添 加 信 息

查询管理 修改信息 删除信息 添加信息修改信息 删除信息 添加信息修改信息 删除信息 学生信息课程信息 选课信息

五、代码分析

连接数据库方法:

这是java连接数据库的驱动,有关数据库的操作都要用到他。

public static Connection CONN(){

String driverName =

\; //加载JDBC驱动 String dbURL = \

DatabaseName=student\; //连接服务器和数据库test,此处student是数据库名 String userName = \; //默认用户名 String userPwd = \; //密码 Connection dbConn=null; try {

Class.forName(driverName);

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

用户登录界面user.java:

登录时,凡是账号密码未填写、输入错误账号密码都会提出错误提示框。在填写好账号密码后,会读取数据库里admin表,并查询其输入是否存在,若无误,则登录到用户界面。

public class User extends JFrame{ private JLabel use,password; private JTextField k1;//用户名输入框 private JPasswordField k2;//密码输入框 private JButton b1,b2; //登录窗口

public User(JFrame f){

super(\系统登录\);

Container c=getContentPane(); c.setLayout(new FlowLayout()); use=new JLabel(\);

use.setFont(new Font(\,Font.PLAIN,20)); password=new JLabel(\);

password.setFont(new Font(\,Font.PLAIN,20)); k1=new JTextField(12); k2=new JPasswordField(12); b1=new JButton(\登录\); b2=new JButton(\退出\);

// 设置登录方法

BHandler b=new BHandler(); EXIT d=new EXIT(); b1.addActionListener(b); b2.addActionListener(d); //添加控件 c.add(use); c.add(k1); c.add(password); c.add(k2); c.add(b1); c.add(b2);

setBounds(600,300,250,150); setVisible(true); setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}

//主函数

public static void main(String[] args) {

User f1=new User(new JFrame());} 效果图:

用户界面:Menu.java

用户界面能菜单栏有4个一级菜单,学生管理、课程管理、选课管理都能添加、修改、删除数据,分别操作数据库里的S表(学生),C表(课程),SC表(选课)。查询管理则可进行三个表的查询。 import java.awt.*;

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

public class Menu extends JFrame implements ActionListener{ Addstu 增加学生界面; Updatastu 修改学生界面; Delstu 删除学生界面; AddC 增加课程界面; DelC 删除课程界面; UpdateC 修改课程界面; AddSC 增加选课界面; DelSC 删除选课界面; UpdateSC 修改选课界面;

Selstu 学生查询界面; JPanel pCenter;

CardLayout card=null; JLabel label=null;

JMenuBar mb=new JMenuBar();//菜单栏 JMenu m1=new JMenu(\学生管理\

JMenuItem add1=new JMenuItem(\ \

JMenuItem updata1=new JMenuItem(\ \JMenuItem delete1=new JMenuItem(\ \JMenu m2=new JMenu(\课程管理\

JMenuItem add2=new JMenuItem(\ \

JMenuItem updata2=new JMenuItem(\ \JMenuItem delete2=new JMenuItem(\ \JMenu m3=new JMenu(\选课管理\

JMenuItem add3=new JMenuItem(\ \

JMenuItem updata3=new JMenuItem(\ \JMenuItem delete3=new JMenuItem(\ \JMenu m4=new JMenu(\查询管理\

JMenuItem 学生查询=new JMenuItem(\查询信息 \JMenuItem m5=new JMenuItem(\系统退出\Font t=new Font (\public Menu (){ this.setTitle(\学生选课管理系统\ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println(\不能设置外观: \ //组合菜单 addMenu1(); addMenu2(); addMenu3(); addMenu4(); addJMenuBar(); setJMenuBar(mb); label=new JLabel(\选课管理系统\ label.setFont(new Font(\宋体\ label.setHorizontalTextPosition(SwingConstants.CENTER); label.setForeground(Color.red); //点击事件 add1.addActionListener(this); updata1.addActionListener(this); delete1.addActionListener(this); m5.addActionListener(this); add2.addActionListener(this); delete2.addActionListener(this); updata2.addActionListener(this);

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

查询界面:Selsto.java

可以以任意条件模糊查找相关的表。

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

public class Selstu extends JPanel implements ActionListener{

JTextField 学号,姓名,系别; JButton 查找;

JTextField 课号,课名; JButton 查找1;

JTextField 课号1,学号1,成绩; JButton 查找2; public Selstu (){

学号=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(系别); box2.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();}

效果图:

下面是java的源代码,总共有15个类。分别是AddC.java,AddSC.java,Addstu.java, Delstu.java,DelC.java, DelSC.java,Menu.java,SelC.java,SelSC.java,Selstu.java,Student.java,Updatastu.java,UpdateC.java,UpdateSC.java,User.java。

因为本人太懒,所以一些类名的错误没有修改,也没有优化系统,总之还算是能运行的,数据库用的是sql2005,如果用别的数据库就要改驱动,具体自己上网百度吧。如果自己有空,优化一下还是很好滴……

AddC.jaba:

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(\不能设置外观: \ 课号=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(课名); 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,\学生信息请填满再录入!\ } Statement stmt=null; ResultSet rs=null,rs1=null; String sql,sql1; sql1=\课号.getText()+\ sql=\课号.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(\ } } }

//连接数据库方法

public static Connection CONN(){ String driverName = \ //加载JDBC驱动 String dbURL = \ //连接服

务器和数据库test String userName = \ //默认用户名 String userPwd = \ //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println(\ //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; } }

AddSC.java:

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(\不能设置外观: \ 课号=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=\课号.getText()+\ sqlS=\学号.getText()+\ sql1=\课号.getText()+\学号.getText()+\ sql=\课号.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(\ } } } }

//连接数据库方法

public static Connection CONN(){ String driverName = \ //加载JDBC驱动 String dbURL = \ //连接服务器和数据库test String userName = \ //默认用户名 String userPwd = \ //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println(\ //如果连接成功 控制台输出

Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; } }

Addstu.jaba: import java.awt.*; import javax.swing.*; import java.sql.*;

import java.awt.event.*;

public class Addstu extends JPanel implements ActionListener{ JTextField 学号,姓名,系别; JButton 录入; public Addstu(){ try

{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println(\不能设置外观: 学号=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(\系别.getText().equals(\ JOptionPane.showMessageDialog(this,\学生信息请填满再录入!\ } Statement stmt=null; ResultSet rs1=null; String sql,sql1; sql1=\学号.getText()+\ sql=\into S values('\学号.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(\Exception occur.Message is:\ } } }

//连接数据库方法

public static Connection CONN(){ String driverName = \ //加载JDBC驱动 String dbURL = \DatabaseName=student\ //连接服务器和数据库test String userName = \ //默认用户名 String userPwd = \ //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println(\Successful!\ //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; } }

DelC.java:

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(\不能设置外观: \ 课号1=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(\课号:\ box1.add(课号); box2.add(new JLabel(\课名:\ box2.add(课名); box4.add(删除); box5.add(new JLabel(\课号:\ 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 JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割 add(splitV,BorderLayout.CENTER); validate(); }

public void actionPerformed(ActionEvent e){ Object obj=e.getSource();

splitV=new Statement stmt=null; ResultSet rs=null,rs1=null; String sql=null,sql1=null,sqlSC=null; if(obj==查找){if(课号1.getText().equals(\请填写查询的课号!\ else{ sql1=\课号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(\ 课名.setText(rs1.getString(\ save=课号1.getText().trim(); } else{JOptionPane.showMessageDialog(this,\没有这个课号的课程\ stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print(\Exception occur.Message is:\ } } } else{ if(obj==删除){if(save==null)JOptionPane.showMessageDialog(this,\还没查找需要修改的课程\ else{sql=\ sqlSC=\ 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(\

stmt.close(); }catch(SQLException e1){ System.out.print(\Exception occur.Message is:\ } } } } } // 连接数据库方法 public static Connection CONN(){ String driverName = \ //加载JDBC驱动 String dbURL = \DatabaseName=student\ //连接服务器和数据库test String userName = \ //默认用户名 String userPwd = \ //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println(\Successful!\ //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; } }

DelSC.java:

import java.awt.*;

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

public class DelSC extends JPanel implements ActionListener{ String saveC=null;

box1.add(new JLabel(\学号:\ box1.add(学号); box2.add(new JLabel(\姓名:\ box2.add(姓名); box3.add(new JLabel(\系别:\ box3.add(系别); box4.add(修改); box5.add(new JLabel(\学号:\ box5.add(学号1); box5.add(查找); 修改.addActionListener(this); 查找.addActionListener(this); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(Box.createVerticalGlue()); 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,sqlSC; if(obj==查找){if(学号1.getText().equals(\请填写查询的学号!\ else{ sql1=\学号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(\ 姓名.setText(rs1.getString(\ 系别.setText(rs1.getString(\ save=学号1.getText(); } else{JOptionPane.showMessageDialog(this,\没有这个学号的学生\ stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print(\ } } } else{ if(obj==修改){if(save==null){JOptionPane.showMessageDialog(this,\还没查找需要修改的学生\ else{ if(学号.getText().equals(\姓名.getText().equals(\系别.getText().equals(\ JOptionPane.showMessageDialog(this,\学生信息填满才能修改!\ } else{sql=\学号.getText()+\姓名.getText()+\系别.getText()+\ 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(\ stmt.close(); }catch(SQLException e1){ System.out.print(\ } } else{sql1=\学号.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=\学号.getText()+\ stmt.executeUpdate(sql); stmt.executeUpdate(sqlSC); save=null; JOptionPane.showMessageDialog(null,\修改完成\ 学号.setText(\ 姓名.setText(\ 系别.setText(\ stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print(\ } } }}}} }

//连接数据库方法

public static Connection CONN(){ String driverName = \ //加载JDBC驱动 String dbURL = \ //连接服务器和数据库test String userName = \ //默认用户名 String userPwd = \ //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println(\Successful!\ //如果连接成功 控制台输出Connection Successful! } catch (Exception e) {

e.printStackTrace(); } return dbConn; } }

UpdateC.java

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(\不能设置外观: \ 课号1=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(\课号:\ box1.add(课号); box2.add(new JLabel(\课名:\ box2.add(课名); box3.add(修改); box5.add(new JLabel(\课号:\ box5.add(课号1); box5.add(查找); 修改.addActionListener(this); 查找.addActionListener(this);

Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(Box.createVerticalGlue()); 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,sqlSC=null; if(obj==查找){if(课号1.getText().equals(\请填写查询的课号!\ else{ sql1=\课号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(\ 课名.setText(rs1.getString(\ save=课号1.getText(); } else{JOptionPane.showMessageDialog(this,\没有这个课号的课程\ stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print(\ }


JAVA数据库课程设计--学生选课管理系统的.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:我县竹产业调研报告

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: