www.pudn.com > workspace.rar > CommonUserDAOImpl.java


package dao; 
 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
 
import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 
import javax.sql.DataSource; 
 
public class CommonUserDAOImpl implements CommonUserDAO { 
 
	//网络有问题或者查询到的名字不存在将会返回false,如果名字有重复返回true 
	 
	public Connection getConnect() { 
		// TODO Auto-generated method stub 
		Context ctx = null; 
		Connection con = null; 
		try { 
			ctx = new InitialContext(); 
			DataSource ds = (DataSource) ctx.lookup("java:/MySqlDS"); 
			con = ds.getConnection(); 
		} catch (NamingException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (SQLException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
		return con; 
	} 
	 
	public boolean selectUserForCheck(String username){ 
		Connection conn = this.getConnect(); 
		try {			 
			PreparedStatement preproc = conn.prepareStatement("select userName from usertable where userName=?"); 
			preproc.setString(1,username);		 
			preproc.execute(); 
			ResultSet rs = preproc.getResultSet();			 
			if(rs.next()){				 
				return true; 
			}else 
				return false;			 
		}catch (SQLException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
			return false; 
		}finally{ 
			try { 
				conn.close(); 
			} catch (SQLException e) { 
				// TODO Auto-generated catch block 
				e.printStackTrace(); 
			} 
		}		 
	}	 
	 
 
	 
}