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

📄 persondaoimpl.java

📁 简易的java问答系统
💻 JAVA
字号:
package cn.netjava.dao.impl;

import java.sql.*;

import cn.netjava.dbc.*;
import cn.netjava.pojo.*;
import cn.netjava.dao.*;

/**
 * 本类以Oracle 为例, 具体实现数据库中表的CRUD操作
 * 
 * @author XieChengmin
 * 
 */
public class PersonDAOImpl implements PersonDAO {
	// 增加操作
	public boolean add(Person p) {
		boolean inserted = false;

		String sql = "insert into person values(seq_personId.nextval, ?, ?, ?)";
		Connection con = DBConnection.getConn();
		PreparedStatement pst = null;

		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, p.getName());
			pst.setString(2, p.getPassword());
			pst.setString(3, p.getDetail());
			// 执行插入语句
			pst.execute();
			inserted = true;
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("ADD 方法: 数据连接创建失败或SQL语句有误");
		} finally {
			try {
				if (pst != null) {
					pst.close();
				}
				if (con != null) {
					// 放回连接池
					con.close();
				}
			} catch (SQLException e) {
				System.out.println("数据连接关闭失败");
			}
		}
		return inserted;
	}

	// 检测数据库中是否存在同名用户
	public boolean existSameUser(String name) {
		Connection con = DBConnection.getConn();
		String sql = "select * from person where name = ?";
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, name);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (pst != null) {
					pst.close();
				}
				if (con != null) {
					// 放回连接池
					con.close();
				}
			} catch (SQLException e) {
				System.out.println("数据连接关闭失败");
			}
		}
		return false;
	}

	// 根据name查询
	public Person queryByName(String name) {
		Person p = new Person();
		String sql = "select * from person where name = ?";
		Connection con = DBConnection.getConn();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql);
			// 执行查询语句
			pst.setString(1, name);

			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				p.setId(rs.getInt(1));
				p.setName(rs.getString(2));
				p.setPassword(rs.getString(3));
				p.setDetail(rs.getString(4));
			} else {
				return null;
			}
		} catch (SQLException e) {
			System.out.println("数据连接创建失败或查询SQL语句有误");
		} finally {
			try {
				if (pst != null) {
					pst.close();
				}
				if (con != null) {
					// 放回连接池
					con.close();
				}
			} catch (SQLException e) {
				System.out.println("数据连接关闭失败");
			}
		}

		return p;
	}

	public Person querybyId(int id) {
		Person p = new Person();
		String sql = "select * from person where id = ?";
		Connection con = DBConnection.getConn();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql);
			// 执行查询语句
			pst.setInt(1, id);

			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				p.setId(rs.getInt(1));
				p.setName(rs.getString(2));
				p.setPassword(rs.getString(3));
				p.setDetail(rs.getString(4));
			} else {
				return null;
			}
		} catch (SQLException e) {
			System.out.println("数据连接创建失败或查询SQL语句有误");
		} finally {
			try {
				if (pst != null) {
					pst.close();
				}
				if (con != null) {
					// 放回连接池
					con.close();
				}
			} catch (SQLException e) {
				System.out.println("数据连接关闭失败");
			}
		}

		return p;
	}

	// 检测是否为合法用户
	public boolean isValidate(String name, String password) {
		Person p = queryByName(name);
		if (null != password && password.equals(p.getPassword())) {
			return true;
		}
		return false;
	}

}

⌨️ 快捷键说明

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