maven连接数据库实现登录修改删除转账
时间: 2024-05-04 21:18:02 浏览: 68
要使用 Maven 连接数据库,你需要在你的 Maven 项目中添加 JDBC 驱动和连接池依赖。常用的 JDBC 驱动有 MySQL、Oracle、PostgreSQL 等,连接池依赖有 Apache Commons DBCP、HikariCP 等。
以下是一个简单的登录功能实现的示例代码:
```java
import java.sql.*;
public class Login {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost/mydb";
private static final String USER = "root";
private static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行查询
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, username, password FROM user WHERE username='test' AND password='123456'";
ResultSet rs = stmt.executeQuery(sql);
// 处理结果集
if (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
System.out.println("Logged in as User " + id + " (" + username + ")");
} else {
System.out.println("Invalid username or password");
}
// 关闭资源
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
```
上面的代码使用了 MySQL 数据库,用户名为 root,密码为空,数据库名为 mydb。你需要根据你的实际情况修改这些参数。
对于修改、删除和转账等操作,你可以使用 PreparedStatement 来执行 SQL 语句,避免 SQL 注入攻击。以下是一个修改功能实现的示例代码:
```java
import java.sql.*;
public class Update {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost/mydb";
private static final String USER = "root";
private static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
PreparedStatement stmt = null;
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行更新
System.out.println("Updating record...");
String sql = "UPDATE user SET password=? WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, "654321");
stmt.setInt(2, 1);
int rows = stmt.executeUpdate();
System.out.println(rows + " rows updated");
// 关闭资源
stmt.close();
conn.close();
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
```
上面的代码将 id 为 1 的用户的密码修改为 654321。你可以根据需要修改 SQL 语句和参数。
其他操作的实现也类似,你需要使用不同的 SQL 语句和参数来执行不同的操作。注意要遵守数据库的设计规范,避免出现数据冗余和数据不一致的情况。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)