本系统采用了三层架构以及Jdbc的技术简单实现了存钱、取钱、转账、查看余额、以及修改密码等功能
1.准备mysql的jar包,进行导入以及一个db.properties配置文件
2.工具类DbUtils类:包装了获取连接、增删改、以及对事务操作的一些方法
/**
* @description:
* @author: yrm
* @create: 2020-08-22 08:30
**/
public class DbUtils {
private static String driver;
private static String url;
private static String user;
private static String password;
//创建一个线程局部变量
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
//1.注册驱动
static {
Properties properties = new Properties();
InputStream is = DbUtils.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
System.out.println("注册失败!");
}
}
//2.获取连接
public static Connection getConnection() {
try {
//判断线程中有没有连接
Connection conn = threadLocal.get();
if (conn == null) {
conn = DriverManager.getConnection(url, user, password);
//绑定连接
threadLocal.set(conn);
}
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//3.释放资源
public static void closeAll(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
//是否开启事务... 如果没开启事务 则进行销毁连接
if (conn.getAutoCommit()) {
threadLocal.remove();
conn.close();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//4.执行命令
public static int executeUpdate(String sql,Object... params) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.closeAll(null,ps,conn);
}
return -1;
}
//1.开启事务
public static void beginAutoCommit() {
Connection conn = getConnection();
if (conn != null) {
try {
conn.setAutoCommit(