accountdaoimpl.java

来自「是我自己在做java训练的时候做的一个小项目」· Java 代码 · 共 88 行

JAVA
88
字号
package com.magic.mobile.dao.impl;

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

import com.magic.mobile.dao.AccountDAO;
import com.magic.mobile.exception.NotFoundAccountException;
import com.magic.mobile.util.DBUtil;
import com.magic.mobile.vo.Account;

public class AccountDAOImpl implements AccountDAO {
	
	//更新帐户
	public boolean updateAccount(Account account){
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "update taccount set contact_person=?,contact_address=?,account_balance=? where account_id=?";
		try {
			conn = DBUtil.getConn();
			pstmt = DBUtil.getpStmt(conn, sql);
			pstmt.setString(1,account.getContactPerson());
			pstmt.setString(2, account.getContactAddress());
			pstmt.setDouble(3, account.getAccountBalance());
			pstmt.setLong(4, account.getAccountId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			return false;
		} finally {
			DBUtil.closeStmt(pstmt);
			DBUtil.closeConn(conn);
		}
	
	}

	// 添加帐户
	public boolean addAccount(Account account) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "insert into taccount values(?,?,?,?)";
		try {
			conn = DBUtil.getConn();
			pstmt = DBUtil.getpStmt(conn, sql);
			pstmt.setLong(1, account.getAccountId());
			pstmt.setString(2, account.getContactPerson());
			pstmt.setString(3, account.getContactAddress());
			pstmt.setDouble(4, account.getAccountBalance());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			return false;
		} finally {
			DBUtil.closeStmt(pstmt);
			DBUtil.closeConn(conn);
		}
	}

	// 查询帐户
	public Account selectAccount(Account account) throws NotFoundAccountException {
		Connection conn = null;
		ResultSet rs = null;
		String sql = "select * from taccount where account_id="+account.getAccountId();
		try {
			conn = DBUtil.getConn();
			rs = DBUtil.getRs(conn, sql);
			if (rs.next()) {
				// 帐户存在
				account.setAccountId(rs.getLong(1));
				account.setContactPerson(rs.getString(2));
				account.setContactAddress(rs.getString(3));
				account.setAccountBalance(rs.getDouble(4));
			} else {
				// 帐户不存在
				throw new NotFoundAccountException();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeRs(rs);
			DBUtil.closeConn(conn);
		}
		return account;
	}

}

⌨️ 快捷键说明

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