⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recorddao.java~13~

📁 我自己在做java训练的时候做的一个小项目
💻 JAVA~13~
字号:
package text;


import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class RecordDAO {
    public RecordDAO() {
    }
      public UpdataTable getStopCompouter(Record record) {
        UpdataTable result = new UpdataTable();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet res = null;

        try {
            con = JDBC_ODBC.getConnction();
               String Sql =
                    "select r.Id,r.cardId,r.computerId,r.BeginTime,c.Balance"
                    + " from Record r, Card c where r.ComputerId =" +
                    record.getComputerId()
                    + " and r.EndTime is null and r.CardId = c.Id";
                 pstmt =con.prepareStatement(Sql);
             res = pstmt.executeQuery();
             if (res.next()) {

                record.setId(res.getInt(1));
                record.setCardId(res.getString(2));
                record.setComputerId(res.getString(3));
                record.setBeginTime(res.getString(4));

                Card card = new Card();
                result.setCard(card);
                result.setRecord(record);

            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        }
        return result;
    }

    public void UpdateRecord(Record record,
                                             Card card) {

        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = JDBC_ODBC.getConnction();
         //从卡里扣款
            String updatCardBalanceSql =
          "update Card set balance =(?) where id=(?) ; ";
          pstmt = con.prepareStatement(updatCardBalanceSql);
          pstmt.setInt(1, card.getBalance());
          pstmt.setString(2, card.getId());
          pstmt.executeUpdate();

             //修改上机记录
            String updataSql =
                    "update Record set endTime =(?), fee =(?) where id=(?) ; ";
            pstmt = con.prepareStatement(updataSql);
            pstmt.setString(1, record.getEndTime());
            pstmt.setInt(2, record.getFee());
            pstmt.setInt(3, record.getId());
            pstmt.executeUpdate();

            //修改机器为未使用状态
            String updatComputerSql =
                    "update Computer set OnUse = 0 where id=(?) ; ";
            pstmt = con.prepareStatement(updatComputerSql);
            pstmt.setString(1, record.getComputerId());
            pstmt.executeUpdate();

            con.commit();
            con.setAutoCommit(true);

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
            try {
                con.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -