学生成绩管理系统(二)

3.2 数据库连接类

        我们先写一个数据库连接类,测试一下和数据库的连接。这里使用Proxool配置了一个数据库连接池,简单地使用了Log4J。

package org.ygy.util;

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

import org.apache.log4j.Logger;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;

public class DBUtil {
	private static Logger logger = Logger.getLogger(DBUtil.class);
	
	static {
		try {
			PropertyConfigurator.configure("src/org/ygy/configuration/proxool.properties");
		} catch (ProxoolException e) {
			e.printStackTrace();
		} 
	}
	private DBUtil() {
		
	}
	
	public static Connection getConnection() {
		logger.info("getConection() start.");
		Connection con = null;
		
		try {
			con = DriverManager.getConnection("proxool.stu_pool");
			logger.info("getConnection() ok");
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
		
		return con;
	}
	
	public static void close(Connection con) {
		if(con != null) {
			try {
				con.close();
				con = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void close(Connection con , PreparedStatement pstmt) {
		if(pstmt != null) {
			try {
				pstmt.close();
				pstmt = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		
		close(con);
	}
	
	public static void close(Connection con , PreparedStatement pstmt , ResultSet rs) {
		if(rs != null) {
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		close(con , pstmt);
	}
	
}


 


 Proxool的配置文件:

jdbc-0.proxool.alias=stu_pool
jdbc-0.proxool.driver-url=jdbc:mysql://localhost/StudentManageSystem
jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver
jdbc-0.user=root
jdbc-0.password=5210
jdbc-0.proxool.maximum-connection-count=50
jdbc-0.proxool.minimum-connection-count=5
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE


 我们测试一下:

@Test
	public void testGetConnection() {
		Connection con = DBUtil.getConnection();
		
		assertNotNull(con);
		DBUtil.close(con);
	}


 3.3 登录模块-管理员

        我们先写一个登录界面:先实现了管理员的登录,对界面不是很擅长,简单地做了一个。

注:数据库中的密码,没有进行处理,都是明文。


图1 登录界面

 

package org.ygy.view;

import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.ygy.exception.ServiceException;
import org.ygy.service.IAdminService;
import org.ygy.service.impl.AdminService;

/**
 * 登录时的选择登录类型界面
 * 
 * @author Administrator
 *
 */
public class StartFrame extends JFrame {
	private static final long serialVersionUID = -1264932141929172778L;
	
	private Container container = null;
	private JPanel inputPanel = null;
	private JPanel buttonPanel = null;
	private JPanel typePanel = null;
	private JLabel idLabel = null;
	private JLabel pwdLabel = null;
	private JLabel typeLabel = null;
	private JRadioButton studentRadio = null;
	private JRadioButton adminRadio = null;
	private JTextField idText = null;
	private JPasswordField pwdText = null;
	private JButton loginButton = null;
	private JButton resetButton = null;
	
	private int width = 460;
	private int height = 270;
	private int locationX = 0;
	private int locationY = 0;
	
	public StartFrame() {
		setTitle("登录界面");
		
		//设置居中显示
		locationX = (Toolkit.getDefaultToolkit().getScreenSize().width - width) / 2;
		locationY = (Toolkit.getDefaultToolkit().getScreenSize().height - height) / 2;
		setBounds(locationX, locationY , width , height);
		
		container = this.getContentPane();
		container.setLayout(null);
		
		initialInputPanel();
		initialRadioPanel();
		initialButtonPanel();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	/**
	 * 初始化输入面板
	 */
	private void initialInputPanel() {
		inputPanel = new JPanel();
		inputPanel.setLayout(null);
		inputPanel.setBounds(0 , 0 , width , height * 2 / 4);
		
		idLabel = new JLabel("账号:");
		pwdLabel = new JLabel("密码:");
		idText = new JTextField(10);
		pwdText = new JPasswordField(10);
		
		inputPanel.add(idLabel);
		inputPanel.add(idText);
		inputPanel.add(pwdLabel);
		inputPanel.add(pwdText);
		
		container.add(inputPanel);
	}
	
	/**
	 * 初始化按钮面板
	 */
	private void initialButtonPanel() {
		buttonPanel =  new JPanel();
		buttonPanel.setLayout(null);
		buttonPanel.setBounds(0 ,  height * 3 / 4 , width ,  height/ 4);

		loginButton = new JButton("登录");
		loginButton.addActionListener(new LoginListener());
		resetButton = new JButton("重置");
		resetButton.addActionListener(new LoginListener());

		idLabel.setBounds(50, 50, 30, 30);
		pwdLabel.setBounds(50, 100, 30, 30);
		
		idText.setBounds(150, 50, 170, 30);
		pwdText.setBounds(150, 100, 170, 30);
		
		loginButton.setBounds(70, 0, 130, 30);
		resetButton.setBounds(230 ,0 ,  130 , 30);
		
		buttonPanel.add(loginButton);
		buttonPanel.add(resetButton);
		
		container.add(buttonPanel);
	}
	
	/**
	 * 初始化单选面板
	 */
	private void initialRadioPanel() {
		studentRadio = new JRadioButton("学生");
		studentRadio.setBounds(200 , 17 , 80 ,  height/ 8);
		studentRadio.setSelected(true);
		
		adminRadio = new JRadioButton("管理员");
		adminRadio.setBounds(300 , 17 , 100 ,  height/ 8);
		
		ButtonGroup loginGroup = new ButtonGroup();
		loginGroup.add(studentRadio);
		loginGroup.add(adminRadio);
		
		typeLabel = new JLabel("请选择登录类型:");
		typeLabel.setBounds(50 , 0, 100 ,  height/ 4);
		
		typePanel = new JPanel();
		typePanel.setBounds(0 ,  height * 2 / 4 , width ,  height/ 4);
		typePanel.setLayout(null);
		typePanel.add(typeLabel);
		typePanel.add(studentRadio);
		typePanel.add(adminRadio);
		
		container.add(typePanel);
	}
	
	/**
	 * 按钮的监听器
	 * 
	 * @author Administrator
	 *
	 */
	class LoginListener implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			
			if("登录".equals(e.getActionCommand())) {
				if(validateNotNull()) {
					if(studentLogin()) {
						System.out.println("su login");
					} else {
						IAdminService adminService = new AdminService();
						try {
							adminService.login(idText.getText(), new String(pwdText.getPassword()));
							StartFrame.this.setVisible(false);
							StartFrame.this.dispose();
						} catch (ServiceException e1) {
							display(e1.getMessage() , "错误提示");
						}
					}
				}
				
			} else if("重置".equals(e.getActionCommand())) {
				clearInput();
			}
		}
		
	}
	
	/**
	 * 清空输入信息
	 */
	private void clearInput() {
		idText.setText("");
		pwdText.setText("");
		studentRadio.setSelected(true);
	}
	
	/**
	 * 验证登录信息不为空
	 * 
	 * @return
	 */
	private boolean validateNotNull() {
		boolean result = true;
		
		String id = idText.getText();
		String password = new String(pwdText.getPassword());
		
		if(id == null || "".equals(id.trim())) {
			display("账号不能为空!" , "错误提示");
			result = false;
		} else if(password == null || "".equals(password.trim())) {
			display("密码不能为空" , "错误提示");
			result = false;
		}
		
		return result;
	}
	
	private boolean studentLogin() {
		boolean result = false;
		
		if(studentRadio.isSelected()) {
			result = true;
		}
		
		return result;
	}
	
	/**
	 * 显示提示框,显示信息
	 * 
	 * @param message	要显示的内容
	 * @param title		提示框标题
	 */
	private void display(String message , String title) {
		JOptionPane.showMessageDialog(container, message, title, JOptionPane.INFORMATION_MESSAGE);
	}
	
	public static void main(String[] args) {
		new StartFrame();
	}
}


package org.ygy.service;

import org.ygy.exception.ServiceException;

public interface IAdminService {
	
	public void login(String name , String password) throws ServiceException;
}



 

package org.ygy.service.impl;

import org.apache.log4j.Logger;
import org.ygy.dao.IAdminDao;
import org.ygy.dao.impl.AdminDao;
import org.ygy.exception.DaoException;
import org.ygy.exception.ServiceException;
import org.ygy.model.Administrator;
import org.ygy.service.IAdminService;

public class AdminService implements IAdminService {
	private Logger logger = Logger.getLogger(AdminService.class);
	
	private IAdminDao adminDao = null;
	
	public AdminService () {
		adminDao = new AdminDao();
	}
	
	@Override
	public void login(String name, String password) throws ServiceException {
		try {
			Administrator admin = adminDao.findByName(name);
			
			if(admin == null) {
				logger.debug("账户不存在!");
				throw new ServiceException("该账户不存在!");
			} else {
				if(password.equals(admin.getPassword())) {
					logger.debug("成功登录!");
					return;
				} else {
					logger.debug("密码不正确!");
					throw new ServiceException("密码不正确!");
				}
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException(e.getMessage());
		}
	}

}


package org.ygy.dao;

import org.ygy.exception.DaoException;
import org.ygy.model.Administrator;

public interface IAdminDao {
	
	public Administrator findByName(String name) throws DaoException;
}


package org.ygy.dao.impl;

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

import org.apache.log4j.Logger;
import org.ygy.dao.IAdminDao;
import org.ygy.exception.DaoException;
import org.ygy.model.Administrator;
import org.ygy.util.DBUtil;

public class AdminDao implements IAdminDao {
	private Logger logger = Logger.getLogger(AdminDao.class);
	private Connection con = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	
	@Override
	public Administrator findByName(String name) throws DaoException {
		String sql = "select password from t_administrator where name=?";
		Administrator admin = null;
		
		con = DBUtil.getConnection();
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1 , name);
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				admin = new Administrator();
				admin.setName(name);
				admin.setPassword(rs.getString("password"));
			}
			
		} catch (SQLException e) {
			logger.debug(e.getMessage());
			throw new DaoException(e.getMessage());
		} finally {
			DBUtil.close(con, pstmt, rs);
		}
		
		return admin;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值