一、音乐管理系统的注册登录
1.Usec的代码
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2.数据库DBUtil的代码
public class DBUtil {
public static Connection getConnection(){
Connection connection=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection, PreparedStatement preparedStatement,ResultSet resultSet){
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.登录的代码
public boolean 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();
if(resultSet.next()){
return true;
}
else{
return false;
}
}
4.注册的代码
public boolean zhuce(String username,String password) throws SQLException {
Connection connection=DBUtil.getConnection();
String sql="insert into tb_user (username,password) values (?,?)";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
int a=statement.executeUpdate();
if(a!=0){
return true;
}
else{
return false;
}
}
5.登录注册的界面
public class MusicService {
public static void main(String[] args) throws SQLException {
System.out.print("请输入用户名:");
Scanner input=new Scanner(System.in);
String username=input.next();
System.out.print("请输入密码:");
String password=input.next();
System.out.println(username+password);
UserDao userDao=new UserDao();
boolean flag=userDao.loges(username,password);
if(flag){
System.out.println("---------------登录成功---------------");
System.out.println("---------欢迎来到音乐管理系统---------");
}
else {
System.out.println("---------------登录失败---------------");
System.out.println("---------------请先注册---------------");
System.out.print("请输入注册名:");
String zcm=input.next();
System.out.print("请输入密码:");
String zcmm1=input.next();
System.out.print("请确认密码:");
String zcmm2=input.next();
if(zcmm1.equals(zcmm2)){
flag=userDao.zhuce(zcm,zcmm1);
if(flag){
System.out.println("---------------注册成功---------------");
}
else{
System.out.println("---------------注册失败---------------");
}
}
else{
System.out.println("注册密码错误,请重新输入!");
}
}
}
}