java银行存取款系统mysql
时间: 2023-08-11 21:46:46 浏览: 119
您的问题是关于Java银行存取款系统与MySQL的相关问题。我可以给您提供一些基本的信息:
Java银行存取款系统:Java是一种广泛使用的编程语言,可以用来开发各种类型的应用程序。在银行领域,Java可以用来开发银行存取款系统。这种系统可以让用户通过电子渠道(比如ATM机、网银等)进行存款、取款、转账等操作。
MySQL:MySQL是一种常用的关系型数据库管理系统,可以用来存储和管理银行存取款系统中的各种数据。比如,可以用MySQL来存储用户信息、账户余额、交易记录等数据。
在Java银行存取款系统中,可以通过Java程序来连接MySQL数据库,进行数据的读取、更新、插入等操作。通常,需要使用一些Java库(比如JDBC)来实现这些功能。
希望这些信息对您有所帮助。如果您有更具体的问题,可以进一步提出,我会尽力回答。
相关问题
java银行存取款系统mysql的代码
很抱歉,由于Java银行存取款系统与MySQL的代码实现非常复杂,完整的代码无法在此处提供。但是,我可以给出一些基本的代码示例,帮助您理解Java和MySQL之间的交互过程。
首先,您需要使用Java代码来连接MySQL数据库。这可以通过以下代码实现:
```java
import java.sql.*;
public class MySQLAccess {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public void readDataBase() throws Exception {
try {
// 连接到MySQL数据库
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");
// 执行查询
statement = connect.createStatement();
resultSet = statement
.executeQuery("select * from feedback.comments");
writeResultSet(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException {
// 打印查询结果
while (resultSet.next()) {
String user = resultSet.getString("myuser");
String website = resultSet.getString("webpage");
String summary = resultSet.getString("summary");
Date date = resultSet.getDate("datum");
String comment = resultSet.getString("comments");
System.out.println("User: " + user);
System.out.println("Website: " + website);
System.out.println("Summary: " + summary);
System.out.println("Date: " + date);
System.out.println("Comment: " + comment);
}
}
// 关闭连接
private void close() {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
}
```
在上述代码中,我们通过`DriverManager.getConnection()`方法连接到MySQL数据库,然后使用`Statement`对象执行查询语句,并通过`ResultSet`对象获取查询结果。
除了查询外,您还可以使用Java代码来插入、更新和删除MySQL数据库中的数据。以下是插入数据的示例代码:
```java
// 插入数据
statement = connect.createStatement();
statement.executeUpdate("INSERT INTO feedback.comments "
+ "VALUES (default, 'myuser', 'mypage', 'test', "
+ "now(), 'this is a test')");
```
如果您需要更详细的Java代码示例,请参考以下资源:
- [Java MySQL CRUD Example Tutorial](https://www.journaldev.com/8758/java-mysql-crud-example-tutorial)
- [Java MySQL Connectivity Example](https://www.javatpoint.com/example-to-connect-to-the-mysql-database)
java课程设计银行存取款操作系统
以下是一个Java银行存取款操作系统的简单实现,包括用户注册、登录、修改密码、余额查询、充值、转账和取款等功能。该系统使用MySQL数据库存储用户信息和交易记录,并采用正则表达式对用户输入进行规范化处理。
```java
import java.sql.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BankSystem {
private static final String DB_URL = "jdbc:mysql://localhost:3306/bank";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "123456";
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
stmt = conn.createStatement();
System.out.println("欢迎使用银行存取款管理系统!");
while (true) {
System.out.println("请选择操作:");
System.out.println("1.用户注册 2.用户登录 3.退出系统");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
register();
break;
case 2:
login();
break;
case 3:
System.out.println("感谢使用银行存取款管理系统!");
System.exit(0);
default:
System.out.println("无效的选择,请重新输入!");
}
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void register() throws SQLException {
System.out.println("请输入用户名:");
String username = scanner.nextLine();
if (usernameExists(username)) {
System.out.println("该用户名已存在,请重新输入!");
return;
}
System.out.println("请输入密码:");
String password = scanner.nextLine();
if (!isValidPassword(password)) {
System.out.println("密码不符合规范,请重新输入!");
return;
}
System.out.println("请输入初始余额:");
double balance = scanner.nextDouble();
scanner.nextLine();
String sql = "INSERT INTO users (username, password, balance) VALUES ('" + username + "', '" + password + "', " + balance + ")";
stmt.executeUpdate(sql);
System.out.println("注册成功!");
}
private static void login() throws SQLException {
System.out.println("请输入用户名:");
String username = scanner.nextLine();
if (!usernameExists(username)) {
System.out.println("该用户名不存在,请重新输入!");
return;
}
System.out.println("请输入密码:");
String password = scanner.nextLine();
String sql = "SELECT * FROM users WHERE username='" + username + "' AND password='" + password + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
System.out.println("登录成功!");
while (true) {
System.out.println("请选择操作:");
System.out.println("1.修改密码 2.余额查询 3.充值 4.转账 5.取款 6.返回上一级");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
changePassword(username);
break;
case 2:
checkBalance(username);
break;
case 3:
deposit(username);
break;
case 4:
transfer(username);
break;
case 5:
withdraw(username);
break;
case 6:
return;
default:
System.out.println("无效的选择,请重新输入!");
}
}
} else {
System.out.println("密码错误,请重新输入!");
}
}
private static void changePassword(String username) throws SQLException {
System.out.println("请输入旧密码:");
String oldPassword = scanner.nextLine();
String sql = "SELECT * FROM users WHERE username='" + username + "' AND password='" + oldPassword + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
System.out.println("请输入新密码:");
String newPassword = scanner.nextLine();
if (!isValidPassword(newPassword)) {
System.out.println("密码不符合规范,请重新输入!");
return;
}
sql = "UPDATE users SET password='" + newPassword + "' WHERE username='" + username + "'";
stmt.executeUpdate(sql);
System.out.println("密码修改成功!");
} else {
System.out.println("旧密码错误,请重新输入!");
}
}
private static void checkBalance(String username) throws SQLException {
String sql = "SELECT balance FROM users WHERE username='" + username + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
double balance = rs.getDouble("balance");
System.out.println("当前余额为:" + balance);
}
}
private static void deposit(String username) throws SQLException {
System.out.println("请输入充值金额:");
double amount = scanner.nextDouble();
scanner.nextLine();
String sql = "UPDATE users SET balance=balance+" + amount + " WHERE username='" + username + "'";
stmt.executeUpdate(sql);
sql = "INSERT INTO transactions (username, type, amount) VALUES ('" + username + "', '充值', " + amount + ")";
stmt.executeUpdate(sql);
System.out.println("充值成功!");
}
private static void transfer(String username) throws SQLException {
System.out.println("请输入对方用户名:");
String toUsername = scanner.nextLine();
if (!usernameExists(toUsername)) {
System.out.println("该用户名不存在,请重新输入!");
return;
}
System.out.println("请输入转账金额:");
double amount = scanner.nextDouble();
scanner.nextLine();
String sql = "SELECT balance FROM users WHERE username='" + username + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
double balance = rs.getDouble("balance");
if (balance < amount) {
System.out.println("余额不足,转账失败!");
return;
}
sql = "UPDATE users SET balance=balance-" + amount + " WHERE username='" + username + "'";
stmt.executeUpdate(sql);
sql = "UPDATE users SET balance=balance+" + amount + " WHERE username='" + toUsername + "'";
stmt.executeUpdate(sql);
sql = "INSERT INTO transactions (username, type, amount) VALUES ('" + username + "', '转账', -" + amount + ")";
stmt.executeUpdate(sql);
sql = "INSERT INTO transactions (username, type, amount) VALUES ('" + toUsername + "', '转账', " + amount + ")";
stmt.executeUpdate(sql);
System.out.println("转账成功!");
}
}
private static void withdraw(String username) throws SQLException {
System.out.println("请输入取款金额:");
double amount = scanner.nextDouble();
scanner.nextLine();
String sql = "SELECT balance FROM users WHERE username='" + username + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
double balance = rs.getDouble("balance");
if (balance < amount) {
System.out.println("余额不足,取款失败!");
return;
}
sql = "UPDATE users SET balance=balance-" + amount + " WHERE username='" + username + "'";
stmt.executeUpdate(sql);
sql = "INSERT INTO transactions (username, type, amount) VALUES ('" + username + "', '取款', -" + amount + ")";
stmt.executeUpdate(sql);
System.out.println("取款成功!");
}
}
private static boolean usernameExists(String username) throws SQLException {
String sql = "SELECT * FROM users WHERE username='" + username + "'";
rs = stmt.executeQuery(sql);
return rs.next();
}
private static boolean isValidPassword(String password) {
String regex = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(password);
return matcher.matches();
}
}
```
阅读全文