maven实现登录修改删除转账
时间: 2024-05-13 22:14:11 浏览: 41
maven写的银行小系统简单的实现一些功能
Maven 是一个项目管理工具,它可以帮助我们自动化构建、测试和部署 Java 项目。而实现登录、修改、删除和转账等功能需要编写 Java 代码和操作数据库。
下面是一个使用 Maven 实现登录、修改、删除和转账功能的示例项目:
1. 创建 Maven 项目
首先,我们需要创建一个 Maven 项目。可以使用 Eclipse 或者 IntelliJ IDEA 等 Java IDE 工具创建 Maven 项目。
2. 添加依赖
我们需要添加如下依赖到 pom.xml 文件中:
```
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
```
其中,mysql-connector-java 是操作 MySQL 数据库的 JDBC 驱动,javax.servlet-api 是操作 Servlet 的 API。
3. 编写代码
接下来,我们需要编写 Java 代码实现登录、修改、删除和转账等功能。下面是示例代码:
```
// 登录
public boolean login(String username, String password) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
String sql = "select * from user where username=? and password=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
stmt.setString(2, password);
rs = stmt.executeQuery();
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, stmt, rs);
}
return false;
}
// 修改密码
public boolean changePassword(String username, String oldPassword, String newPassword) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = getConnection();
String sql = "update user set password=? where username=? and password=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, newPassword);
stmt.setString(2, username);
stmt.setString(3, oldPassword);
int rows = stmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, stmt, null);
}
return false;
}
// 删除用户
public boolean deleteUser(String username) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = getConnection();
String sql = "delete from user where username=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
int rows = stmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, stmt, null);
}
return false;
}
// 转账
public boolean transfer(String fromAccount, String toAccount, double amount) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
String sql1 = "update account set balance=balance-? where account=?";
stmt = conn.prepareStatement(sql1);
stmt.setDouble(1, amount);
stmt.setString(2, fromAccount);
int rows1 = stmt.executeUpdate();
if (rows1 <= 0) {
conn.rollback();
return false;
}
String sql2 = "update account set balance=balance+? where account=?";
stmt = conn.prepareStatement(sql2);
stmt.setDouble(1, amount);
stmt.setString(2, toAccount);
int rows2 = stmt.executeUpdate();
if (rows2 <= 0) {
conn.rollback();
return false;
}
conn.commit();
return true;
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
} finally {
close(conn, stmt, null);
}
return false;
}
// 获取数据库连接
private Connection getConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
String username = "root";
String password = "root";
return DriverManager.getConnection(url, username, password);
}
// 关闭资源
private void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
这段代码定义了一个 UserDao 类,其中包含了 login、changePassword、deleteUser 和 transfer 四个方法,分别用于实现登录、修改密码、删除用户和转账功能。getConnection 方法用于获取数据库连接,close 方法用于关闭资源。
4. 编写 Servlet
最后,我们需要编写 Servlet,将前端请求与 Java 代码联系起来。下面是示例代码:
```
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private UserDao userDao = new UserDao();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
boolean result = userDao.login(username, password);
if (result) {
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
}
@WebServlet("/changePassword")
public class ChangePasswordServlet extends HttpServlet {
private UserDao userDao = new UserDao();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String oldPassword = request.getParameter("oldPassword");
String newPassword = request.getParameter("newPassword");
boolean result = userDao.changePassword(username, oldPassword, newPassword);
if (result) {
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("changePassword.jsp");
}
}
}
@WebServlet("/deleteUser")
public class DeleteUserServlet extends HttpServlet {
private UserDao userDao = new UserDao();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
boolean result = userDao.deleteUser(username);
if (result) {
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("deleteUser.jsp");
}
}
}
@WebServlet("/transfer")
public class TransferServlet extends HttpServlet {
private UserDao userDao = new UserDao();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fromAccount = request.getParameter("fromAccount");
String toAccount = request.getParameter("toAccount");
double amount = Double.parseDouble(request.getParameter("amount"));
boolean result = userDao.transfer(fromAccount, toAccount, amount);
if (result) {
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("transfer.jsp");
}
}
}
```
这段代码定义了四个 Servlet,分别对应登录、修改密码、删除用户和转账四个功能。每个 Servlet 通过 UserDao 类中的方法实现相应的功能。
5. 编译打包
最后,我们需要使用 Maven 编译打包项目。在项目根目录下执行以下命令:
```
mvn clean package
```
执行完成后,会在 target 目录下生成一个 war 包。将这个 war 包部署到 Tomcat 等 Java Web 服务器上即可使用。
阅读全文