userdaoimpl_jdbc.java

来自「出学者的基础,你可以用它来开始你学习AJAX的旅程.很简单的.」· Java 代码 · 共 48 行

JAVA
48
字号
package demo;

import java.sql.*;

public class UserDaoImpl_Jdbc implements UserDaoIf{
	//根据用户名查找用户
	public User findUserByName(String userName){
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{	
			con = getConnection();
			String sql = "select * from ajax_user where username = ?";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1,userName);
			rs = pstmt.executeQuery();
			if(rs.next()){
				User user = new User();
				user.setUserId(rs.getInt(1));
				user.setUserName(rs.getString(2));
				user.setPassWord(rs.getString(3));
				return user;
			}else{
				return null;
			}			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			close(con,pstmt,rs);
		}
	}
	//封装了获取连接的动作
	public Connection getConnection(){
		ConnectionFactory factory = ConnectionFactory.getInstance();
		return factory.getConnection();
	}
	//释放资源
	public void close(Connection con,PreparedStatement pstmt,ResultSet res){
		try{
			if(con != null) con.close();
			if(pstmt != null) pstmt.close();
			if(res != null) res.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}		
}

⌨️ 快捷键说明

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