pagedao.java

来自「本方案是在与***信息技术(北京)有限公司相关人员协商并分析了北京****投资有」· Java 代码 · 共 109 行

JAVA
109
字号
package com.test.dao;

import com.test.dao.pool.*;
import com.test.dao.userpage.*;
import com.test.model.LoginBean;
import com.test.model.RoleBean;
import com.test.model.UserBean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class PageDAO {
	private ConnectionPool connectionPool;
	private Connection connection;

	public PageDAO() {
		try {
			connectionPool = ConnectionPool.getConnectionPool();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	private Connection getConnection() {
		try {
			return connectionPool.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("无法得到数据库连接");
		}
	}

	private void closeConnection() {
		try {
			this.connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public IPage getDataByPageIndex(int pageIndex) {
		try {
			// 使用pageManage得到数据并返回
			PageManage pm = PageManage.newInstance(PageManage.USERBEAN, 10);
			this.connection = this.getConnection();
			return pm.queryByPageIndex(connection, pageIndex);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			this.closeConnection();
		}
	}
	
	/**
	 * 查询多个管理员
	 * @param sqlText sql查询字符串
	 * @return 包含多个用户的集合
	 */
	public List<LoginBean> query(String sqlText) {
		try {
			connection = this.getConnection();
			Statement stat = connection.createStatement();
			ResultSet rs = stat.executeQuery(sqlText);
			List<LoginBean> admins = new java.util.ArrayList<LoginBean>();
			while (rs.next()) {
				LoginBean user = new LoginBean();
				user.setUser_id(rs.getInt(1) );
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setLogout( rs.getBoolean(4));
				user.setState_pwf(rs.getBoolean(5));
				user.setState_lb(rs.getBoolean(6));
				admins.add(user);
			}
			rs.close();
			stat.close();
			return admins;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("查询管理员失败,请重试");
		} finally {
			this.closeConnection();
		}
	}
	
	/**
	 * 更新数据的方法
	 * @param sqlText  更新字符串sql
	 */
	public void update( String sqlText) {
		try {
			connection = this.getConnection();
			Statement stat = connection.createStatement();
			stat.executeUpdate(sqlText);
			stat.close();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("数据更新失败,请重试");
		} finally {
			this.closeConnection();
		}
	}
	
}

⌨️ 快捷键说明

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