导言
闲着没事写的一个 影院管理系统,连接的有数据库,在控制台运行,相对简陋,可以拿去当课程设计或者用来学习java用。
文章末尾附有源代码!
一、程序结构
二、运行效果
三、代码展示
1.数据库部分
在Dao层里面实现数据处理问题。
通过JDBC连接数据库:
先 Connection 进行数据库连接,
再 PreparedStatement 执行sql语句,
最后 ResultSet 查看sql语句执行结果。
下面展示一下对电影的增删改查的代码。
// 添加 电影
public void addMovie (String name,String actor,String type,String time,String home) throws SQLException{
QueryRunner runner = new QueryRunner(); // 完成数据库的增删改查
Connection conn = DBHelper.getConnection();
String sql="insert into movie (name,actor,type,time,home) values (?,?,?,?,?); ";
int count = runner.update(conn,sql,name,actor,type,time,home);
DBHelper.close(conn);
}
// 删除 电影
public void delMovie (String name) throws SQLException{
QueryRunner runner = new QueryRunner(); // 完成数据库的增删改查
Connection conn = DBHelper.getConnection();
String sql="delete from movie where name = ?";
int count = runner.update(conn,sql,name);
DBHelper.close(conn);
}
// 查找 影片 。
public ArrayList<Movie> findoneMovie(String name) throws SQLException {
Connection con = DBHelper.getConnection();
ArrayList<Movie> findmovies = new ArrayList<>();
String sql = "select * from movie where name = ?";
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1,name);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
findmovies.add(new Movie(resultSet.getString(1),resultSet.getString(2),resultSet.getString(3),resultSet.getString(4),resultSet.getString(5)));
}
return findmovies;
}
// 修改 电影
public void modMovie (String name,String actor,String type,String time,String home) throws SQLException{
QueryRunner runner = new QueryRunner(); // 完成数据库的增删改查
Connection conn = DBHelper.getConnection();
String sql="update movie set actor=?,type=?,time=?,home=? where name=?";
int count = runner.update(conn,sql,actor,type,time,home,name);
DBHelper.close(conn);
}
2.主页面部分
// 查看影片排挡信息
public static void manage_one() throws SQLException {
System.out.println("----影片排档信息----");
MovieDao movieDao = new MovieDao();
System.out.println("电影总数: "+movieDao.findMovie().size());
System.out.println("电影名 电影主演 电影类型 上映时间 播放厅");
for (int i=0;i<movieDao.findMovie().size();i++){
System.out.print(movieDao.findMovie().get(i).getName()+" ");
System.out.print(movieDao.findMovie().get(i).getActor()+" ");
System.out.print(movieDao.findMovie().get(i).getType()+" ");
System.out.print(movieDao.findMovie().get(i).getTime()+" ");
System.out.print(movieDao.findMovie().get(i).getHome()+" ");
System.out.println();
}
System.out.println("----请您选择要进行的操作 ");
System.out.println("1. 增加影片");
System.out.println("2. 删除影片");
System.out.println("3. 修改影片");
System.out.println("4. 查找影片");
System.out.println("5. 返回首页面");
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
switch (s1) {
case "1" :
addmovie();
break;
case "2" :
delMovie();
break;
case "3" :
modMovie();
break;
case "4" :
findMovie();
break;
case "5" :
manage_select();
break;
default:
System.out.println("您输入的选项有误");
manage_one();
break;
}
}
// 查看用户信息
public static void manage_two() throws SQLException {
System.out.println("----用户信息----");
AccountDao accountDao = new AccountDao();
System.out.println("账号 密码");
for (int i=0;i<accountDao.findUser().size();i++){
System.out.print(" "+new AccountDao().findUser().get(i).getUser_name()+" ");
System.out.print(new AccountDao().findUser().get(i).getPassword()+" ");
System.out.println();
}
System.out.println("----请您选择要进行的操作 ");
System.out.println("1. 增加用户");
System.out.println("2. 删除用户");
System.out.println("3. 修改用户");
System.out.println("4. 查找用户");
System.out.println("5. 返回首页面");
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
switch (s1) {
case "1" :
addUser();
break;
case "2" :
delUser();
break;
case "3" :
modUser();
break;
case "4" :
findUser();
break;
case "5" :
manage_select();
break;
default:
System.out.println("您输入的选项有误");
manage_two();
break;
}
}
3. 对于实现增删改查的前端方法
// 1. 添加用户
public static void addUser() throws SQLException {
Scanner sc = new Scanner(System.in);
System.out.println("-----添加用户-----");
System.out.println("请输入用户名");
String s1 = sc.nextLine();
System.out.println("请输入密码");
String s2 = sc.nextLine();
new AccountDao().addUser(s1,s2);
System.out.println("添加成功");
manage_two();
}
// 2. 删除 用户
public static void delUser () throws SQLException {
Scanner sc = new Scanner(System.in);
System.out.println("-----删除用户-----");
System.out.println("请输入您要删除用户的名字");
String s1=sc.nextLine();
new AccountDao().delUser(s1);
System.out.println("删除成功");
manage_two();
}
// 3. 修改 用户
public static void modUser()throws SQLException{
Scanner sc = new Scanner(System.in);
System.out.println("-----修改用户-----");
System.out.println("请输入您要修改的用户的名字");
String s1 = sc.nextLine();
System.out.println("请输入用户名为: "+s1+" 的新密码");
String s2 = sc.nextLine();
new AccountDao().modUser(s1,s2);
System.out.println("修改成功");
manage_two();
}
// 4. 查找 用户
public static void findUser () throws SQLException {
Scanner sc = new Scanner(System.in);
System.out.println("-----查找用户-----");
System.out.println("请输入您要查找用户的名字");
String s1=sc.nextLine();
System.out.println("账号 密码");
System.out.print(" "+new AccountDao().findUser().get(0).getUser_name()+" ");
System.out.print(new AccountDao().findOneUser(s1).get(0).getPassword()+" ");
System.out.println();
System.out.println("查找成功");
manage_two();
}
四、结语
这个简单的系统,写了一下午,功能实现的较少,大家如果有好的建议可以再评论区指出,或者有问题想要一起探讨学习的也可以再评论区指出哟!
五、源代码展示
链接:https://pan.baidu.com/s/1FAVCs_von7hnAUaKZFVduw?pwd=ojbk
提取码:ojbk