java+mysql实现简单电影影片管理系统
系统结构介绍
1.主要视图
分为以下两个部分:
1.数据库部分:有一个movies数据库,库里面有两张表,一是电影表movies,二是用户表user_info,建表采用的是图形化方式建表,但是也可以用建表语句建表;
2.菜单部分,这一部分写在了demo.java文件里面。具体见代码
2.项目准备
这一部分是利用eclipse连接mysql数据库,主要操作大体如下:
不熟悉eclipse的小伙伴可以搜关于eclipse的使用教程
1.在eclipse新建一个java工程Movies-City
2.下载数据库连接驱动mysql-connector-java-5.1.19-bin.jar,具体看你自己使用的是哪个版本的Navicat.下载地址可以去这里(包含mysql,Navicat,以及连接jar包)
链接:https://pan.baidu.com/s/1vkmmntdKDL5ZAR7SYPPb3Q?pwd=s98e
提取码:s98e
3.代码
又到了激动人心的代码时刻啦,建议把这三个都放到同一个包下,比如我的都是在action包下,包名可自定义
本项目的主要代码分为两个部分:
1.Movies.java和user.java
主要是关于电影以及用户的各个属性
package action;
public class Movies {
private String name;
private Integer id;//电影的id发行号
private String director;
private String type;//电影所属的类型
public double score;//电影评分
//有参构造函数,便于demo.java里的函数使用
public Movies(String id,String name) {
this.id=Integer.parseInt(id);
this.name=name;
}
public Movies(String name,String director,String type,double score,Integer id) {
this.name=name;
this.director=director;
this.type=type;
this.score=score;
this.id=id;
}
public Movies(String name, Integer id, String director, String type, double score) {
this.name = name;
this.id = id;
this.director = director;
this.type = type;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
//toString方法
@Override
public String toString() {
return "movies [name=" + name + ", id=" + id + ", director=" + director + ", type=" + type + ", score="
+ score + "]";
}
}
package action;
public class user{
String username;
String password;
String sex;
public user(String username, String password, String sex) {
this.username = username;
this.password = password;
this.sex = sex;
}
public user(String username,String password) {
this.username=username;
this.password=password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "user [username=" + username + ", password=" + password + ", sex=" + sex + "]";
}
}
2.demo.java
主要包含菜单界面,然后是对数据库进行操作的各种函数
主要函数如下:
package action;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.sql.Connection;
public class demo {
//前三个是有关数据库的信息,其中movies是建立的数据库
private static String url="jdbc:mysql://localhost:3306/movies";
private static String user="root";
private static String password="root";
private static String adminusername="双枪";//定义好的管理员用户名
private static String adminpassword="2233234242";//定义好的管理员密码
private static String inputadusername;//手动输入的管理员用户名
private static String inputadpassword;//手动输入的管理员密码
public static void main(String[] args) {
System.out.println("--------------------欢迎来到电影影片管理系统--------------------");
System.out.println("请选择您的身份:");
System.out.println("1.管理员");
System.out.println("2.用户");
Scanner sc=new Scanner(System.in);
int op=sc.nextInt();
switch(op) {
case 1:
System.out.println("请输入管理员用户名:");
inputadusername=sc.next();
System.out.println("请输入管理员密码:");
inputadpassword=sc.next();
if(isTruepassword(inputadusername,inputadpassword)) {
System.out.println("登录成功!");
System.out.println("请选择您要进行的操作:");
System.out.println("1.查看用户信息;");
System.out.println("2.通过id号更改电影信息;");
System.out.println("3.通过其他例如电影名更改id号;");
System.out.println("4.退出");
int choice=sc.nextInt();
if(choice==1) selectalluser();//查看所有用户
if(choice==2) {
//通过id号更改电影信息
System.out.println("请输入您要更改的电影的id号:");//id号不变
Scanner a=new Scanner(System.in);
String newid=a.next();
System.out.println("请输入要更改的电影的名字:");
String newname=a.next();
System.out.println("请输入要更改的电影的导演:");
String newdirector=a.next();
System.out.println("请输入要更改的电影的类型:");
String newtype=a.next();
System.out.println("请输入要更改的电影的评分:");
double newscore=a.nextDouble();
//创建一个Movies对象
Movies x=new Movies(newname,newdirector,newtype,newscore,Integer.parseInt(newid));
updatemoviebyother(x);
a.close();//a包括更改的电影的信息
}
if(choice==3) {
//通过其他更改电影信息
System.out.println("请输入您要更改的电影的名字:");
Scanner a=new Scanner(System.in);
String x=a.next();
System.out.println("请输入改的id号:");
String newid1=a.next();//id号
Movies y=new Movies(newid1,x);
updatemoviebyid(y);
a.close();
}
}
else {
System.out.println("登录失败!");//如果管理员账号或者密码对不上,就登陆失败
}
break;
//用户菜单
case 2:System.out.println("---------欢迎来到电影影片管理系统---------");
System.out.println("请选择您的身份:");
System.out.println("1.游客(未注册用户)");
System.out.println("2.普通用户");
int option=sc.nextInt();
if(option==1) {
System.out.println("请输入您的用户名:");
Scanner visitor=new Scanner(System.in);
String visitorusername=visitor.next();
if(isTrueexisttheuser(visitorusername)) {
System.out.println("对不起,该用户已存在!");
}
else {
System.out.println("该用户不存在,请继续创建");
System.out.println("请输入您的密码:");
String visitorpassword=visitor.next();
System.out.println("请输入您的性别:");
String sex=visitor.next();
user u=new user(visitorusername,visitorpassword,sex);
adduser(u);
}
visitor.close();
}
if(option==2) {
System.out.println("请输入您的用户名:");
Scanner visitor=new Scanner(System.in);
String ex_username=visitor.next();
System.out.println("请输入您的密码:");
String ex_password=visitor.next();
user u=new user(ex_username,ex_password);
if(isTruepassworduser(u)) {
System.out.println("登录成功!");
}
else {
System.out.println("登录失败");
}
visitor.close();//加close是为了防止资源泄露的报错
}
break;
}
//通过id号更改电影信息
/*System.out.println("请输入您要更改的电影的id号:");//id号不变
Scanner a=new Scanner(System.in);
String newid=a.next();
Movies x=new Movies("吸血鬼日记","ashc","惊悚",7.8,Integer.parseInt(newid));
updatemoviebyother(x);
*/
//通过其他例如电影名更改id号
/*System.out.println("请输入您要更改的电影的名字:");
String x=a.next();
System.out.println("请输入改的id号:");
String newid1=a.next();//id号
Movies y=new Movies(newid1,x);
updatemoviebyid(y);
*/
sc.close();//sc数据扫描仪包括输入的选择op,以及管理员用户名,管理员密码,以及管理员 的选择choice,用户的选择option,加close理由同上
}
//查看所有电影
public static void select() {
// TODO 自动生成的方法存根
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Connection connection=null;
Statement stat=null;
ResultSet rs=null;
//获取连接
try {
connection=DriverManager.getConnection(url,user,password);//获取连接
String sql="select * from movies";
stat=connection.createStatement();//接受运行之后产生的对象
rs=stat.executeQuery(sql);//获取sql语句运行执行之后的结果
while(rs.next()) {
//接收各个属性
String id=rs.getString("id");
String name=rs.getString("name");
String director=rs.getString("director");
String type=rs.getString("type");
String score=rs.getString("score");
//放到一个movie对象里
Movies movie=new Movies(name, Integer.parseInt(id), director, type, Double.parseDouble(score));
System.out.println(movie);
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭connection对象
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//关闭statement对象
try {
stat.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//关闭rs对象
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
//删除电影
public static void delete(Movies m) {
// TODO 自动生成的方法存根
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Connection connection=null;
PreparedStatement ps=null;
try {
connection=DriverManager.getConnection(url,user,password);
String sql="delete from movies where id=?";//?表示占位符
ps=connection.prepareStatement(sql);
ps.setInt(1, m.getId());//从sql语句中的第一个?开始,getid传入的是m的id号
ps.executeUpdate();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭connection对象
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//关闭ps对象
try {
ps.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
//添加电影
public static void addmovie(Movies m) {
// TODO 自动生成的方法存根
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Connection connection=null;
PreparedStatement ps=null;
try {
connection=DriverManager.getConnection(url,user,password);
String sql="INSERT INTO movies(name,id,director,type,score) VALUES(?,?,?,?,?) ";//?表示占位符
ps=connection.prepareStatement(sql);//接入connection的预编译对象
ps.setString(1, m.getName());//从sql语句中的第一个?开始,getid传入的是m的id号
ps.setInt(2,m.getId());
ps.setString(3,m.getDirector());
ps.setString(4, m.getType());
ps.setDouble(5, m.getScore());//把m里面的整个属性赋值给?里面
ps.executeUpdate();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭connection对象
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
//通过id号更改电影信息
public static void updatemoviebyother(Movies m) {
// TODO 自动生成的方法存根
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Connection connection=null;
PreparedStatement ps=null;
try {
connection=DriverManager.getConnection(url,user,password);
String sql="update movies set name=?,director=?,type=?,score=? where ID=?";//?表示占位符
ps=connection.prepareStatement(sql);//接入connection的预编译对象
//从sql语句中的第一个?开始,后面的传入的是相对应的得到该属性的Get方法
ps.setString(1,m.getName());
ps.setString(2,m.getDirector());
ps.setString(3, m.getType());
ps.setDouble(4, m.getScore());
ps.setInt(5, m.getId());//把m里面的整个属性赋值给?里面
ps.executeUpdate();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭connection对象
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//关闭ps对象
try {
ps.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
//通过其他信息如电影名更改电影信息
public static void updatemoviebyid(Movies m) {
// TODO 自动生成的方法存根
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Connection connection=null;
PreparedStatement ps=null;
try {
connection=DriverManager.getConnection(url,user,password);
String sql="update movies set id=? where name=?";//?表示占位符
ps=connection.prepareStatement(sql);//接入connection的预编译对象
//从sql语句中的第一个?开始,后面的传入的是相对应的得到该属性的Get方法
ps.setInt(1, m.getId());
ps.setString(2,m.getName());
//把m里面的整个属性赋值给?里面
ps.executeUpdate();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭connection对象
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//关闭ps对象
try {
ps.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
//判断管理员账号密码是否正确
public static boolean isTruepassword(String inputadusername,String inputadpassword) {
if(inputadusername.equals(adminusername)&&inputadpassword.equals(adminpassword)) {
return true;
}
else {
return false;
}
}
//查看所有用户
public static void selectalluser() {
// TODO 自动生成的方法存根
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Connection connection=null;
Statement stat=null;
ResultSet rs=null;
//获取连接
try {
connection=DriverManager.getConnection(url,user,password);//获取连接
String sql="select * from user_info";
stat=connection.createStatement();//接受运行之后产生的对象
rs=stat.executeQuery(sql);//获取sql语句运行执行之后的结果
while(rs.next()) {
//接收各个属性
String username=rs.getString("username");
String password=rs.getString("password");
String sex=rs.getString("sex");
//放到一个user对象里
user user=new user(username,password,sex);
System.out.println(user);
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭connection对象
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//关闭statement对象
try {
stat.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//关闭rs对象
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
//判断数据库里面是否已有该用户
public static boolean isTrueexisttheuser(String s) {
// TODO 自动生成的方法存根
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Connection connection=null;
PreparedStatement ps=null;
ResultSet rs=null;
int flag=0;//flag标记表示是否执行了while(rs.next())里面的语句
try {
connection=DriverManager.getConnection(url,user,password);
String sql="select * from user_info where username=?";//?表示占位符
ps=connection.prepareStatement(sql);//接入connection的预编译对象
ps.setString(1, s);
rs=ps.executeQuery();//存储运行结果
while(rs.next()) {
String username=rs.getString("username");
if(username.equals(s)) {
flag=1;
}
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭connection对象
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(flag==1) {
return true;
}
else {
return false;
}
}
//添加用户
public static void adduser(user u) {
// TODO 自动生成的方法存根
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Connection connection=null;
PreparedStatement ps=null;
try {
connection=DriverManager.getConnection(url,user,password);
String sql="insert into user_info(username,password,sex) values(?,?,?);";//?表示占位符
ps=connection.prepareStatement(sql);//接入connection的预编译对象
ps.setString(1, u.getUsername());//从sql语句中的第一个?开始,getid传入的是m的id号
ps.setString(2,u.getPassword());
ps.setString(3,u.getSex());//把u里面的整个属性赋值给?里面
ps.executeUpdate();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭connection对象
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
//判断密码是否正确
public static boolean isTruepassworduser(user u) {
// TODO 自动生成的方法存根
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Connection connection=null;
PreparedStatement ps=null;
ResultSet rs=null;
int flag=0;
try {
connection=DriverManager.getConnection(url,user,password);
String sql="select password from user_info where username=?";//?表示占位符
ps=connection.prepareStatement(sql);//接入connection的预编译对象
ps.setString(1, u.username);
rs=ps.executeQuery();//存储运行结果
while(rs.next()) {
String password=rs.getString("password");
if(password.equals(u.password)) {
flag=1;
}
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭connection对象
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(flag==1) {
return true;
}
else {
return false;
}
}
}
以上就是该项目的所有内容,如果有需要的可以自取,。
说明:本项目主要针对新手,前端没有实现,为了新手便于理解。