www.pudn.com > Jsp-Blog.rar > RegDao.java, change:2010-05-20,size:1733b


package myblog.dao; 
 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
 
import myblog.commons.ConnectionPool; 
 
public class RegDao { 
	private Connection conn = null; 
 
	private PreparedStatement pstmt = null; 
 
	private ResultSet rs = null; 
 
	// 检测是否有重名用户 
	public int repeat(String user_name) { 
		int num = 0; 
		conn = ConnectionPool.getConnection(); 
		String sql = "select user_name from users where user_name=?"; 
		try { 
			pstmt = conn.prepareStatement(sql); 
			pstmt.setString(1, user_name); 
			rs = pstmt.executeQuery(); 
			if (rs.next()) { 
				num = 3; 
			} 
		} catch (SQLException e) { 
 
			e.printStackTrace(); 
		} 
		return num; 
 
	} 
 
	// 用户注册 
	public int Reg(String user_name, String user_pw, String user_question, 
			String user_answer, String user_email, String user_sex, 
			String user_content) { 
		int num = 0; 
		conn = ConnectionPool.getConnection(); 
		String sql = "insert into users(user_name,user_pw,user_question,user_answer,user_email,user_sex,user_content) values (?,?,?,?,?,?,?)"; 
		try { 
			pstmt = conn.prepareStatement(sql); 
			pstmt.setString(1, user_name); 
			pstmt.setString(2, user_pw); 
			pstmt.setString(3, user_question); 
			pstmt.setString(4, user_answer); 
			pstmt.setString(5, user_email); 
			pstmt.setString(6, user_sex); 
			pstmt.setString(7, user_content); 
			num = pstmt.executeUpdate(); 
		} catch (SQLException e) { 
 
			e.printStackTrace(); 
		} 
		if (conn != null) { 
			try { 
				conn.close(); 
				if (pstmt != null) { 
					pstmt.close(); 
				} 
			} catch (SQLException e) { 
 
				e.printStackTrace(); 
			} 
		} 
		return num; 
 
	} 
}