初学java 课程设计ATM机 简易系统(卡片布局)

本文概述了AI音视频处理领域的关键技术,包括视频分割、语义识别、自动驾驶、AR、SLAM等,并探讨了其在实际应用中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人初学java,期末自己写的ATM机系统,已链接mysql数据库,其中check类体现面向对象思想特别明显。其余代码简单易懂,适于初学者借鉴,参考。(PS:当时在做此系统的时候 不小心删错文件了,使得java文件丢失,现在的代码是有打包的class文件反编译回来的,经过调试 和之前代码 一样,只不过前的 注释没有了。再就是链接的数据库是当时自己的路径,用时改下路径就好。)


package com.sunguangxing.atm;

public class AtmTester
{
  public static void main(String[] args)
  {
    AtmWindows atms = new AtmWindows();
    atms.scanWelcome();
  }
}




package com.sunguangxing.atm;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class AtmWindows implements ActionListener{
 
  JFrame frame;
  JPanel start;
  JPanel inputUser;
  JPanel selectMain;
  JPanel searchMoney;
  JPanel tranMoney;
  JPanel getMoney;
  JPanel inputMoney;
  JPanel inputInformation;
  JPanel exitWindow;
 
  JButton continButton;
  JButton exitButton;
  int count = 0;
  JButton btnok;
  JButton btnexit;
  JLabel jblCarId;
  JLabel jblCarPass;
  JTextField txfCardId;
  JPasswordField txfCardPass;//输入密码时候
  JLabel jblErr01;
  JLabel jblErr02;
  String userCarId = "";
  String userCarpwd = "";
  JButton btnGetMoney;
  JButton btnSelectMoney;
  JButton btnSelectList;
  JButton btnTranMoney;
  JButton btnExit1;
  JButton btnBack1;
  JButton btnExit2;
  JTextField txfBanlance;
  JLabel jlInputCarId;
  JLabel jlTranMoney;
  JTextField inputCarId;
  JTextField jtTranMoney;
  JButton btnTranOk;
  JButton btnBack2;
  JLabel labOk;
  JTextField selectList;
  JButton selectOk;
  JButton selectExit;
  JLabel labSelectList;
  JButton btm100;
  JButton btm200;
  JButton btm500;
  JButton btm800;
  JButton btm1000;
  JButton btm2000;
  JButton btmOk;
  JButton btmBack;
 
  String str1;
  Object str2;
  JLabel lab2;
  CardLayout card = new CardLayout();
  JPanel cardPanel;
  Check cac = new Check();

  public void scanWelcome(){
    frame = new JFrame("ATM操作系统");

    showWelcome();
    inputCardUser();
    selectMain();
    balanceSearch();
    tranMoney();
    inputInformation();
    getMoney();

    cardPanel = new JPanel();
    cardPanel.setLayout(card);
    cardPanel.add(start, "start");
    cardPanel.add(inputUser, "inputUser");
    cardPanel.add(selectMain, "selectMain");
    cardPanel.add(searchMoney, "searchMoney");
    cardPanel.add(tranMoney, "tranMoney");
    cardPanel.add(inputInformation, "inputInformation");
    cardPanel.add(getMoney, "getMoney");

    frame.add(cardPanel);
    frame.setBounds(300, 150, 700, 450);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(3);
  }

  public void showWelcome(){
    start = new JPanel();
    JLabel welcom = new JLabel("        欢迎使用ATM机系统!");
    welcom.setFont(new Font("楷体", 1, 40));
    welcom.setAlignmentX(0F);
    continButton = new JButton("继续");
    exitButton = new JButton("退出");

    continButton.addActionListener(this);
    exitButton.addActionListener(this);

    JPanel cunButton = new JPanel();
    cunButton.add(continButton);
    cunButton.add(exitButton);
    start.setLayout(new BorderLayout());
    start.add(welcom, "Center");
    start.add(cunButton, "South");
  }

  public void inputCardUser() {
    inputUser = new JPanel();
    jblCarId = new JLabel("请输入卡号:");
    txfCardId = new JTextField(20);
    jblCarPass = new JLabel("密码(6位数密码):");
    txfCardPass = new JPasswordField(20);
    jblErr01 = new JLabel("");
    btnok = new JButton("确认");
    btnexit = new JButton("退出");
    btnok.addActionListener(this);
    btnexit.addActionListener(this);
    Box boxButton = Box.createVerticalBox();
    boxButton.add(Box.createVerticalStrut(140));
    boxButton.add(btnok);
    boxButton.add(Box.createVerticalStrut(45));
    boxButton.add(btnexit);
    JPanel jpan01 = new JPanel();
    JPanel jpan02 = new JPanel();
    JPanel jpan03 = new JPanel();

    jpan01.setLayout(new FlowLayout());
    jpan01.add(jblCarId);
    jpan01.add(txfCardId);

    jpan02.setLayout(new FlowLayout());
    jpan02.add(jblCarPass);
    jpan02.add(txfCardPass);

    jpan03.setLayout(new FlowLayout());
    jpan03.add(jblErr01);

    JPanel jpan = new JPanel();
    jpan.setLayout(new GridLayout(3, 1));
    jpan.add(jpan01);
    jpan.add(jpan02);
    jpan.add(jpan03);

    inputUser.setLayout(new BorderLayout());

    inputUser.add(jpan, "Center");
    inputUser.add(boxButton, "East");
  }

  public JPanel selectMain() {
    selectMain = new JPanel();
    JLabel labChoice = new JLabel("           业务选择");
    labChoice.setFont(new Font("楷体", 1, 35));
    labChoice.setForeground(Color.BLUE);
    labChoice.setAlignmentX(0F);

    btnSelectMoney = new JButton("余额查询");
    btnTranMoney = new JButton("转    账");
    btnSelectList = new JButton("查询详单");
    btnGetMoney = new JButton("取    款");
    btnExit1 = new JButton("退  出");
    btnSelectMoney.addActionListener(this);
    btnTranMoney.addActionListener(this);
    btnSelectList.addActionListener(this);
    btnGetMoney.addActionListener(this);
    btnExit1.addActionListener(this);

    Box boxButton01 = Box.createVerticalBox();
    boxButton01.add(Box.createVerticalStrut(130));
    boxButton01.add(btnGetMoney);
    boxButton01.add(Box.createVerticalStrut(50));
    boxButton01.add(btnTranMoney);

    Box boxButton02 = Box.createVerticalBox();
    boxButton02.add(Box.createVerticalStrut(130));
    boxButton02.add(btnSelectMoney);
    boxButton02.add(Box.createVerticalStrut(50));
    boxButton02.add(btnSelectList);
    boxButton02.add(Box.createVerticalStrut(50));
    boxButton02.add(btnExit1);

    selectMain.setLayout(new BorderLayout());
    selectMain.add(labChoice, "Center");
    selectMain.add(boxButton01, "West");
    selectMain.add(boxButton02, "East");
    selectMain.setBackground(Color.green);
    return selectMain;
  }

  public void balanceSearch() {
    searchMoney = new JPanel();
    JLabel labBalance = new JLabel("账户余额为:");
    labBalance.setAlignmentX(0F);
    txfBanlance = new JTextField();
    txfBanlance.setEditable(false);
    btnBack1 = new JButton("返回");
    btnExit2 = new JButton("退出");
    btnExit2.addActionListener(this);
    btnBack1.addActionListener(this);

    JPanel jpan = new JPanel();
    JLabel lab1 = new JLabel("  ");
    JLabel lab2 = new JLabel("  ");
    JLabel lab3 = new JLabel("  ");
    JLabel lab4 = new JLabel("  ");
    jpan.add(lab1);
    jpan.add(lab2);
    jpan.add(labBalance);
    jpan.add(txfBanlance);
    jpan.add(lab3);
    jpan.add(lab4);

    Box boxButton01 = Box.createVerticalBox();
    boxButton01.add(Box.createVerticalStrut(130));
    boxButton01.add(btnBack1);
    Box boxButton02 = Box.createVerticalBox();
    boxButton02.add(Box.createVerticalStrut(130));
    boxButton02.add(btnExit2);
    searchMoney.setLayout(new GridLayout(1, 3));
    searchMoney.add(boxButton02, "West");
    searchMoney.add(jpan, "Center");
    searchMoney.add(boxButton01, "East");
  }

  public void tranMoney() {
    tranMoney = new JPanel();
    JLabel lab1 = new JLabel("          请输入转账信息:");
    labOk = new JLabel("");
    labOk.setFont(new Font("楷体", 1, 10));
    labOk.setBackground(Color.BLUE);
    lab1.setFont(new Font("楷体", 1, 30));
    lab1.setAlignmentX(0F);
    jlInputCarId = new JLabel("转出帐号:");
    jlTranMoney = new JLabel("转出金额:");
    inputCarId = new JTextField(40);
    jtTranMoney = new JTextField(40);
    JPanel jpan = new JPanel();
    jpan.add(jlInputCarId);
    jpan.add(inputCarId);
    jpan.add(jlTranMoney);
    jpan.add(jtTranMoney);
    jpan.add(labOk);
    btnTranOk = new JButton("确认");
    btnBack2 = new JButton("返回");
    btnTranOk.addActionListener(this);
    btnBack2.addActionListener(this);
    Box boxButton01 = Box.createVerticalBox();
    boxButton01.add(Box.createVerticalStrut(130));
    boxButton01.add(btnTranOk);

    Box boxButton02 = Box.createVerticalBox();
    boxButton02.add(Box.createVerticalStrut(130));
    boxButton02.add(btnBack2);
    tranMoney.setLayout(new BorderLayout());
    tranMoney.add(lab1, "North");
    tranMoney.add(boxButton02, "West");
    tranMoney.add(jpan, "Center");
    tranMoney.add(boxButton01, "East");
  }

  public void inputInformation()
  {
    inputInformation = new JPanel();
    labSelectList = new JLabel("");
  
    JLabel inputSelectDate = new JLabel("请查询要查询的日期:(格式:年-月-日)");
    inputSelectDate.setAlignmentX(0F);
    inputSelectDate.setFont(new Font("楷体", 1, 30));
    selectList = new JTextField(20);
    labSelectList.setFont(new Font("宋体", 1, 10));
    JPanel jpan1 = new JPanel();
    jpan1.add(inputSelectDate);
    jpan1.add(selectList);
    jpan1.add(labSelectList);
    selectOk = new JButton("确认");
    selectExit = new JButton("返回");
    selectOk.addActionListener(this);
    selectExit.addActionListener(this);
    Box boxButton01 = Box.createVerticalBox();
    boxButton01.add(Box.createVerticalStrut(130));
    boxButton01.add(selectOk);

    Box boxButton02 = Box.createVerticalBox();
    boxButton02.add(Box.createVerticalStrut(130));
    boxButton02.add(selectExit);
    inputInformation.setLayout(new BorderLayout());
    inputInformation.add(boxButton02, "West");
    inputInformation.add(jpan1, "Center");
    inputInformation.add(boxButton01, "East");
  }
  //取款
  public void getMoney() {
    getMoney = new JPanel();
    JLabel lab1 = new JLabel("  请选择选取金额:");
    lab2 = new JLabel("");
    lab2.setFont(new Font("楷体", 1, 20));
    lab2.setAlignmentX(0F);
    lab1.setAlignmentX(0F);
    lab1.setFont(new Font("楷体", 1, 35));
    lab1.setBackground(Color.BLUE);
    btm100 = new JButton("100");
    btm200 = new JButton("200");
    btm500 = new JButton("500");
    btm800 = new JButton("800");
    btm1000 = new JButton("1000");
    btm2000 = new JButton("2000");
    btmOk = new JButton("确认");
    btmBack = new JButton("返回");
    btm100.addActionListener(this);
    btm200.addActionListener(this);
    btm500.addActionListener(this);
    btm800.addActionListener(this);
    btm1000.addActionListener(this);
    btm2000.addActionListener(this);
    btmOk.addActionListener(this);
    btmBack.addActionListener(this);
    Box boxButton01 = Box.createVerticalBox();
    boxButton01.add(Box.createVerticalStrut(130));
    boxButton01.add(btm100);
    boxButton01.add(Box.createVerticalStrut(50));
    boxButton01.add(btm200);
    boxButton01.add(Box.createVerticalStrut(50));
    boxButton01.add(btm500);
    boxButton01.add(Box.createVerticalStrut(50));
    boxButton01.add(btmBack);

    Box boxButton02 = Box.createVerticalBox();
    boxButton02.add(Box.createVerticalStrut(130));
    boxButton02.add(btm800);
    boxButton02.add(Box.createVerticalStrut(50));
    boxButton02.add(btm1000);
    boxButton02.add(Box.createVerticalStrut(50));
    boxButton02.add(btm2000);
    boxButton02.add(Box.createVerticalStrut(50));
    boxButton02.add(btmOk);

    getMoney.setLayout(new BorderLayout());
    getMoney.add(boxButton01, "West");
    getMoney.add(lab1, "Center");
    getMoney.add(boxButton02, "East");
    getMoney.add(lab2, "South");
  }

  public void getAccount(Object e)
  {
    if (e == btm100)
      if (cac.getAccount(userCarId, 100)) {
        lab2.setText("         交易金额为100元");
      } else {
        lab2.setText("        余额不足");
        lab2.setBackground(Color.RED);
      }

    if (e == btm200)
      if (cac.getAccount(userCarId, 200)) {
        lab2.setText("        交易金额为200元");
      } else {
        lab2.setText("        余额不足");
        lab2.setBackground(Color.RED);
      }

    if (e == btm500)
      if (cac.getAccount(userCarId, 500)) {
        lab2.setText("       交易金额为500元");
      } else {
        lab2.setText("       余额不足");
        lab2.setBackground(Color.RED);
      }

    if (e == btm800)
      if (cac.getAccount(userCarId, 800)) {
        lab2.setText("      交易金额为800元");
      } else {
        lab2.setText("      余额不足");
        lab2.setBackground(Color.RED);
      }

    if (e == btm1000)
      if (cac.getAccount(userCarId, 1000)) {
        lab2.setText("      交易金额为1000元");
      } else {
        lab2.setText("      余额不足");
        lab2.setBackground(Color.RED);
      }

    if (e == btm2000)
      if (cac.getAccount(userCarId, 2000)) {
        lab2.setText("     交易金额为2000元");
        lab2.setFont(new Font("楷体", 1, 20));
      } else {
        lab2.setText("     余额不足");
        lab2.setFont(new Font("楷体", 1, 20));
        lab2.setBackground(Color.RED);
      }
  }

  public void actionPerformed(ActionEvent e){
    Object obj = e.getSource();
    if (obj == continButton) {
      card.show(cardPanel, "inputUser");
      txfCardId.requestFocusInWindow();
    }
    //当点的是登录界面的按钮 的时候。
    if (obj == btnok) {
      jblErr01.setText("");
      String pedString = String.valueOf(txfCardPass.getText());
      if ((!(txfCardId.getText().equals(""))) && (!(txfCardPass.getText().equals("")))) {
        if (cac.checkAccountId(txfCardId.getText(),txfCardPass.getText())) {
          userCarId = txfCardId.getText();
          userCarpwd = txfCardPass.getText();
          jblErr01.setText("");
          card.show(cardPanel, "selectMain");
        }else {
          if (count > 2) {
            jblErr01.setText("卡号或者密码输入错误超过三次,系统自动退出!");
            try {
              Thread.sleep(2000);
              card.show(cardPanel, "start");
            }
            catch (InterruptedException e1) {
              e1.printStackTrace();
            }
          }else{
          jblErr01.setText("卡号或者密码不正确!请重新输入!");
          jblErr01.setForeground(Color.RED);
          txfCardId.requestFocusInWindow();
          count += 1;
          }
        }
      }else{
        jblErr01.setForeground(Color.RED);
        jblErr01.setText("卡号或者密码不能为空!请重新输入!");
        txfCardId.requestFocusInWindow();
      }
    }
    //查询余额按钮的时候
    if (obj == btnSelectMoney) {
      double balance = cac.SelectMoney(userCarId);
      txfBanlance.setText(balance + "元");
      card.show(cardPanel, "searchMoney");
    }
    //返回
    if (obj == btnBack1) {
      card.show(cardPanel, "selectMain");
    }

    if (obj == btnTranMoney) {
      card.show(cardPanel, "tranMoney");
      inputCarId.requestFocusInWindow();
    }

    if (obj == btnTranOk) {
      labOk.setText(" ");
      String toCarId = inputCarId.getText();
      String toMoney = jtTranMoney.getText();
      String fromCarId = userCarId;

      if (cac.tranMoney(toCarId, fromCarId, toMoney)) {
        try {
          Thread.sleep(1000L);
          labOk.setText("恭喜你操作成功。   正在跳回刚才界面...");
          card.show(cardPanel, "selectMain");
        }
        catch (InterruptedException e1) {
          e1.printStackTrace();
        }
      }
      else {
        labOk.setText("操作失败!");
        labOk.setFont(new Font("楷体", 1, 35));
        labOk.setBackground(Color.red);
        try {
          Thread.sleep(1000L);
          card.show(cardPanel, "selectMain");
        }
        catch (InterruptedException e1) {
          e1.printStackTrace();
        }
      }

    }

    if (obj == btnSelectList) {
      labSelectList.setText("");
      card.show(cardPanel, "inputInformation");
      selectList.requestFocusInWindow();
    }

    if (obj == selectOk) {
      String getDate = selectList.getText();
      String str = cac.sqlSelectList(userCarId, getDate);
      if (!(str.equals(""))) {
        labSelectList.setText("查询结果为:(日期 汇款 取款 余额)\n" + str);
        labSelectList.setFont(new Font("楷体", 1, 25));
        labSelectList.setBackground(Color.BLUE);
      } else {
        labSelectList.setText("查询记录为空!");
        labSelectList.setFont(new Font("楷体", 1, 25));
        labSelectList.setBackground(Color.red);
      }
    }

    if (obj == selectExit) {
      card.show(cardPanel, "selectMain");
      selectList.setText("");
    }

    if (obj == btnGetMoney) {
      lab2.setText("");
      card.show(cardPanel, "getMoney");
    }

    if ((obj == btmBack) || (obj == btnBack2)) {
      card.show(cardPanel, "selectMain");
    }

    if ((obj == btm100) || (obj == btm200) || (obj == btm500) || (obj == btm800) || (obj == btm1000) ||
      (obj == btm2000)) {
      str2 = obj;
    }

    if (obj == btmOk) {
      getAccount(str2);
      try {
          Thread.sleep(1000);
          card.show(cardPanel, "getMoney");
        }
        catch (InterruptedException e1) {
          e1.printStackTrace();
        }
   
    }
    if ((obj == btnExit1) || (obj == btnexit) || (obj == btnExit2)) {
      card.show(cardPanel, "start");
      cac.commit();
    }
    if (obj == exitButton)
      System.exit(0);
  }
}




package com.sunguangxing.atm;

import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Check{
  ResultSet re = null;

  public boolean checkAccountId(String userCardId, String userCardPwd){
    DAO dao = new DAO();
    String sqlSelectId = "";
    Boolean userFlag=false;
    try{
      sqlSelectId = "select count(*) from usercard where userCardId='" + userCardId + "'and userCardPassword='" + userCardPwd + "'";
      System.out.println("SQL查询语句为:" + sqlSelectId);
      re = dao.executeQuery(sqlSelectId);
      re.next();

      int resoult = re.getInt(1);
      System.out.println("检索的记录数为:" + resoult);

      if (resoult > 0) {
        userFlag=true;
        //System.out.println(userFlag);
       }else
       userFlag=false;
    }
    catch (SQLException e){
      System.out.println(e.getMessage());
      e.printStackTrace();
    }
     return userFlag;
  }

  public double SelectMoney(String userCardId) {
    double balence = 0;
    DAO dao = new DAO();

    String sqlSelectBalance = "select userCardBalence from usercard where userCardId='" + userCardId + "'";
    System.out.println("SQL查询语句为:" + sqlSelectBalance);
    try {
      re = dao.executeQuery(sqlSelectBalance);
      re.next();
      balence = re.getInt(1);
    }
    catch (SQLException e) {
      System.out.println("异常信息:" + e.getMessage());
      e.printStackTrace();
    } finally {
      return balence;
    }
  }

  public synchronized boolean tranMoney(String toCarId, String fromCarId, String toMoney) {
    DAO dao = new DAO();
    boolean tranFlag = false;
    int toMoney1 = Integer.parseInt(toMoney);
    try
    {
      String sqlToCardId = "select count(*) from usercard where userCardId='" + toCarId + "'";
      System.out.println("检测用户是否存在SQL查询语句:" + sqlToCardId);
      re = dao.executeQuery(sqlToCardId);
      re.next();
      int toCardIdCount =re.getInt(1);
      System.out.println("打印出检索数据的个数:" + toCardIdCount);
      if (toCardIdCount > 0) {
        String sqlMoney = "select userCardBalence from usercard where userCardId='" + fromCarId + "'";
        System.out.println("检索操作人用户的余额的SQL查询语句:" + sqlMoney);
        re = dao.executeQuery(sqlMoney);
        re.next();
        int fromMoney =re.getInt(1);
        System.out.println("查询操作人的余额为:" + fromMoney);
        if (fromMoney > toMoney1) {
          String toTranMoney = "update usercard set userCardBalence=userCardBalence+'" + toMoney1 + " 'where userCardId='" + toCarId + "'";
          String fromTranMoney = "update usercard set userCardBalence=userCardBalence-'" + toMoney1 + "' where userCardId='" + fromCarId + "'";
          String tranDate = "insert into useraccount values('" + fromCarId + "',2,now()," + toMoney1 + ",0)";
          System.out.println("SQL更新收款人 账户增加余额" + toTranMoney);
          System.out.println("SQL更新转账人 账户减少余额" + fromTranMoney);
          System.out.println("SQL更新交易明细" + tranDate);
          dao.executeUpdate(toTranMoney);
          dao.executeUpdate(fromTranMoney);
          dao.executeUpdate(tranDate);
          dao.commit();
          tranFlag = true;
        }else
        tranFlag = false;
      }else
      tranFlag = false;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    return tranFlag;
  }

  public String sqlSelectList(String userCardId01, String Date) {
    DAO dao = new DAO();
    String selectList = "";
    try
    {
      String sqlSelectList = "select b.date,b.hui,b.qu,a.userCardBalence from usercard a,useraccount b where b.userCardId01='" +
        userCardId01 + "' " +
        "and b.userCardId01=a.userCardId " +
        "and date_format(date,'%Y-%m-%d')='" + Date + "'";
      System.out.println("SQL查询语句为:" + sqlSelectList);
      re = dao.executeQuery(sqlSelectList);
      re.last();
      ResultSetMetaData rsmd = re.getMetaData();
      int i = re.getRow();
      int a = rsmd.getColumnCount();
      int j = i / 5;
      if (i > 0) {
        for (int x = 1; x <= a * i; ++x) {
          selectList = selectList + re.getString(x);
        }

       }
      selectList = "";
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    label206: System.out.println("明细记录:" + selectList);
    return selectList; }

  public synchronized boolean getAccount(String userCard, int money) {
    DAO dao = new DAO();
    double balence =0;
    boolean getFlag = false;

    String sqlSelectBalance = "select userCardBalence from usercard where userCardId='" + userCard + "'";
    System.out.println("查询余额语句:" + sqlSelectBalance);
    try{
      re = dao.executeQuery(sqlSelectBalance);
      re.next();
      balence = re.getInt(1);
      System.out.println("账户余额为:" + balence);
//      double m=Double.valueOf(money).doubleValue();
//       int m=Integer.parseInt(money);
      if (balence > money) {
      String money1=money+"";
        String fromMoney = "update usercard set userCardBalence=userCardBalence-'" + money1 + "'  where userCardId='" + userCard + "'";
        String tranDate = "insert into useraccount values('" + userCard + "',1,now(),'" + money1 + "',0)";
        System.out.println("SQL转账查询语句:" + fromMoney);
        System.out.println("SQL更新明细语句:" + tranDate);
        dao.executeUpdate(fromMoney);
        dao.executeUpdate(tranDate);
        dao. commit();
        dao.relateColse();
      System.out.println("11111111111");
        getFlag = true;
        }
      else{
      getFlag = false;
      }
      System.out.println(getFlag);
    
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    return getFlag;
  }

//  public void sleep(int i) {
//    try {
//      Thread.sleep(i);
//    }
//    catch (InterruptedException e1) {
//      e1.printStackTrace(); }
//  }

  public void commit() {
    DAO dao = new DAO();
    try {
      dao.commit();
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
  }
}




package com.sunguangxing.atm;

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DAO{
  String driver = "com.mysql.jdbc.Driver";
  String url = "jdbc:mysql://localhost:3306/accounttext";
  String userName = "root";
  String password = "123";
  private Connection conn = null;
  private Statement stat = null;
  private ResultSet re = null;

  public DAO(){
    try{
      Class.forName(driver);
      System.out.println("加载驱动成功!");
      conn = DriverManager.getConnection(url, userName, password);
      conn.setAutoCommit(false);
      System.out.println("连接数据库成功!");
    } catch (ClassNotFoundException e) {
      System.out.println("异常信息:" + e.getMessage());
      e.printStackTrace();
    } catch (SQLException e) {
      System.out.println("异常信息:" + e.getMessage());
      e.printStackTrace();
    }
  }

  public ResultSet executeQuery(String sql) throws SQLException{
    try {
      stat = conn.createStatement();
      re = stat.executeQuery(sql);
    } catch (SQLException e) {
      System.out.println("异常信息:" + e.getMessage());
      conn.rollback();
      e.printStackTrace();
      throw e;
    }
    return re;
  }

  public int executeUpdate(String sql) throws SQLException {
    int count = 0;
    try{
      stat = conn.createStatement();
      count = stat.executeUpdate(sql);
    
    } catch (SQLException e) {
      System.out.println("异常信息:" + e.getMessage());
      conn.rollback();
      e.printStackTrace();
      throw e;
    }
    return count;
  }

  public void commit() throws SQLException {
    try {
      conn.commit();
    } catch (SQLException e) {
      System.out.println("异常信息:" + e.getMessage());
      conn.rollback();
      e.printStackTrace();
      throw e;
    } finally {
    }
  }

  public void relateColse() {
    try {
      stat.close();
      if (conn == null) return;
      conn.close();
    } catch (SQLException e) {
      System.out.println("异常信息:" + e.getMessage());
      e.printStackTrace();
    }
  }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值