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

📄 customerdbdao.java

📁 纯JAVA代码的考试系统
💻 JAVA
字号:
package org.fangsoft.testcenter.dao.db;

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

import org.fangsoft.testcenter.dao.CustomerDao;
import org.fangsoft.testcenter.model.Customer;

public class CustomerDBDao implements CustomerDao {
	private static final CustomerDBDao cdao=new CustomerDBDao();
	public static final CustomerDBDao getInstance(){
		return cdao;
	}

	private void close(Connection conn) {
		if (conn != null)
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}

	//implement findByUid
	private static final String sql_findByUid = "select * from CUSTOMER where UESID=?";

	public Customer findByUid(String uid) {
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql_findByUid);
			ps.setString(1, uid);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				Customer cu = new Customer();
				cu.setUserId(rs.getString("UESID"));
				cu.setPassword(rs.getString("PASSWORD"));
				cu.setEmail(rs.getString("EMAIL"));
				return cu;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return null;
	}

	//implement insert
	private static final String sql_insert = "inert into CUSTOMER(UESID,PASSWORD,EMAIL) values(?,?,?)";

	public void insert(Customer customer) {
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql_insert);
			ps.setString(1, customer.getUserId());
			ps.setString(2, customer.getPassword());
			ps.setString(3, customer.getEmail());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			this.close(conn);
		}

	}

	//implement update
	private static final String sql_update = "update CUSTOMER set PASSWORD=?,EMAIL=? where UESID=?";

	public void update(Customer customer) {
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql_update);
			ps.setString(1, customer.getPassword());
			ps.setString(2, customer.getEmail());
			ps.setString(3, customer.getUserId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			this.close(conn);
		}
	}

	//implement delete
	private static final String sql_delete = "delete from CUSTOMER where UESID=?";

	public void delete(String uid) {
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql_delete);
			ps.setString(1, uid);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			this.close(conn);
		}
	}
	public Customer login(String userId, String pass) {
		Customer c=this.findByUid(userId);
		if(c!=null && c.getPassword().equals(pass))return c;
		return null;
	}
}

⌨️ 快捷键说明

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