1、/*JDBC连接数据库*/package immoc4.bao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class JDBC private static final String URL = jdbc:mysql:/localhost:3306/a?characterEncoding=utf8;private static final Strin
2、g USER = root;private static final String PASSWORD = 995923;private static Connection conn = null;static try Class.forName(com.mysql.jdbc.Driver);conn = DriverManager.getConnection(URL, USER, PASSWORD); catch (ClassNotFoundException e) e.printStackTrace(); catch (SQLException e) e.printStackTrace();
3、public static Connection getConnection() return conn;/*登录界面*/package immoc4.bao;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.SQLException;import java.util.Scanner;import javax.swing.JButton;import javax.swing.JFrame;import javax.sw
4、ing.JLabel;import javax.swing.JTextField;public class View extends JFramestatic GongNeng gn = new GongNeng();static Student s = new Student();static JTextField text1;static JTextField text2;static JButton button; public void denglu()setLayout(new FlowLayout();add(new JLabel(用户id:);text1 = new JTextF
5、ield(20);add(text1);add(new JLabel(密码:);text2 = new JTextField(20);add(text2);button = new JButton(登录);add(button); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /关闭窗口 public static void main(String args)View dl = new View();dl.setBounds(300, 300, 300, 200);dl.setTitle(学生选课管理系统);
6、dl.denglu(); button.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) / TODO Auto-generated method stub dl.dispose(); s.setStudent_id(Integer.parseInt(text1.getText(); s.setStudent_code(text2.getText(); try gn.correspond(s); catch (SQLException e1) / TODO Auto-generat
7、ed catch blocke1.printStackTrace(); ); /*主界面*/package immoc4.bao;import javax.swing.*;import immoc4.bao.Course;import java.awt.*;import java.awt.event.*;import java.sql.SQLException;public class Card extends JFrame /* * JFrame的布局管理器是BorderLayout */Course g = new Course();GongNeng gn = new GongNeng()
8、; JPanel p;/位于中心区域的面板 JButton b1,b2,b3,b4;/位于北部区域的四个按钮 JButton add; JButton delete; JButton update; JButton query; JLabel add_id,add_name,add_teacher_name; JLabel delete_id; JLabel update_id,update_name,update_teacher_name; JLabel query_id; JLabel query_show; JTextField text_add_id,text_add_name,tex
9、t_add_teacher_name; JTextField text_delete_id; JTextField text_update_id,text_update_name,text_update_teacher_name; JTextField text_query_id; JTextArea area_query_show; CardLayout c;/设置面板p的布局 Card() super(); this.setVisible(true); this.setBounds(400,300,400,300); p = new JPanel();/实例化p c = new CardL
10、ayout();/实例化c p.setLayout(c);/设置面板p的布局为c,等价于p = new JPanel(c); /实例化4个按钮和标签 b1 = new JButton(添加课程); b2 = new JButton(删除课程); b3 = new JButton(修改课程); b4 = new JButton(查询课程); /实例化4个子面板并设定相应背景颜色 JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JPanel p4 = new JPanel(); / p1.s
11、etLayout( p1.setBackground(Color.green); p2.setBackground(Color.pink); p3.setBackground(Color.orange); p4.setBackground(Color.lightGray); /把上面4个面板添加到中心面板p中,并把4个面板分别用编号为1,2,3,4代替 p.add(p1,1); p.add(p2,2); p.add(p3,3); p.add(p4,4); /实例化位于南部区域的面板,并添加4个按钮 JPanel pnorth = new JPanel(); pnorth.add(b1); pn
12、orth.add(b2); pnorth.add(b3); pnorth.add(b4); /把p,psourth分别添加到本JFrame的中心区域和南部区域 this.add(p);/等价于this.add(p,BorderLayout.CENTER); 因为默认是把组件添加到中部 this.add(pnorth,BorderLayout.NORTH); / 添加课程 add_id = new JLabel(课程号); text_add_id = new JTextField(30); add_name = new JLabel(课程名); text_add_name = new JText
13、Field(30); add_teacher_name = new JLabel(教师名); text_add_teacher_name = new JTextField(30); add = new JButton(添加); /为按钮b1添加监听器,当被按下时显示面板p中的前一个子面板 b1.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) / TODO Auto-generated method stub c.show(p,1);/显示p中的上一个面板 p1.
14、add(add_id); p1.add(text_add_id); p1.add(add_name); p1.add(text_add_name); p1.add(add_teacher_name); p1.add(text_add_teacher_name); p1.add(add); setVisible(true); ); add.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubg.setId(
15、Integer.parseInt(text_add_id.getText();g.setName(text_add_name.getText();g.setTeacher_name(text_add_teacher_name.getText();try gn.addcourse(g); catch (Exception e1) / TODO Auto-generated catch blocke1.printStackTrace(); ); /删除课程 delete_id = new JLabel(课程号); text_delete_id = new JTextField(20); delet
16、e = new JButton(删除); /为按钮b2添加监听器,当被按下时显示面板p中的第2个子面板 b2.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) / TODO Auto-generated method stub c.show(p, 2);/显示p中代号为2的面板 p2.add(delete_id); p2.add(text_delete_id) ; p2.add(delete); setVisible(true); ); delete.addAct
17、ionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stub int id = Integer.parseInt(text_delete_id.getText();try gn.delCourse(id); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();); / 修改课程 update_id = new JLabel
18、(课程号); text_update_id = new JTextField(30); update_name = new JLabel(课程名); text_update_name = new JTextField(30); update_teacher_name = new JLabel(教师名); text_update_teacher_name = new JTextField(30); update = new JButton(修改); /以下类推 b3.addActionListener(new ActionListener() Override public void actio
19、nPerformed(ActionEvent e) / TODO Auto-generated method stub c.show(p, 3); p3.add(update_id); p3.add(text_update_id); p3.add(update_name); p3.add(text_update_name); p3.add(update_teacher_name); p3.add(text_update_teacher_name); p3.add(update); setVisible(true); ); update.addActionListener(new ActionL
20、istener() Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubint id = Integer.parseInt(text_update_id.getText();g.setId(Integer.parseInt(text_update_id.getText();g.setName(text_update_name.getText();g.setTeacher_name(text_update_teacher_name.getText();try gn.updateco
21、urse(g); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();); /查询课程 query_id = new JLabel(课程号); text_query_id = new JTextField(20); query = new JButton(查询); b4.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) / TODO Auto-generated
22、 method stub c.show(p,4); p4.add(query_id); p4.add(text_query_id); p4.add(query); setVisible(true); ); query.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubint id = Integer.parseInt(text_query_id.getText();try gn.get(id); cat
23、ch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace(););/*底层增删改查功能*/package immoc4.bao;import java.awt.FlowLayout;import java.io.ByteArrayInputStream;import java.sql.Connection;import java.sql.Date;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLEx
24、ception;import java.util.ArrayList;import java.util.List;import java.util.Map;import javax.swing.JLabel;import javax.swing.JTextField;public class GongNeng Cue cue = new Cue();/添加课程功能public void addcourse(Course g) throws Exception List result = new ArrayList();Connection conn = JDBC.getConnection()
25、;StringBuilder sb = new StringBuilder();sb.append(select * from student_course );PreparedStatement ptmt = conn.prepareStatement(sb.toString();ResultSet rs = ptmt.executeQuery();/ ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成,用while循环来表示结果集int x = 1;while (rs.next() if(g.getId() = rs.getInt(id) /当输入的课程号不存在的
26、异常处理 x = 0;if(x=1)ptmt = conn.prepareStatement(insert into student_course(id,name,teacher_name) values(?,?,?);/ SPreparedStatement是SQL语句被预编译并存储在 PreparedStatement/ 对象中。然后可以使用此对象多次高效地执行该语句ptmt.setInt(1, g.getId();ptmt.setString(2,g.getName();ptmt.setString(3, g.getTeacher_name();ptmt.execute();elsecu
27、e.denglu(对不起,你输入的课程号已存在,请重新输入);/修改课程功能public void updatecourse(Course g) throws SQLException List result = new ArrayList();Connection conn = JDBC.getConnection();StringBuilder sb = new StringBuilder();sb.append(select * from student_course );PreparedStatement ptmt = conn.prepareStatement(sb.toString
28、();ResultSet rs = ptmt.executeQuery();/ ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成/ 用while循环来表示结果集int x = 0;while (rs.next() if(g.getId() = rs.getInt(id) /当输入的课程号不存在的异常处理 x = 1;if(x=1)ptmt = conn.prepareStatement(update student_course set name = ?,teacher_name = ? where id = ?);ptmt.setString(1, g.getNa
29、me();ptmt.setString(2, g.getTeacher_name();ptmt.setInt(3, g.getId();ptmt.execute();elsecue.denglu(对不起,你输入的课程号不存在,请重新输入);/删除课程功能public void delCourse(int id) throws SQLException List result = new ArrayList();Connection conn = JDBC.getConnection();StringBuilder sb = new StringBuilder();sb.append(selec
30、t * from student_course );PreparedStatement ptmt = conn.prepareStatement(sb.toString();ResultSet rs = ptmt.executeQuery();/ ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成/ 用while循环来表示结果集int x = 0;while (rs.next() if(id = rs.getInt(id) /当输入的课程号不存在的异常处理 x = 1;if(x=1) ptmt = conn.prepareStatement( delete from
31、student_course + where id=?);ptmt.setInt(1, id);ptmt.execute();elsecue.denglu(你输入的课程号不存在,请重新输入);查询课程功能public void get(Integer id) throws SQLException List result = new ArrayList();Connection conn = JDBC.getConnection();StringBuilder sb = new StringBuilder();sb.append(select * from student_course );P
32、reparedStatement ptmt = conn.prepareStatement(sb.toString();ResultSet rs = ptmt.executeQuery();/ ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成/ 用while循环来表示结果集int x = 0;while (rs.next() if(id = rs.getInt(id) /当输入的课程号不存在的异常处理 x = 1;if(x=1)Course g = null;Connection conn1 = JDBC.getConnection();String sql = +
33、 select * from student_course + where id=? ;PreparedStatement ptmt1 = conn.prepareStatement(sql);ptmt1.setInt(1, id);ResultSet rs1 = ptmt.executeQuery();while (rs1.next() g = new Course();g.setId(rs1.getInt(id);g.setName(rs1.getString(name);g.setTeacher_name(rs1.getString(teacher_name);System.out.pr
34、intln(id = + g.getId() + ,name = + g.getName() + ,teacher_name = + g.getTeacher_name() + );else cue.denglu(对不去,你输入的课程号不存在,请重新输入);/匹配登录名与密码是否符合用户 public void correspond(Student s) throws SQLException List result = new ArrayList();Connection conn = JDBC.getConnection();StringBuilder sb = new StringBui
35、lder();sb.append(select * from student );PreparedStatement ptmt = conn.prepareStatement(sb.toString();ResultSet rs = ptmt.executeQuery();/ ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成/ 用while循环来表示结果集int x = 0;while (rs.next() if(s.getStudent_id() = rs.getInt(student_id)&s.getStudent_code().equals(rs.getSt
36、ring(student_code) x = 1;break;if(s.getStudent_id() = rs.getInt(student_id)if(s.getStudent_code().equals(rs.getString(student_code)=false)x = 3; break;if(s.getStudent_id() != rs.getInt(student_id)x = 2;if(x = 1) Card cd = new Card();if(x = 2)cue.denglu(用户不存在,请重新输入);if(x = 3)cue.denglu(您输入的密码有误,请重新输入); /*定义课程类*/package immoc4.bao;public class Course private int id;private String name;private String teacher_name;public int getId() return id;public void setId(int id) this.id = id;public String getName() return name;public void