完成简单的用户操作:
实现代码:
package com.service;
import java.util.Date;
public class User {
private int sid;
private String sname;
private String password;
private String email;
private Date brithday;
get()
set()
tostring()
}
配置DBUtils.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysql01?useSSL=false
user=root
password=root
JDBC工具类
package com.Tool;
import java.sql.*;
import java.util.ResourceBundle;
public class DBUtils {
private static String url;
private static String user;
private static String password;
private static String driverClass;
static {
//加载配置文件:
ResourceBundle dbUtils = ResourceBundle.getBundle("DBUtils");
driverClass = dbUtils.getString("driverClass");
url = dbUtils.getString("url");
user=dbUtils.getString("user");
password=dbUtils.getString("password");
//加载驱动
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
//释放资源
public static void close(Statement statement,Connection connection){
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//释放资源
public static void close(ResultSet resultSet, Statement statement, Connection connection){
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package com.login;
import com.service.Server;
import com.service.User;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.util.Scanner;
public class Login {
static Scanner input=new Scanner(System.in);
public static void show(){
System.out.println("--------------用户系统----------------");
System.out.println("1.登录账户");
System.out.println("2.修改密码");
System.out.println("3.注册用户");
System.out.println("4.注销用户");
System.out.println("5.退出账户");
System.out.println("-------------------------------------");
}
public static void input(){
System.out.println("请输入用户名:");
String sname = input.nextLine();
System.out.println("请输入密 码:");
String password=input.nextLine();
User user=new User();
Server server = new Server();
server.findUser(sname,password);
if(server==null){
System.out.println("欢迎您:"+ user.getSname());
}else {
System.out.println("登录失败");
}
}
public static void update(){
System.out.println("请输入用户新密码");
String password= input.nextLine();
System.out.println("请输入修改编号 :");
int number=input.nextInt();
User user=new User();
Server server = new Server();
int update = server.Update(password, number);
if(update>0){
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
}
public static void Insert(){
System.out.println("请输入id:");
int sid = input.nextInt();
System.out.println("请输入用户名:");
String sname = input.next();
System.out.println("请输入密 码:");
String password=input.next();
System.out.println("请输入用户邮箱");
String email= input.next();
System.out.println("请输入日期");
String brithday=input.next();
Server server = new Server();
int insert= server.Insert(sid,sname,password,email,brithday);
if(insert>0){
System.out.println("注册成功");
}else {
System.out.println("注册失败");
}
}
public static void Delete(){
System.out.println("请输入删除编号:");
int number=input.nextInt();
Server server = new Server();
int delete = server.Delete(number);
if(delete>0){
System.out.println("注销成功");
}else {
System.out.println("注销失败");
}
}
}
package com.service;
import com.Tool.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Server {
//查询
public User findUser(String sname, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;//无参数
User u=null;
try {
//1加载驱动 并获得连接
conn = DBUtils.getConnection();
//2编写sql(预编译)
String sql="SELECT * FROM stu WHERE sname=? AND PASSWORD=?";
//3得到执行SQL小货车
ps = conn.prepareStatement(sql);
//4设置参数
ps.setString(1,sname);
ps.setString(2,password);
//输出sql
System.out.println(sql);
//5执行sql
rs = ps.executeQuery();
//处理结果
u = null;
if (rs.next()) {
u=new User();
u.setSid(rs.getInt("sid"));
u.setSname(rs.getString("sname"));
u.setPassword(rs.getString("password"));
u.setEmail(rs.getString("email"));
u.setBrithday(rs.getDate("brithday"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
DBUtils.close(rs,ps,conn);
}
return u;
}
//修改
public int Update(String password, int number) {
Connection connection = null;
PreparedStatement preparedStatement = null;
int result = 0;
try {
//1.获取链接
connection = DBUtils.getConnection();
//2.编写sql(预编译)
String sql ="UPDATE stu SET sname=? WHERE sid=?";
//3得到执行SQL小货车
preparedStatement = connection.prepareStatement(sql);
//4设置参数
preparedStatement.setString(1,password);
preparedStatement.setInt(2,number);
//输出
System.out.println(sql);
//5执行sql
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.close(null,preparedStatement,connection);
}
return result;
}
//插入注册
public int Insert(int sid, String sname, String password, String email, String brithday) {
Connection connection = null;
PreparedStatement preparedStatement = null;
int result = 0;
try {
//1.获取链接
connection = DBUtils.getConnection();
//2.编写sql(预编译)
String sql ="INSERT INTO stu (sid,sname,PASSWORD,email,brithday)VALUES(?,?,?,?,?)";
//3得到执行SQL小货车
preparedStatement = connection.prepareStatement(sql);
//4设置参数
preparedStatement.setInt(1,sid);
preparedStatement.setString(2,sname);
preparedStatement.setString(3,password);
preparedStatement.setString(4,email);
preparedStatement.setString(5,brithday);
//输出
System.out.println(sql);
//5执行sql
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.close(null,preparedStatement,connection);
}
return result;
}
//删除
public int Delete(int number) {
Connection connection = null;
PreparedStatement preparedStatement = null;
int result = 0;
try {
//1.获取链接
connection = DBUtils.getConnection();
//2.编写sql(预编译)
String sql ="DELETE FROM stu WHERE sid=?";
//3得到执行SQL小货车
preparedStatement = connection.prepareStatement(sql);
//4设置参数
preparedStatement.setInt(1,number);
//输出
System.out.println(sql);
//5执行sql
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.close(null,preparedStatement,connection);
}
return result;
}
}
测试类
package com.Main;
import com.login.Login;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Login.show();
Scanner input=new Scanner(System.in);
while (true) {
System.out.println("请输入执行操作:");
int number = input.nextInt();
switch (number) {
case 1:
Login.input();
break;
case 2:
Login.update();
break;
case 3:
Login.Insert();
break;
case 4:
Login.Delete();
break;
case 5:
System.out.println("5.退出账户");
System.exit(1);
}
}
}
}