选课系统 (MySQL+Java )
简介:
本文介绍博主的Java练手小项目,由MySQL管理数据库,Java控制台做输入输出,新手上路,多多包涵。
项目主要功能点:
1.数据库设计:
本选课系统因为只是练手,只做了必要的属性设计,较为简陋,设计如下:
1.1 user表(即学生表)
1.2 course表(课程表)
1.3 manage表(管理员表)
1.4 user_course表(学生课程信息表)
2.项目设计
2.1 项目结构
2.2 项目设计
学生端设计:
设计User实体类
1 package com.epoint.model; 2 3 public class User { 4 private String username; 5 private String password; 6 7 8 public String getUsername() { 9 return username; 10 } 11 public void setUsername(String username) { 12 this.username = username; 13 } 14 public String getPassword() { 15 return password; 16 } 17 public void setPassword(String password) { 18 this.password = password; 19 } 20 21 22 }
设计Course实体类
1 package com.epoint.model; 2 3 public class Course { 4 private String c_id; 5 private String c_name; 6 private String teacher; 7 8 public String getC_id() { 9 return c_id; 10 } 11 public void setC_id(String i) { 12 this.c_id = i; 13 } 14 public String getC_name() { 15 return c_name; 16 } 17 public void setC_name(String c_name) { 18 this.c_name = c_name; 19 } 20 public String getTeacher() { 21 return teacher; 22 } 23 public void setTeacher(String teacher) { 24 this.teacher = teacher; 25 } 26 27 28 }
View Code
设计UserDao接口类
1 package com.epoint.Dao; 2 3 import com.epoint.model.User; 4 5 public interface UserDao { 6 public User login(User user); 7 public void changePass(User user); 8 public void getStuCourse(String username); 9 public void getAvaCourse(String username); 10 public void chooseCourse(String username,String c_id); 11 public void deleteCourse(String username,String c_id); 12 13 }
View Code
设计UserDaoImpl类实现UserDao接口
1 package com.epoint.Dao.Impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 7 import javax.swing.plaf.synth.SynthScrollBarUI; 8 9 import com.epoint.Dao.UserDao; 10 import com.epoint.utils.*; 11 import com.epoint.model.Course; 12 import com.epoint.model.User; 13 14 public class UserDaoImpl implements UserDao{ 15 16 @Override 17 public User login(User user) { 18 // TODO Auto-generated method stub 19 Connection con = null; 20 PreparedStatement ps= null; 21 ResultSet rs=null; 22 try { 23 con=BaseDao.getCon(); 24 String sql="select * from user where username=? and password=?"; 25 ps=con.prepareStatement(sql); 26 ps.setString(1, user.getUsername()); 27 ps.setString(2, user.getPassword()); 28 rs=ps.executeQuery(); 29 User users=null; 30 if(rs.next()){ 31 users=new User(); 32 //从数据库中获取值设置到实体类的setter方法中 33 users.setUsername(rs.getString("username")); 34 users.setPassword(rs.getString("password")); 35 return users; 36 }else{ 37 return null; 38 } 39 } 40 catch (Exception e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 return null; 45 } 46 47 @Override 48 public void changePass(User user) { 49 // TODO Auto-generated method stub 50 Connection con = null; 51 PreparedStatement ps= null; 52 ResultSet rs=null; 53 try { 54 con=BaseDao.getCon(); 55 String sql = "update user set password=? where username=?"; 56 ps=con.prepareStatement(sql); 57 ps.setString(1, user.getPassword()); 58 ps.setString(2, user.getUsername()); 59 int a =ps.executeUpdate(); 60 61 } catch (Exception e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 } 66 67 @Override 68 public void chooseCourse(String username,String c_id) { 69 // TODO Auto-generated method stub 70 Connection con = null; 71 PreparedStatement ps= null; 72 ResultSet rs=null; 73 try { 74 con=BaseDao.getCon(); 75 String sql ="insert into user_course (username,c_id) values(?,?)"; 76 ps=con.prepareStatement(sql); 77 ps.setString(1, username); 78 ps.setString(2, c_id); 79 int a =ps.executeUpdate(); 80 if(a>0){ 81 System.out.println("选课成功"); 82 } 83 else{ 84 System.out.println("输入ID有误,选课失败"); 85 } 86 } catch (Exception e) { 87 // TODO Auto-generated catch block 88 e.printStackTrace(); 89 } 90 } 91 //删除选课 92 @Override 93 public void deleteCourse(String username,String c_id) { 94 // TODO Auto-generated method stub 95 96 Connection con = null; 97 PreparedStatement ps= null; 98 ResultSet rs=null; 99 try { 100 con=BaseDao.getCon(); 101 String sql="delete from user_course where username=? and c_id=? "; 102 ps=con.prepareStatement(sql); 103 ps.setString(1, username); 104 ps.setString(2, c_id); 105 int a =ps.executeUpdate(); 106 if(a>0){ 107 System.out.println("删除成功"); 108 } 109 else{ 110 System.out.println("输入ID有误,删除失败"); 111 } 112 113 } catch (Exception e) { 114 // TODO Auto-generated catch block 115 e.printStackTrace(); 116 } 117 } 118 //查询已选课程 119 @Override 120 public void getStuCourse(String username) { 121 // TODO Auto-generated method stub 122 Connection con = null; 123 PreparedStatement ps= null; 124 ResultSet rs=null; 125 try { 126 con=BaseDao.getCon(); 127 128 String sql ="select * from course where c_id in (select c_id from user_course where username=?)"; 129 ps=con.prepareStatement(sql); 130 ps.setString(1, username); 131 rs=ps.executeQuery(); 132 while (rs.next()) { 133 //System.out.println(6); 134 Course course = new Course(); 135 course.setC_id(rs.getString("c_id")); 136 course.setC_name(rs.getString("c_name")); 137 course.setTeacher(rs.getString("teacher")); 138 139 System.out.println(course.getC_id()+" "+course.getC_name()+" "+course.getTeacher()); 140 } 141 } catch (Exception e) { 142 // TODO Auto-generated catch block 143 e.printStackTrace(); 144 } 145 146 147 } 148 //查询可选课程 149 @Override 150 public void getAvaCourse(String username) { 151 // TODO Auto-generated method stub 152 Connection con = null; 153 PreparedStatement ps= null; 154 ResultSet rs=null; 155 try { 156 con=BaseDao.getCon(); 157 String sql = "select * from course where c_id not in (select c_id from user_course where username=?)"; 158 ps=con.prepareStatement(sql); 159 ps.setString(1, username); 160 rs=ps.executeQuery(); 161 while (rs.next()) { 162 Course course = new Course(); 163 course.setC_id(rs.getString("c_id")); 164 course.setC_name(rs.getString("c_name")); 165 course.setTeacher(rs.getString("teacher")); 166 167 System.out.println(course.getC_id()+" "+course.getC_name()+" "+course.getTeacher()); 168 } 169 } catch (Exception e) { 170 // TODO Auto-generated catch block 171 e.printStackTrace(); 172 } 173 } 174 175 }
View Code
设计UserService类进行业务逻辑处理
1 package com.epoint.service; 2 import java.util.Scanner; 3 4 import com.epoint.Dao.UserDao; 5 import com.epoint.Dao.Impl.UserDaoImpl; 6 import com.epoint.model.User; 7 import com.epoint.utils.StuMenu; 8 9 public class UserService { 10 static Scanner sc = new Scanner(System.in); 11 public void userLogin(String username,String password){ 12 UserDao dao = new UserDaoImpl(); 13 User user =new User(); 14 user.setUsername(username); 15 user.setPassword(password); 16 dao.login(user); 17 if(dao.login(user)!=null){ 18 System.out.println("登录成功"); 19 StuMenu.showMenu(username); 20 } 21 else{ 22 System.out.println("用户名或密码错误"); 23 } 24 25 } 26 public static void userChangePassWord(String username){ 27 UserDao dao = new UserDaoImpl(); 28 User user =new User(); 29 System.out.println("请输入新密码"); 30 String password = sc.nextLine(); 31 if(password==""||password.length()>10){ 32 System.exit(0); 33 } 34 else{ 35 user.setUsername(username); 36 user.setPassword(password); 37 dao.changePass(user); 38 System.out.println("修改密码成功,新密码为:"+password); 39 StuMenu.showMenu(username); 40 } 41 42 } 43 public static void userStuCourse(String username){ 44 UserDao dao = new UserDaoImpl(); 45 System.out.println("_________________________"); 46 System.out.println("已选课程为:"); 47 dao.getStuCourse(username); 48 StuMenu.showMenu(username); 49 } 50 public static void userDeleteCourse(String username){ 51 UserDao dao = new UserDaoImpl(); 52 System.out.println("_________________________"); 53 System.out.println("想要删除的课程ID为:"); 54 String c_id=sc.nextLine(); 55 dao.deleteCourse(username, c_id); 56 StuMenu.showMenu(username); 57 58 } 59 public static void userAvaCourse(String username){ 60 UserDao dao = new UserDaoImpl(); 61 System.out.println("_________________________"); 62 System.out.println("可选课程为:"); 63 dao.getAvaCourse(username); 64 StuMenu.showMenu(username); 65 } 66 public static void userChooserCourse(String username){ 67 UserDao dao = new UserDaoImpl(); 68 System.out.println("_________________________"); 69 System.out.println("想要选择的课程ID为:"); 70 String c_id=sc.nextLine(); 71 dao.chooseCourse(username, c_id); 72 StuMenu.showMenu(username); 73 } 74 75 }
View Code
管理员端设计
设计Manager实体类
1 package com.epoint.model; 2 3 public class Manager { 4 private String m_name; 5 private String m_password; 6 public String getM_name() { 7 return m_name; 8 } 9 public void setM_name(String m_name) { 10 this.m_name = m_name; 11 } 12 public String getM_password() { 13 return m_password; 14 } 15 public void setM_password(String m_password) { 16 this.m_password = m_password; 17 } 18 19 }
View Code
设计ManagerDao接口类
1 package com.epoint.Dao; 2 3 import com.epoint.model.Course; 4 import com.epoint.model.Manager; 5 import com.epoint.model.User; 6 7 public interface ManagerDao { 8 public Manager login(Manager manager); 9 10 public void addCourse(Course course); 11 public void deleteCourse(String c_id); 12 public void updateCourse(Course course); 13 public void selectOneCourse(String c_id); 14 public void selectAllCourse(); 15 16 public void addUser(User user); 17 public void deleteUser(String username); 18 public void updateUser(User user); 19 public void selectOneUser(String username); 20 public void selectAllUser(); 21 22 }
View Code
设计ManagerDaoImpl类实现ManagerDao接口
1 package com.epoint.Dao.Impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 7 import com.epoint.Dao.ManagerDao; 8 import com.epoint.model.Course; 9 import com.epoint.model.Manager; 10 import com.epoint.model.User; 11 import com.epoint.utils.BaseDao; 12 13 public class ManagerDaoImpl implements ManagerDao{ 14 //管理员登录 15 @Override 16 public Manager login(Manager manager) { 17 // TODO Auto-generated method stub 18 Connection con = null; 19 PreparedStatement ps= null; 20 ResultSet rs=null; 21 try { 22 con=BaseDao.getCon(); 23 String sql="select * from manager where m_name=? and m_password=?"; 24 ps=con.prepareStatement(sql); 25 ps.setString(1, manager.getM_name()); 26 ps.setString(2, manager.getM_password()); 27 rs=ps.executeQuery(); 28 Manager managers=null; 29 if(rs.next()){ 30 managers=new Manager(); 31 managers.setM_name(rs.getString("m_name")); 32 managers.setM_password(rs.getString("m_password")); 33 return managers; 34 35 } 36 else 37 return null; 38 39 } catch (Exception e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 44 return null; 45 } 46 //添加课程 47 @Override 48 public void addCourse(Course course) { 49 // TODO Auto-generated method stub 50 Connection con = null; 51 PreparedStatement ps= null; 52 ResultSet rs=null; 53 try { 54 con=BaseDao.getCon(); 55 String sql="insert into course (c_id,c_name,teacher) values(?,?,?)"; 56 ps=con.prepareStatement(sql); 57 ps.setString(1, course.getC_id()); 58 ps.setString(2, course.getC_name()); 59 ps.setString(3, course.getTeacher()); 60 int a = ps.executeUpdate(); 61 } catch (Exception e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 66 } 67 //删除课程 68 @Override 69 public void deleteCourse(String c_id) { 70 // TODO Auto-generated method stub 71 Connection con = null; 72 PreparedStatement ps= null; 73 ResultSet rs=null; 74 try { 75 con=BaseDao.getCon(); 76 String sql = "delete from course where c_id=?"; 77 ps=con.prepareStatement(sql); 78 ps.setString(1, c_id); 79 int a=ps.executeUpdate(); 80 if(a>0){ 81 System.out.println("删除成功"); 82 String sql2="delete from user_course where c_id=?"; 83 ps=con.prepareStatement(sql2); 84 ps.setString(1, c_id); 85 int b=ps.executeUpdate(); 86 } 87 else{ 88 System.out.println("输入ID有误,删除失败"); 89 } 90 }catch (Exception e) { 91 // TODO Auto-generated catch block 92 e.printStackTrace(); 93 } 94 95 } 96 //修改课程 97 @Override 98 public void updateCourse(Course course) { 99 // TODO Auto-generated method stub 100 Connection con = null; 101 PreparedStatement ps= null; 102 ResultSet rs=null; 103 try { 104 con=BaseDao.getCon(); 105 String sql="update course set c_name=?,teacher=? where c_id=?"; 106 ps=con.prepareStatement(sql); 107 ps.setString(1, course.getC_name()); 108 ps.setString(2, course.getTeacher()); 109 ps.setString(3, course.getC_id()); 110 int a = ps.executeUpdate(); 111 if(a>0){ 112 System.out.println("课程修改成功"); 113 } 114 else{ 115 System.out.println("输入ID有误,课程修改失败"); 116 } 117 118 } catch (Exception e) { 119 // TODO Auto-generated catch block 120 e.printStackTrace(); 121 } 122 123 } 124 // 查看某一课程 125 @Override 126 public void selectOneCourse(String c_id) { 127 // TODO Auto-generated method stub 128 Connection con = null; 129 PreparedStatement ps= null; 130 ResultSet rs=null; 131 try { 132 con=BaseDao.getCon(); 133 String sql ="select * from course where c_id=?"; 134 ps=con.prepareStatement(sql); 135 ps.setString(1, c_id); 136 rs=ps.executeQuery(); 137 Course course=null; 138 if(rs.next()){ 139 course = new Course(); 140 course.setC_id(rs.getString("c_id")); 141 course.setC_name(rs.getString("c_name")); 142 course.setTeacher(rs.getString("teacher")); 143 System.out.println(course.getC_id()+" "+course.getC_name()+" "+course.getTeacher()); 144 } 145 } catch (Exception e) { 146 // TODO Auto-generated catch block 147 e.printStackTrace(); 148 } 149 } 150 //查看全部课程 151 @Override 152 public void selectAllCourse() { 153 // TODO Auto-generated method stub 154 Connection con = null; 155 PreparedStatement ps= null; 156 ResultSet rs=null; 157 try { 158 con=BaseDao.getCon(); 159 String sql ="select * from course "; 160 ps=con.prepareStatement(sql); 161 rs=ps.executeQuery(); 162 Course course=null; 163 while(rs.next()){ 164 course = new Course(); 165 course.setC_id(rs.getString("c_id")); 166 course.setC_name(rs.getString("c_name")); 167 course.setTeacher(rs.getString("teacher")); 168 System.out.println(course.getC_id()+" "+course.getC_name()+" "+course.getTeacher()); 169 } 170 } catch (Exception e) { 171 // TODO Auto-generated catch block 172 e.printStackTrace(); 173 } 174 175 } 176 //添加学生 177 @Override 178 public void addUser(User user) { 179 // TODO Auto-generated method stub 180 Connection con = null; 181 PreparedStatement ps= null; 182 ResultSet rs=null; 183 try { 184 con=BaseDao.getCon(); 185 String sql="insert into user (username,password) values(?,?)"; 186 ps=con.prepareStatement(sql); 187 ps.setString(1, user.getUsername()); 188 ps.setString(2, user.getPassword()); 189 int a = ps.executeUpdate(); 190 if(a>0){ 191 System.out.println("学生添加成功"); 192 } 193 else{ 194 System.out.println("学生添加失败"); 195 } 196 } catch (Exception e) { 197 // TODO Auto-generated catch block 198 e.printStackTrace(); 199 } 200 } 201 //删除学生 202 @Override 203 public void deleteUser(String username) { 204 // TODO Auto-generated method stub 205 Connection con = null; 206 PreparedStatement ps= null; 207 ResultSet rs=null; 208 try { 209 con=BaseDao.getCon(); 210 String sql = "delete from user where username=?"; 211 ps=con.prepareStatement(sql); 212 ps.setString(1, username); 213 int a=ps.executeUpdate(); 214 if(a>0){ 215 System.out.println("删除成功"); 216 String sql2="delete from user_course where username=?"; 217 ps=con.prepareStatement(sql2); 218 ps.setString(1, username); 219 int b=ps.executeUpdate(); 220 } 221 else{ 222 System.out.println("输入用户名有误,删除失败"); 223 } 224 }catch (Exception e) { 225 // TODO Auto-generated catch block 226 e.printStackTrace(); 227 } 228 } 229 //修改用户 230 @Override 231 public void updateUser(User user) { 232 // TODO Auto-generated method stub 233 Connection con = null; 234 PreparedStatement ps= null; 235 ResultSet rs=null; 236 try { 237 con=BaseDao.getCon(); 238 String sql="update user set password=? where username=?"; 239 ps=con.prepareStatement(sql); 240 ps.setString(1, user.getPassword()); 241 ps.setString(2, user.getUsername()); 242 int a = ps.executeUpdate(); 243 if(a>0){ 244 System.out.println("用户修改成功"); 245 } 246 else{ 247 System.out.println("输入有误,用户修改失败"); 248 } 249 250 } catch (Exception e) { 251 // TODO Auto-generated catch block 252 e.printStackTrace(); 253 } 254 } 255 //查看某一学生 256 @Override 257 public void selectOneUser(String username) { 258 // TODO Auto-generated method stub 259 Connection con = null; 260 PreparedStatement ps= null; 261 ResultSet rs=null; 262 try { 263 con=BaseDao.getCon(); 264 String sql ="select * from user where username=?"; 265 ps=con.prepareStatement(sql); 266 ps.setString(1, username); 267 rs=ps.executeQuery(); 268 User user=null; 269 if(rs.next()){ 270 user = new User(); 271 user.setUsername(rs.getString("username")); 272 user.setPassword(rs.getString("password")); 273 System.out.println(user.getUsername()+" "+user.getPassword()); 274 } 275 } catch (Exception e) { 276 // TODO Auto-generated catch block 277 e.printStackTrace(); 278 } 279 } 280 //查看全部课程 281 @Override 282 public void selectAllUser() { 283 // TODO Auto-generated method stub 284 Connection con = null; 285 PreparedStatement ps= null; 286 ResultSet rs=null; 287 try { 288 con=BaseDao.getCon(); 289 String sql ="select * from user "; 290 ps=con.prepareStatement(sql); 291 rs=ps.executeQuery(); 292 User user=null; 293 while(rs.next()){ 294 user = new User(); 295 user.setUsername(rs.getString("username")); 296 user.setPassword(rs.getString("password")); 297 System.out.println(user.getUsername()+" "+user.getPassword()); 298 } 299 } catch (Exception e) { 300 // TODO Auto-generated catch block 301 e.printStackTrace(); 302 } 303 } 304 305 }
View Code
设计ManagerService类进行业务逻辑处理
1 package com.epoint.service; 2 3 import java.util.Scanner; 4 5 import com.epoint.Dao.ManagerDao; 6 import com.epoint.Dao.Impl.ManagerDaoImpl; 7 import com.epoint.model.Course; 8 import com.epoint.model.Manager; 9 import com.epoint.model.User; 10 import com.epoint.utils.ManMenu; 11 import com.epoint.utils.StuMenu; 12 13 public class ManagerService { 14 static Scanner sc = new Scanner(System.in); 15 //管理员登录 16 public void manLogin(String username,String password){ 17 ManagerDao dao = new ManagerDaoImpl(); 18 Manager manager = new Manager(); 19 manager.setM_name(username); 20 manager.setM_password(password); 21 dao.login(manager); 22 if(dao.login(manager)!=null){ 23 System.out.println("登录成功"); 24 ManMenu.ShowMenu(); 25 26 } 27 else{ 28 System.out.println("用户名或密码错误"); 29 } 30 } 31 //添加课程 32 public static void manAddCourse(){ 33 ManagerDao dao = new ManagerDaoImpl(); 34 Course course=new Course(); 35 System.out.println("请输入要填加的课程ID:"); 36 course.setC_id(sc.nextLine()); 37 System.out.println("请输入要填加的课程名:"); 38 course.setC_name(sc.nextLine()); 39 System.out.println("请输入要填加的课程教师:"); 40 course.setTeacher(sc.nextLine()); 41 dao.addCourse(course); 42 ManMenu.ShowMenu(); 43 } 44 //删除课程 45 public static void manDeleteCourse(){ 46 ManagerDao dao = new ManagerDaoImpl(); 47 System.out.println("请输入要删除的课程ID:"); 48 String c_id = sc.nextLine(); 49 dao.deleteCourse(c_id); 50 ManMenu.ShowMenu(); 51 } 52 //修改课程 53 public static void manUpdateCourse(){ 54 ManagerDao dao = new ManagerDaoImpl(); 55 Course course=new Course(); 56 System.out.println("请输入要修改的课程ID:"); 57 course.setC_id(sc.nextLine()); 58 System.out.println("请输入修改后的课程名:"); 59 course.setC_name(sc.nextLine()); 60 System.out.println("请输入修改后的课程教师:"); 61 course.setTeacher(sc.nextLine()); 62 dao.updateCourse(course); 63 ManMenu.ShowMenu(); 64 } 65 //查看某一课程 66 public static void manOneCourse(){ 67 ManagerDao dao = new ManagerDaoImpl(); 68 System.out.println("请输入要查看的课程ID:"); 69 String c_id=sc.nextLine(); 70 dao.selectOneCourse(c_id); 71 ManMenu.ShowMenu(); 72 } 73 //查看全部课程 74 public static void manAllCourse(){ 75 ManagerDao dao = new ManagerDaoImpl(); 76 System.out.println("已有课程如下:"); 77 dao.selectAllCourse(); 78 ManMenu.ShowMenu(); 79 } 80 //添加学生 81 public static void manAddUser(){ 82 ManagerDao dao = new ManagerDaoImpl(); 83 User user = new User(); 84 System.out.println("请输入要填加的学生用户名:"); 85 user.setUsername(sc.nextLine()); 86 System.out.println("请输入密码:"); 87 user.setPassword(sc.nextLine()); 88 dao.addUser(user); 89 ManMenu.ShowMenu(); 90 } 91 //删除学生 92 public static void manDeleteUser(){ 93 ManagerDao dao = new ManagerDaoImpl(); 94 System.out.println("请输入要删除的用户名:"); 95 String username = sc.nextLine(); 96 dao.deleteUser(username); 97 ManMenu.ShowMenu(); 98 } 99 //修改学生 100 public static void manUpdateUser(){ 101 ManagerDao dao = new ManagerDaoImpl(); 102 User user= new User(); 103 System.out.println("请输入要修改的用户名:"); 104 user.setUsername(sc.nextLine()); 105 System.out.println("请输入修改后的密码:"); 106 user.setPassword(sc.nextLine()); 107 dao.updateUser(user); 108 ManMenu.ShowMenu(); 109 } 110 //查看某一学生 111 public static void manOneUser(){ 112 ManagerDao dao = new ManagerDaoImpl(); 113 System.out.println("请输入要查看的用户名:"); 114 String username=sc.nextLine(); 115 dao.selectOneUser(username); 116 ManMenu.ShowMenu(); 117 } 118 //查看全部学生 119 public static void manAllUser(){ 120 ManagerDao dao = new ManagerDaoImpl(); 121 System.out.println("已有学生信息如下:"); 122 dao.selectAllUser(); 123 ManMenu.ShowMenu(); 124 } 125 126 }
View Code
Utils包设计 (包含一些菜单类以及公共的数据库连接类)
设计BaseDao类做为公共的数据库连接类
1 package com.epoint.utils; 2 3 import java.sql.*; 4 5 public class BaseDao { 6 7 private static String driver="com.mysql.jdbc.Driver"; 8 private static String url="jdbc:mysql://localhost:3306/select_course"; 9 private static String user="root"; 10 private static String password="Gepoint"; 11 12 /** 13 * 连接数据库 14 * @throws Exception 15 */ 16 public static Connection getCon () throws Exception{ 17 Class.forName(driver); 18 //System.out.println("加载"); 19 Connection con =DriverManager.getConnection(url, user, password); 20 //System.out.println("连接"); 21 return con; 22 } 23 /** 24 * 关闭数据库 25 */ 26 public static void close(Connection con,PreparedStatement ps,ResultSet rs){ 27 if(rs!=null){//关闭资源,避免出现异常 28 try { 29 rs.close(); 30 } catch (SQLException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 } 35 if(ps!=null){//关闭资源,避免出现异常 36 try { 37 ps.close(); 38 } catch (SQLException e) { 39 // TODO Auto-generated catch block 40 e.printStackTrace(); 41 } 42 } 43 if(con!=null){//关闭资源,避免出现异常 44 try { 45 con.close(); 46 } catch (SQLException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 } 51 52 } 53 // public static void main(String[] args) { 54 // try { 55 // BaseDao.getCon(); 56 // System.out.println("成功"); 57 // } catch (Exception e) { 58 // // TODO Auto-generated catch block 59 // e.printStackTrace(); 60 // } 61 // 62 // } 63 64 }
View Code
菜单类设计
1 package com.epoint.utils; 2 3 import java.util.Scanner; 4 import com.epoint.service.*; 5 /** 6 * 学生界面 7 * @author zhai 8 * 9 */ 10 public class StuMenu { 11 public static void showMenu(String username){ 12 Scanner sc = new Scanner(System.in); 13 System.out.println("_________________________"); 14 System.out.println(" 欢迎"+username+"使用选课系统 "); 15 System.out.println(" 1.修改学生密码"); 16 System.out.println(" 2.查看已选课程"); 17 System.out.println(" 3.选择可选课程"); 18 System.out.println(" 4.查看可选课程"); 19 System.out.println(" 5.删除已选课程"); 20 System.out.println(" 6.退出选课系统"); 21 System.out.println("_________________________"); 22 int a = sc.nextInt(); 23 switch(a){ 24 case 1: UserService.userChangePassWord(username); 25 break; 26 case 2: UserService.userStuCourse(username); 27 break; 28 case 3: UserService.userChooserCourse(username); 29 break; 30 case 4: UserService.userAvaCourse(username); 31 break; 32 case 5: UserService.userDeleteCourse(username); 33 break; 34 case 6: System.out.println("用户成功退出!"); 35 System.exit(0); 36 break; 37 default: 38 System.out.println("输入数字不合法,程序退出"); 39 System.exit(0); 40 } 41 42 43 } 44 45 }
View Code
1 package com.epoint.utils; 2 3 import java.util.Scanner; 4 /** 5 * 管理员界面 6 * @author zhai 7 * 8 */ 9 public class ManMenu { 10 public static void ShowMenu(){ 11 Scanner sc = new Scanner(System.in); 12 System.out.println("_________________________"); 13 System.out.println(" 欢迎使用选课系统 "); 14 System.out.println(" 1.课程管理"); 15 System.out.println(" 2.学生管理"); 16 System.out.println(" 3.退出系统"); 17 System.out.println("_________________________"); 18 int a =sc.nextInt(); 19 switch(a){ 20 case 1: 21 CourseControlMenu.ShowMenu(); 22 break; 23 case 2: 24 UserControlMenu.ShowMenu(); 25 break; 26 case 3: 27 System.out.println("用户成功退出!"); 28 System.exit(0); 29 break; 30 default: 31 System.out.println("输入数字不合法,程序退出"); 32 System.exit(0); 33 } 34 } 35 36 37 }
View Code
1 package com.epoint.utils; 2 3 import java.util.Scanner; 4 /** 5 * 课程管理界面 6 */ 7 import com.epoint.service.ManagerService; 8 public class CourseControlMenu { 9 public static void ShowMenu(){ 10 Scanner sc = new Scanner(System.in); 11 System.out.println("_________________________"); 12 System.out.println(" 1.课程新增"); 13 System.out.println(" 2.课程查看"); 14 System.out.println(" 3.课程删除"); 15 System.out.println(" 4.课程修改"); 16 System.out.println("_________________________"); 17 int a =sc.nextInt(); 18 switch(a){ 19 case 1: 20 ManagerService.manAddCourse(); 21 break; 22 case 2: 23 CourseLookMenu.ShowMenu(); 24 break; 25 case 3: 26 ManagerService.manDeleteCourse(); 27 break; 28 case 4: 29 ManagerService.manUpdateCourse(); 30 break; 31 default: 32 System.out.println("输入数字不合法,程序退出"); 33 System.exit(0); 34 35 } 36 } 37 38 }
View Code
1 package com.epoint.utils; 2 /** 3 * 课程查看界面 4 */ 5 import java.util.Scanner; 6 import com.epoint.service.ManagerService; 7 public class CourseLookMenu { 8 9 public static void ShowMenu(){ 10 Scanner sc = new Scanner(System.in); 11 System.out.println("_________________________"); 12 System.out.println(" 1.查看某一课程"); 13 System.out.println(" 2.查看全部课程"); 14 System.out.println("_________________________"); 15 int a =sc.nextInt(); 16 switch(a){ 17 case 1: 18 ManagerService.manOneCourse(); 19 break; 20 case 2: 21 ManagerService.manAllCourse(); 22 default: 23 System.out.println("输入数字不合法,程序退出"); 24 System.exit(0); 25 } 26 } 27 28 }
View Code
1 package com.epoint.utils; 2 3 import java.util.Scanner; 4 5 import com.epoint.service.ManagerService; 6 /** 7 * 学生管理界面 8 * @author zhai 9 * 10 */ 11 public class UserControlMenu { 12 public static void ShowMenu(){ 13 Scanner sc = new Scanner(System.in); 14 System.out.println("_________________________"); 15 System.out.println(" 1.学生新增"); 16 System.out.println(" 2.学生查看"); 17 System.out.println(" 3.学生删除"); 18 System.out.println(" 4.学生修改"); 19 System.out.println("_________________________"); 20 int a =sc.nextInt(); 21 switch(a){ 22 case 1: 23 ManagerService.manAddUser(); 24 break; 25 case 2: 26 UserLookMenu.ShowMenu(); 27 break; 28 case 3: 29 ManagerService.manDeleteUser(); 30 break; 31 case 4: 32 ManagerService.manUpdateUser(); 33 break; 34 default: 35 System.out.println("输入数字不合法,程序退出"); 36 System.exit(0); 37 } 38 } 39 40 }
View Code
1 package com.epoint.utils; 2 3 import java.util.Scanner; 4 5 import com.epoint.service.ManagerService; 6 /** 7 * 学生信息查看界面 8 * @author zhai 9 * 10 */ 11 public class UserLookMenu { 12 public static void ShowMenu(){ 13 Scanner sc = new Scanner(System.in); 14 System.out.println("_________________________"); 15 System.out.println(" 1.查看某一学生"); 16 System.out.println(" 2.查看全部学生"); 17 System.out.println("_________________________"); 18 int a =sc.nextInt(); 19 switch(a){ 20 case 1: 21 ManagerService.manOneUser(); 22 break; 23 case 2: 24 ManagerService.manAllUser(); 25 break; 26 default: 27 System.out.println("输入数字不合法,程序退出"); 28 System.exit(0); 29 } 30 } 31 }
View Code
测试类设计
1 package com.epoint.utils; 2 3 import com.epoint.service.ManagerService; 4 import com.epoint.service.UserService; 5 import java.util.Scanner; 6 7 public class Test { 8 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 Scanner sc = new Scanner(System.in); 12 System.out.println("欢迎使用选课系统"); 13 System.out.println("请输入登录名:"); 14 String username = sc.nextLine(); 15 System.out.println("请输入密码"); 16 String password = sc.nextLine(); 17 System.out.println("请选择您的身份"); 18 System.out.println("1.管理员"); 19 System.out.println("2.学生"); 20 int a = sc.nextInt(); 21 switch(a){ 22 case 1: 23 new ManagerService().manLogin(username, password); 24 break; 25 case 2: 26 new UserService().userLogin(username, password); 27 break; 28 default: 29 System.out.println("输入数字不合法,程序退出"); 30 System.exit(0); 31 } 32 } 33 34 }
View Code