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

📄 accountdao.java

📁 中国移动管理系统
💻 JAVA
字号:
package com.chinamobile.dao;

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

import com.chinamobile.bean.AccountBean;
import com.chinamobile.bean.CustomerBean;

public class AccountDAO {

	public AccountBean findAccountByID(String ID) {
		String find = "select * from TAccount where Account_ID=?";
		AccountBean account = null;
		SQLServerDAOFactory dao = new SQLServerDAOFactory();
		Connection conn = dao.getConnection();
		ResultSet res = null;
		PreparedStatement pstmt = null;

		try {
			pstmt = conn.prepareStatement(find);
			pstmt.setString(1, ID);
			res = pstmt.executeQuery();
			if (res.next()) {
				account = new AccountBean();
				account.setAccount_ID(res.getString(1));
				account.setContact_Person(res.getString(2));
				account.setContact_Address(res.getString(3));
				account.setAccount_Balance(res.getDouble(4));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			dao.closeConnection(conn);
			dao.closeResultSet(res);
			dao.closeStatement(pstmt);
		}
		return account;

	}

	public void addAccount(AccountBean ac) {
		String add = "insert into TAccount values(?,?,?,?)";
		SQLServerDAOFactory dao = new SQLServerDAOFactory();
		Connection conn = dao.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(add);
			pstmt.setString(1, ac.getAccount_ID());
			pstmt.setString(2, ac.getContact_Person());
			pstmt.setString(3, ac.getContact_Address());
			pstmt.setDouble(4, ac.getAccount_Balance());
			pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			dao.closeConnection(conn);
			dao.closeStatement(pstmt);
		}

	}

	public void addDelta(AccountBean ac) {
		String update = "update TAccount set Account_Balance = ? where Account_ID = ?";
		String get = "select Account_Balance from TAccount where Account_ID = ?";
		String getdelta = "select Charge_Money from TCharge where Charge_Code = ?";
		SQLServerDAOFactory dao = new SQLServerDAOFactory();
		Connection conn = dao.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		double delta = 0.0;
		try {
			pstmt = conn.prepareStatement(get);
			pstmt.setString(1, ac.getAccount_ID());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				ac.setAccount_Balance(rs.getDouble(1));
			}
			dao.closeStatement(pstmt);
			dao.closeResultSet(rs);

			pstmt = conn.prepareStatement(getdelta);
			pstmt.setString(1, "A");
			rs = pstmt.executeQuery();
			if (rs.next()) {
				delta = rs.getDouble(1);
			}
			dao.closeStatement(pstmt);
			dao.closeResultSet(rs);
			pstmt = conn.prepareStatement(update);
			pstmt.setDouble(1, ac.getAccount_Balance() - delta);
			pstmt.setString(2, ac.getAccount_ID());
			pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			dao.closeConnection(conn);
		}

	}

	public void updateAccount(AccountBean ac) {
		String add = "update TAccount set Contact_Person = ?,Contact_Address=?,Account_Balance=? where Account_ID=?";
		SQLServerDAOFactory dao = new SQLServerDAOFactory();
		Connection conn = dao.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(add);
			pstmt.setString(4, ac.getAccount_ID());
			pstmt.setString(1, ac.getContact_Person());
			pstmt.setString(2, ac.getContact_Address());
			pstmt.setDouble(3, ac.getAccount_Balance());
			pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			dao.closeConnection(conn);
			dao.closeStatement(pstmt);
		}

	}

}

⌨️ 快捷键说明

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