登陆窗口:
package 课程管理系统;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LoginWindow extends JFrame{
//标签
private JLabel lable1;
private JLabel lable2;
//文本框
private JTextField text1;
private JTextField text2;
//按钮
private JButton bt1;
private JButton bt2;
//构造函数
public LoginWindow()
{
this.init();
this.addComponent();
this.addListener();
}
public void init()
{
this.setSize(500,400);
this.setVisible(true);
this.setTitle("登录界面");
this.setLayout(null);
this.setLocation(700, 300);
}
private void addComponent()
{
lable1 = new JLabel("用户名");
lable1.setSize(100,70);
lable1.setLocation(100,80);
this.add(lable1);
lable2 = new JLabel("密 码");
lable2.setSize(100,70);
lable2.setLocation(100,130);
this.add(lable2);
text1 = new JTextField();
text1.setSize(150,30);
text1.setLocation(160,100);
this.add(text1);
text2 = new JTextField();
text2.setSize(150,30);
text2.setLocation(160,150);
this.add(text2);
bt1 = new JButton("登录");
bt1.setSize(70,30);
bt1.setLocation(140,195);
this.add(bt1);
bt2 = new JButton("退出");
bt2.setSize(70,30);
bt2.setLocation(250,195);
this.add(bt2);
this.setBackground(Color.blue);
//设置单击关闭按钮时的默认操作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addListener()
{
bt1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(text1.getText().equals("123")&&text2.getText().equals("123"))
{
new MainWindow();
dispose();
}
else
{
JOptionPane.showMessageDialog(null, "登陆密码错误");
}
}
});
bt2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
}
});
}
public static void main(String[] args) {
new LoginWindow();
}
}
package 课程管理系统;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MainWindow extends JFrame implements ActionListener {
JButton bt1;
JButton bt2;
JButton bt3;
JButton bt4;
JPanel panel;
JPanel panel2;
JLabel label;
MainWindow(){
this.setSize(900, 700);
this.setTitle("学生课程管理系统");
this.setLayout(null);
this.setLocation(400,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bt1=new JButton("查询课程");
bt1.setSize(150, 50);
bt1.setLocation(150, 400);
bt1.addActionListener(this);
bt1.setActionCommand("查询课程");
bt2=new JButton("退选课程");
bt2.setSize(150, 50);
bt2.setLocation(150, 500);
bt2.addActionListener(this);
bt2.setActionCommand("退选课程");
bt3=new JButton("添加课程");
bt3.setSize(150, 50);
bt3.setLocation(550, 400);
bt3.addActionListener(this);
bt3.setActionCommand("添加课程");
bt4=new JButton("修改课程");
bt4.setSize(150, 50);
bt4.setLocation(550, 500);
bt4.addActionListener(this);
bt4.setActionCommand("修改课程");
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.add(bt4);
panel=new JPanel();
panel.setLocation(100, 20);
panel.setLayout(null);
panel.setBackground(Color.GRAY);
this.add(panel);
panel2=new JPanel();
panel2.setSize(650,350);
panel2.setLocation(100, 20);
panel2.setLayout(null);
panel2.setBackground(Color.lightGray);
label=new JLabel();
label.setText("欢迎登陆课程管理系统");
label.setLocation(165,60);
label.setSize(500, 200);
panel2.add(label);
label.setFont( (new Font("仿宋",Font.BOLD,30)));
this.add(panel2);
panel2.setVisible(true);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton bt=(JButton )e.getSource();
//移除上一个面板
if(bt!=null)
{
this.remove(panel2);
this.remove(panel);
}
if(bt.getText().equals("查询课程"))
{
panel=new FindCourse();
panel.setLocation(100, 20);
this.add(panel);
this.repaint();
}
else {
if(bt.getText().equals("添加课程"))
{
panel=new AddCourse();
panel.setLocation(100, 20);
this.add(panel);
this.repaint();
}
else {
if(bt.getText().equals("退选课程"))
{
panel=new DeleteCourse();
panel.setLocation(100, 20);
this.add(panel);
this.repaint();
}
else {
if(bt.getText().equals("修改课程"))
{
panel=new UpdateCourse();
panel.setLocation(100, 20);
this.add(panel);
this.repaint();
}
}
}
}
}
}
package 课程管理系统;
import java.awt.Color;
import java.sql.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class AddCourse extends JPanel implements ActionListener{
JLabel Namelabel;
JLabel Typelabel;
JLabel Teacherlabel;
JLabel Creditlabel;
JTextField Nametext;
JTextField Teachertext;
JTextField Credittext;
JComboBox Typecom;
JButton Addbt;
JScrollPane scrollpane;
JTable table;
public AddCourse() {
this.setSize(650,350);
this.setLocation(100, 20);
this.setLayout(null);
this.setBackground(Color.lightGray);
Namelabel=new JLabel("请输入课程名称");
Namelabel.setSize(100,30);
Namelabel.setLocation(60, 20);
this.add(Namelabel);
Nametext=new JTextField();
Nametext.setSize(120,30);
Nametext.setLocation(180, 20);
this.add(Nametext);
Teacherlabel=new JLabel("请输入授课教师");
Teacherlabel.setSize(100,30);
Teacherlabel.setLocation(60, 60);
this.add(Teacherlabel);
Teachertext=new JTextField();
Teachertext.setSize(120,30);
Teachertext.setLocation(180, 60);
this.add(Teachertext);
Typelabel=new JLabel("请选择课程类型");
Typelabel.setSize(100,30);
Typelabel.setLocation(60, 100);
this.add(Typelabel);
Typecom=new JComboBox();
Typecom.setSize(120,30);
Typecom.setLocation(180, 100);
Typecom.addItem("必修课");
Typecom.addItem("选修课");
this.add(Typecom);
Addbt=new JButton("添加");
Addbt.setSize(80,30);
Addbt.setLocation(350, 80);
this.add(Addbt);
Addbt.addActionListener(this);
Creditlabel=new JLabel("请输入课程学分");
Creditlabel.setSize(100,30);
Creditlabel.setLocation(60, 140);
this.add(Creditlabel);
Credittext=new JTextField();
Credittext.setSize(120,30);
Credittext.setLocation(180, 140);
this.add(Credittext);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String addName=Nametext.getText();
String addType=(String) Typecom.getSelectedItem();
String addTeacher=Teachertext.getText();
String addCredit=Credittext.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=StudentCourse";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2386180";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL="insert into dbo.Table_2 values('"+addName+"','"+addType+"','"+addTeacher+"','"+addCredit+"')";
String strSQL1="(Select* from dbo.Table_2 where CourseName='"+addName+"' )";
if(!addName.trim().equals("")&&!addTeacher.trim().equals("")&&!addCredit.trim().equals(""))
{
ResultSet rs1=st.executeQuery(strSQL1);
if(rs1.next())
{
JOptionPane.showMessageDialog(null,"该课程已存在"); }
else {
int rs=st.executeUpdate(strSQL);
if(rs==1) {
JOptionPane.showMessageDialog(null,"课程添加成功");
}
else{
JOptionPane.showMessageDialog(null,"课程添加失败");
}
}
}
else
{ JOptionPane.showMessageDialog(null,"请输入课程信息");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
}
package 课程管理系统;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
public class DeleteCourse extends JPanel implements ActionListener{
JLabel Namelabel;
JTextField Nametext;
JButton Delbt;
JTable table;
public DeleteCourse() {
this.setSize(650,350);
this.setLocation(100, 20);
this.setLayout(null);
this.setBackground(Color.lightGray);
Namelabel=new JLabel("请输入退选课程");
Namelabel.setSize(150,50);
Namelabel.setLocation(100, 280);
this.add(Namelabel);
Nametext=new JTextField();
Nametext.setSize(160,40);
Nametext.setLocation(200, 280);
this.add(Nametext);
Delbt=new JButton("确认退选");
Delbt.setSize(90,38);
Delbt.setLocation(420, 280);
this.add(Delbt);
Delbt.addActionListener(this);
Object[] columnTitle= {"课程名称","授课教师","课程类型","课程学分"};
//表格行对象数据
Object[][] tableData= {
new Object[] {"面向对象Java","必修课","张老师","3分"},
new Object[] {"面向对象C++","选修课","李老师","2分"},
new Object[] {"微机原理与接口技术","必修课","王老师","3分"},
};
//创建表格
JTable table=new JTable(tableData,columnTitle);
JScrollPane scrollpane=new JScrollPane(table);
scrollpane.setSize(550,150);
scrollpane.setLocation(60, 20);
this.add(scrollpane);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String delName=Nametext.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=StudentCourse";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2386180";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL="delete from dbo.Table_2 where CourseName='"+delName+"' ";
int rs=st.executeUpdate(strSQL);
if(rs==1) {
JOptionPane.showMessageDialog(null,"课程删除成功");
}
else{
JOptionPane.showMessageDialog(null,"课程删除失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
}
package 课程管理系统;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateCourse extends JPanel implements ActionListener {
JLabel Namelabel;
JLabel Typelabel;
JLabel Teacherlabel;
JLabel Creditlabel;
JTextField Nametext;
JTextField Teachertext;
JTextField Credittext;
JComboBox Typecom;
JButton Updatebt;
JScrollPane scrollpane;
JTable table;
public UpdateCourse() {
this.setSize(650,350);
this.setLocation(100, 20);
this.setLayout(null);
this.setBackground(Color.lightGray);
Namelabel=new JLabel("请输入课程名称");
Namelabel.setSize(100,30);
Namelabel.setLocation(60, 20);
this.add(Namelabel);
Nametext=new JTextField();
Nametext.setSize(120,30);
Nametext.setLocation(180, 20);
this.add(Nametext);
Teacherlabel=new JLabel("请输入授课教师");
Teacherlabel.setSize(100,30);
Teacherlabel.setLocation(60, 60);
this.add(Teacherlabel);
Teachertext=new JTextField();
Teachertext.setSize(120,30);
Teachertext.setLocation(180, 60);
this.add(Teachertext);
Typelabel=new JLabel("请选择课程类型");
Typelabel.setSize(100,30);
Typelabel.setLocation(60, 100);
this.add(Typelabel);
Typecom=new JComboBox();
Typecom.setSize(120,30);
Typecom.setLocation(180, 100);
Typecom.addItem("必修课");
Typecom.addItem("选修课");
this.add(Typecom);
Updatebt=new JButton("修改");
Updatebt.setSize(80,30);
Updatebt.setLocation(350, 80);
this.add( Updatebt);
Updatebt.addActionListener(this);
Creditlabel=new JLabel("请输入课程学分");
Creditlabel.setSize(100,30);
Creditlabel.setLocation(60, 140);
this.add(Creditlabel);
Credittext=new JTextField();
Credittext.setSize(120,30);
Credittext.setLocation(180, 140);
this.add(Credittext);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String updateName=Nametext.getText();
String updateType=(String) Typecom.getSelectedItem();
String updateTeacher=Teachertext.getText();
String updateCredit=Credittext.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=StudentCourse";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2386180";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL1="update dbo.Table_2 set CourseType='"+updateType+"' where CourseName='"+updateName+"'";
String strSQL2="update dbo.Table_2 set CourseTeacher='"+updateTeacher+"' where CourseName='"+updateName+"'";
String strSQL3="update dbo.Table_2 set CourseCredit='"+updateCredit+"' where CourseName='"+updateName+"'";
int rs1=st.executeUpdate(strSQL1);
int rs2=st.executeUpdate(strSQL2);
int rs3=st.executeUpdate(strSQL3);
if(rs1==1&&rs2==1&&rs3==1) {
JOptionPane.showMessageDialog(null,"课程修改成功");
}
else{
JOptionPane.showMessageDialog(null,"课程修改失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
}
package 课程管理系统;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class FindCourse extends JPanel implements ActionListener{
JLabel Inputlabel;
JTextField Inputtext;
JButton Findbt;
JLabel Namelabel;
JLabel Typelabel;
JLabel Teacherlabel;
JLabel Creditlabel;
JTextField Nametext;
JTextField Teachertext;
JTextField Credittext;
JTextField Typetext;
public FindCourse() {
this.setSize(650,350);
this.setLocation(100, 20);
this.setLayout(null);
this.setBackground(Color.lightGray);
Namelabel=new JLabel("课程名称");
Namelabel.setSize(100,30);
Namelabel.setLocation(100, 120);
this.add(Namelabel);
Nametext=new JTextField();
Nametext.setSize(120,30);
Nametext.setLocation(220, 120);
this.add(Nametext);
Teacherlabel=new JLabel("授课教师");
Teacherlabel.setSize(100,30);
Teacherlabel.setLocation(100, 160);
this.add(Teacherlabel);
Teachertext=new JTextField();
Teachertext.setSize(120,30);
Teachertext.setLocation(220, 160);
this.add(Teachertext);
Typelabel=new JLabel("课程类型");
Typelabel.setSize(100,30);
Typelabel.setLocation(100, 200);
this.add(Typelabel);
Typetext=new JTextField();
Typetext.setSize(120, 30);
Typetext.setLocation(220, 200);
this.add(Typetext);
Creditlabel=new JLabel("课程学分");
Creditlabel.setSize(100,30);
Creditlabel.setLocation(100, 240);
this.add(Creditlabel);
Credittext=new JTextField();
Credittext.setSize(120, 30);
Credittext.setLocation(220, 240);
this.add(Credittext);
Inputlabel=new JLabel("请输入课程名称");
Inputlabel.setSize(150,50);
Inputlabel.setLocation(100, 45);
this.add(Inputlabel);
Inputtext=new JTextField();
Inputtext.setSize(160,40);
Inputtext.setLocation(200, 45);
this.add(Inputtext);
Findbt=new JButton("查询");
Findbt.setSize(90,38);
Findbt.setLocation(420, 45);
this.add(Findbt);
Findbt.addActionListener(this);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String inputName=Inputtext.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=StudentCourse";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2386180";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL="(Select* from dbo.Table_2 where CourseName='"+inputName+"' )";
ResultSet rs=st.executeQuery(strSQL);
if(rs.next())
{
Nametext.setText(rs.getString(1));
Typetext.setText(rs.getString(2));
Teachertext.setText(rs.getString(3));
Credittext.setText(rs.getString(4));
}
else
{
JOptionPane.showMessageDialog(null, "您查询的课程不存在,请重新输入");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
}