一、音乐管理系统
增加了权限
权限代码:
public class UserDao {
public User loges(String username,String password) throws SQLException {
Connection connection=DBUtil.getConnection();
String sql="select * from tb_user where username=? and password=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
ResultSet resultSet = statement.executeQuery();
User user=new User();
if(resultSet.next()){
user.setId(resultSet.getInt(1));
user.setUsername(username);
user.setPassword(password);
user.setType(resultSet.getInt(4));
}
else{
user=null;
}
return user;
}
权限的界面代码:
public class MusicService {
public static void main(String[] args) throws SQLException, NoSuchAlgorithmException {
Scanner input = new Scanner(System.in);
UserDao userDao = new UserDao();
User user=new User();
boolean flas=true;
while (flas) {
System.out.print("请输入用户名:");
String username = input.next();
System.out.print("请输入密码:");
String password=input.next();
/*MessageDigest md=MessageDigest.getInstance("MD5");
md.update(password.getBytes());
String str=new BigInteger(1,md.digest()).toString(16);*/
System.out.println("用户名"+username + "密码:"+password);
user= userDao.loges(username,password);
MusicService musicService=new MusicService();
if (user!=null) {
musicService.getMenu(user);
flas=false;
}
else {
musicService.zhuce();
flas=false;
}
}
}
public void getMenu(User user) throws SQLException {
Scanner input=new Scanner(System.in);
MusicDao musicDao=new MusicDao();
UserDao userDao=new UserDao();
Music music=new Music();
System.out.println("---------------登录成功---------------");
System.out.println("---------欢迎来到音乐管理系统---------");
System.out.println("\t1.音乐管理\t2.用户管理\t3.退出系统");
int j=input.nextInt();
if(j==1){
System.out.println("---------欢迎进入音乐管理系统---------");
System.out.println("\t1.查询音乐\t2.添加音乐\t3.修改音乐\t4.删除音乐\t5.返回上一层");
j=input.nextInt();
switch (j){
case 1:
List<Music> musics=musicDao.findAll();
System.out.println(musics);
break;
case 2:
if(user.getType()!=0){
System.out.println("你没有权限执行该操作!");
System.out.println("返回上一级");
getMenu(user);
}
System.out.println("请输入要添加的歌名:");
String name=input.next();
System.out.println("请输入要添加的作者名:");
String write=input.next();
music.setMusicname(name);
music.setAuto(write);
musicDao.insert(music);
break;
case 3:
if(user.getType()!=0){
System.out.println("你没有权限执行该操作!");
System.out.println("返回上一级");
getMenu(user);
}
System.out.println("请输入要修改的编号:");
int id=input.nextInt();
System.out.println("请输入要修改的歌名:");
String name1=input.next();
System.out.println("请输入要修改的作者名:");
String auto=input.next();
music.setMusicname(name1);
music.setAuto(auto);
music.setId(id);
musicDao.update(music);
break;
case 4:
if(user.getType()!=0){
System.out.println("你没有权限执行该操作!");
System.out.println("返回上一级");
getMenu(user);
}
System.out.println("请输入要删除的歌名:");;
musicDao.delete(input.next());
break;
case 5:
getMenu(user);
break;
}
}
else if(j==2){
System.out.println("---------欢迎进入用户管理系统---------");
System.out.println("\t1.查询用户\t2.添加用户\t3.修改用户信息\t4.删除用户\t5.返回上一层");
int a=input.nextInt();
switch (a){
case 1:
List<User> users=userDao.find();
System.out.println(users);
System.out.println("请选择1.退出 2.返回");
int i=input.nextInt();
if(i==1){
System.exit(0);
}else{
getMenu(user);
}
break;
case 2:
System.out.print("请输入要添加的用户名:");
String username=input.next();
System.out.print("请输入要添加的用户的密码:");
String password=input.next();
System.out.print("请选择权限(0.管理员 1.普通用户):");
int js=input.nextInt();
User user1=new User();
user1.setUsername(username);
user1.setPassword(password);
user1.setType(js);
userDao.insert(user1);
break;
case 3:
System.out.print("请输入要修改的用户名:");
String name=input.next();
System.out.print("请输入要修改后的用户的密码:");
String password1=input.next();
userDao.update(name,password1);
break;
case 4:
System.out.print("请输入要删除的用户名:");
String name1=input.next();
userDao.delete(name1);
break;
case 5:
getMenu(user);
break;
}
}
}
二、n的阶乘
代码:
public static void main(String[] args){
System.out.println("请输入n:");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int sum=1;
for(int i=1;i<=n;i++){
sum=sum*i;
}
System.out.println(sum);
}