银行转账java mysql_如何编写一个银行转账案例?

今天是刘小爱自学Java的第70天。

感谢你的观看,谢谢你。

话不多说,开始今天的学习:

转账,相信基本都接触过,无论是线下用银行卡转账还是线上用手机转账,本质上都是差不多的。

一、转账案例

需要两个账户:刘小爱账户和刘妈妈账户;刘小爱账户转出;刘妈妈账户转入。

刘小爱要给刘妈妈转账,毕竟要攒着将来娶媳妇,emm……就转1000吧(看来是娶不到媳妇了……)

在数据库中用sql语句编写一个账户信息表,同时也是对sql语法的一次回顾:

当然,账户信息表肯定是在银行的数据库中的,并且是最重要的一个存在,会严加防范。

我这边只是模拟这个案例,所以在自己的数据库上创建了一个账户信息表。

表很简单:主要两个属性,账户名和账户余额。

现在编写代码:

刘小爱账户给刘妈妈账户转1000

刘小爱账户余额减少1000

刘妈妈账户余额增加1000

根据代码可拓展性原则,并未将账户名,转账金额写死。

提示用户输入信息

这个本来是应该在网页上面显示信息提醒用户的,但是这块还不会,就用Java中的控制台来模拟。

也就是Scanner这个类的使用。

从德鲁伊连接池中获取连接

这个我们昨天将德鲁伊连接池封装进工具类JdbcUtil了,所以可以直接用工具类获取连接。

转出账户预编译

转出账户名为inName,转出的金额为money。

那么要将数据库中对应账户名的mone

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是用Java编写一个简单的银行转账系统,包括取款,存款,转账等功能,其中用到了数据库的连接,采用Eclipse编写,包含数据库的设计文件。非常适合有一定基础的Java初学者使用。 package com.gujunjia.bank; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.sql.*; /** * * @author gujunjia */ public class DataBase { static Connection conn; static PreparedStatement st; static ResultSet rs; /** * 加载驱动 */ public static void loadDriver() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("加载驱动失败"); } } /** * 创建数据库的连接 * * @param database * 需要访问的数据库的名字 */ public static void connectionDatabase(String database) { try { String url = "jdbc:mysql://localhost:3306/" + database; String username = "root"; String password = "gujunjia"; conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { System.out.println(e.getMessage()); } } /** * 关闭数据库连接 */ public static void closeConnection() { if (rs != null) { // 关闭记录集 try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { // 关闭声明 try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { // 关闭连接对象 try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } package com.gujunjia.bank; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 本类主要实现整个系统的界面 * * @author gujunjia */ public class MainFrame extends JFrame implements ActionListener, FocusListener { /** * */ private static final long serialVersionUID = 1L; public static String userId; JTextField userIdText; JPasswordField passwordText; JButton registerButton; JButton logInButton; public MainFrame() { super("个人银行系统"); this.setSize(400, 500); this.setLocation(getMidDimension(new Dimension(400, 500))); getAppearance(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * 获取屏幕的中间尺寸 * * @param d * Dimension类型 * @return 一个Point类型的参数 */ public static Point getMidDimension(Dimension d) { Point p = new Point(); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); p.setLocation((dim.width - d.width) / 2, (dim.height - d.height) / 2); return p; } /** * 布局 * * @return Container */ public Container getAppearance() { Container container = this.getContentPane(); container.setLayout(new GridLayout(4, 0)); JLabel label1 = new JLabel("个人银行系统"); label1.setFont(new Font("楷体", Font.BOLD, 40)); JLabel label2 = new JLabel("账号:"); label2.setFont(new Font("楷体", Font.PLAIN, 15)); JLabel label3 = new JLabel("密码:"); label3.setFont(new Font("楷体", Font.PLAIN, 15)); userIdText = new JTextField(20); userIdText.addFocusListener(this); passwordText = new JPasswordField(20); passwordText.addFocusListener(this); JPanel jp1 = new JPanel(); JPanel jp2 = new JPanel(); JPanel jp3 = new JPanel(); JPanel jp4 = new JPanel(); jp1.add(label1); jp2.add(label2); jp2.add(userIdText); jp3.add(label3); jp3.add(passwordText); registerButton = new JButton("注册"); registerButton.addActionListener(this); registerButton.setFont(new Font("楷体", Font.BOLD, 15)); logInButton = new JButton("登录"); logInButton.addActionListener(this); logInButton.setFont(new Font("楷体", Font.BOLD, 15)); jp4.add(registerButton); jp4.add(logInButton); container.add(jp1); container.add(jp2); container.add(jp3); container.add(jp4); return container; } public void actionPerformed(ActionEvent e) { Object btn = e.getSource(); if (btn == registerButton) { new Register(); } else if (btn == logInButton) { String id = userIdText.getText().trim(); String password = new String(passwordText.getPassword()); Bank bank = new Bank(); if (id.equals("") || password.equals("")) { JOptionPane.showMessageDialog(null, "请输入账号和密码"); } else { String dPassword = bank.getPassword(id); if (password.equals(dPassword)) { userId = id; this.dispose(); new UserGUI(); } else { JOptionPane.showMessageDialog(this, "密码或用户名错误", "错误", JOptionPane.ERROR_MESSAGE); } } } } @Override public void focusGained(FocusEvent e) { Object text = e.getSource(); if (text == userIdText) { userIdText.setText(""); userIdText.setFont(new Font("宋体", Font.BOLD, 15)); } else if (text == passwordText) { passwordText.setText(""); } } @Override public void focusLost(FocusEvent e) { Object text = e.getSource(); if (text == userIdText) { if (userIdText.getText().equals("")) { userIdText.setText("请输入账号"); userIdText.setFont(new Font("楷体", Font.ITALIC, 15)); } } } }
好的,下面是 Java Web 实现银行转账的代码示例: 首先,需要创建一个 TransferServlet 类来处理转账请求。在该类中,我们需要实现 doPost 方法以处理 POST 请求: ```java import java.io.IOException; import java.math.BigDecimal; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TransferServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fromAccount = request.getParameter("fromAccount"); String toAccount = request.getParameter("toAccount"); BigDecimal amount = new BigDecimal(request.getParameter("amount")); // 调用银行转账服务进行转账 BankService bankService = new BankService(); boolean success = bankService.transfer(fromAccount, toAccount, amount); // 根据转账结果返回不同的响应 if (success) { response.getWriter().write("转账成功!"); } else { response.getWriter().write("转账失败!"); } } } ``` 在 doPost 方法中,我们从请求参数中获取转账相关的信息,然后调用一个 BankService 类的 transfer 方法进行转账转账成功后,我们返回一个简单的成功或失败的消息。 接下来,我们需要实现 BankService 类来处理转账逻辑。在该类中,我们需要实现 transfer 方法来完成转账操作: ```java import java.math.BigDecimal; public class BankService { public boolean transfer(String fromAccount, String toAccount, BigDecimal amount) { // 检查账户余额是否充足 BigDecimal balance = getBalance(fromAccount); if (balance.compareTo(amount) < 0) { return false; } // 扣除转出账户的余额 BigDecimal newBalance = balance.subtract(amount); updateBalance(fromAccount, newBalance); // 增加转入账户的余额 balance = getBalance(toAccount); newBalance = balance.add(amount); updateBalance(toAccount, newBalance); return true; } private BigDecimal getBalance(String account) { // 查询账户余额 // 省略具体实现 } private void updateBalance(String account, BigDecimal newBalance) { // 更新账户余额 // 省略具体实现 } } ``` 在 transfer 方法中,我们首先检查转出账户的余额是否充足。如果余额不足,则转账失败。否则,我们扣除转出账户的余额,并增加转入账户的余额。最后,我们返回转账成功的消息。 以上就是 Java Web 实现银行转账的代码示例。当然,这只是一个简单的示例,实际场景中还需要考虑更多的因素,比如并发操作、事务处理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值