首页 > 代码库 > 学生管理系统----学生连接数据库类

学生管理系统----学生连接数据库类

StudentDB.java:


/**
 * @Title:StudentDB.java
 * @Package:com.you.student.system.utils
 * @Description:
 * @author:Youhaidong(游海东)
 * @date:2014-6-16 下午11:20:51
 * @version V1.0
 */
package com.you.student.system.utils;

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

import com.you.student.system.model.Student;

/**
 * 类功能说明
 * 类修改者 修改日期
 * 修改说明
 * <p>Title:StudentDB.java</p>
 * <p>Description:游海东个人开发</p>
 * <p>Copyright:Copyright(c)2013</p>
 * @author:游海东
 * @date:2014-6-16 下午11:20:51
 * @version V1.0
 */
public class StudentDB 
{
	private Connection conn = null;
	private PreparedStatement psmt = null;
	@SuppressWarnings("unused")
	private ResultSet rs = null;
	
	/**
	 * 获取连接数据库
	 * @return the conn
	 */
	public Connection getConn() 
	{
		try 
		{
			if(this.conn == null || this.conn.isClosed())
			{
				DataBaseConn db = new DataBaseConn();
				this.conn = db.getConn();
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 
	 * @Title:addStu
	 * @Description:
	 * @param:@param stu
	 * @param:@return
	 * @return:Student
	 * @throws
	 */
	public Student addStu(Student stu)
	{
		String sql = "";
		String sql0 = "";
		try 
		{
			psmt = this.getConn().prepareStatement(sql);
			psmt.setString(2, stu.getStuName());
			psmt.setString(3, stu.getStuSex());
			psmt.setTimestamp(4, new Timestamp(stu.getBirthday().getTime()));
			psmt.setString(5, stu.getProfessional());
			psmt.setDouble(6, stu.getScore());
			psmt.setString(7, stu.getRemarks());
			psmt.execute();
			psmt = this.getConn().prepareStatement(sql0);
			psmt.setString(1, stu.getStuName());
			psmt.setBytes(2, stu.getPhoto());
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				//关闭PreparedStatement对象
				psmt.close();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
			try 
			{
				//关闭Connection对象
				conn.close();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		}
		return stu;
	}

}