www.baidu.com:443
今天我们学习了模拟一个登入页面。
首先:打开sql,创建一个表tb_user,具体参数如下:
接着:创建几个包,包里创建几个文件,截图如下
Music、DBUtil和Test1是上节课的,我们忽略就好。
MusicDao中输入代码:
package com.zhongruan.Dao;
import com.didi.model.Music;
import com.zhongruan.Util.DBUtil;
import java.sql.Connection;
import java.sql.Preparedstatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MusicDao {
public static List<Music> findMusics(){
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List<Music> musics=new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "select * from music";
statement = connection.preparestatement(sql);
resultSet = statement.executequery();
while (resultSet.next()) {
Music music = new Music();
music.setId(resultSet.getInt(1));
music.setName(resultSet.getString(2));
music.setAuthor(resultSet.getString(3));
musics.add(music);
}
} catch (Exception e) {
e.printstacktrace();
} finally {
DBUtil.closeAll(resultSet, statement, connection);
}
return musics;
}
public static void delete(int id) throws SQLException{
Connection connection= DBUtil.getConnection();
String sql="delete from music where id=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setInt(1,id);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
public static void charu(String name, String author) throws SQLException {
Connection connection=DBUtil.getConnection();
String sql="insert into music(name,author) values(?,?)";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,name);
statement.setString(2,author);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
public static void updateMusic(int id,String newname) throws SQLException{
Connection connection=DBUtil.getConnection();
String sql="update music set name=? where id=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,newname);
statement.setInt(2,id);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
}
在UserDao中输入代码:
package com.zhongruan.Dao;
import com.zhongruan.Util.DBUtil;
import com.zhongruan.model.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
public class UserDao {
public static User findUserByUsername(String username){
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
User user=null;
try{
connection= DBUtil.getConnection();
String sql=“select * from tb_user where username=?”;
statement=connection.prepareStatement(sql);
statement.setString(1,username);
resultSet=statement.executeQuery();
while (resultSet.next()){
user=new User();
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return user;
}
public void churu(String username,String passwod){
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
try {
connection=DBUtil.getConnection();
String sql=“insert into tb_user(username,password) values(?,?)”;
statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,passwod);
statement.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
}
}
在LoginException输入代码:
package com.zhongruan.exception;
public class LoginException extends Exception{
public LoginException(String message){
super(message);
}
}
在User中输入代码:
package com.zhongruan.model;
public class User {
private String username;
private String password;
private int type;
public int getType() {
return type;
}
public void setType(integer type) {
this.type = type;
}
public String getUsername() {
return username;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", type=" + type +
'}';
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在View中输入代码:
package com.zhongruan.Util;
import com.didi.model.Music;
import com.zhongruan.Dao.MusicDao;
import com.zhongruan.Dao.UserDao;
import com.zhongruan.exception.LoginException;
import com.zhongruan.model.User;
import org.omg.CORBA.PUBLIC_MEMBER;
import java.sql.SQLException;
import java.util.List;
import java.util.scanner;
public class View {
public static void main(String[] args) throws LoginException, SQLException {
boolean flag = true;
while (flag) {
Scanner input = new Scanner(System.in);
System.out.println(“请输入用户名”);
String username = input.next();
System.out.println(“请输入密码”);
String passwod = input.next();
UserDao userDao = new UserDao();
User user = UserDao.findUserByUsername(username);
if (user == null) {
System.out.println("---该用户尚未注册。请先注册---");
System.out.println("请输入注册名");
String name = input.next();
System.out.println("请输入注册码");
String pw = input.next();
userDao.churu(name, pw);
} else if (user.getPassword().equals(passwod)) {
System.out.println("---欢迎来到音乐管理系统---");
musicSystem();
flag=false;
} else {
//System.out.println("---密码输入错误,请重新输入");
throw new LoginException("登入失败");
}
}
}
public static void musicSystem() throws SQLException {
Scanner input=new Scanner(System.in);
System.out.println("1.音乐查询 2.音乐添加 3.音乐修改 4.音乐删除 5退出");
int choice=input.nextint();
switch (choice){
case 1:
//1.音乐查询
List<Music> musics=MusicDao.findMusics();
System.out.println(musics);
break;
case 2:
System.out.println("请输入要输入的音乐名");
String ym=input.next();
System.out.println("请输入插入的作者名");
String au=input.next();
MusicDao.charu(ym,au);
break;
case 3:
System.out.println("请输入要删除的id");
int id=input.nextInt();
MusicDao.delete(id);
case 4:
System.out.println("请输入要修改的 id");
int id1=input.nextInt();
System.out.println("请输入新的音乐名");
String newym=input.next();
System.out.println("请输入新的作者名");
String newau=input.next();
MusicDao.updateMusic(id1,newym,newau);
break;
case 5:
System.exit(0);
}
}
}
代码到这个步骤就已经写完了,接下来在View中运行,看一下结果怎么样。
打开SQL,看看有没有添加上用户
添加上了就成功了。
接着就运行来一步步测试,看看结果:
打开sql看结果,首先是用户,其次是music:
修改:
SQL:
查询:
删除:
SQL:
退出:
密码错误:
文章创建于: 2019-09-18 14:58:05
相关阅读
淘宝搜索流量入口有哪些,史上最全!这是很多卖家关注的话题,很多时候他们往往不知道淘宝搜索流量入口有哪些,小编针对这一问题整合相关
潮电街入驻有哪些要求?怎样才能入驻潮电街?不少卖家想要入驻潮电街,下面seo实验室小编为大家带来潮电街入驻要求详解,希望对各位入
阿里妈妈鹊桥入口在哪里?鹊桥计划是阿里妈妈新推出的一个产品,很多淘宝卖家不知道这个阿里妈妈鹊桥入口在哪里?,也不知道怎么玩,今天
Linux入门之ext\ext2\ext3\ext4的介绍与区别
Linux之ext\ext2\ext3\ext4的介绍与区别 文件系统名称 介绍 特点 优势 ext 第一代扩展文件系统,一种文件系
每年的黑五大促都是海淘党的福音,2016天猫黑色星期五大促全球直播,11月25日至27日,买手带你直击全球黑五扫货现场。今天seo实验室小