天天看点

java中dao和实体类的关系_java里面实体类和dao类 有一段实例,希望各位高手给注释一下 要详细的注释,字数有限 就不想实体类了...

实体类很简单的。。大家懂的packages2jsp.bysj.dao;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.util.ArrayList;importjava.u...

实体类很简单的。。大家懂的

package s2jsp.bysj.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import s2jsp.bysj.entity.Users;

public class UserDao extends BaseDao {

private Connection conn;

private PreparedStatement pstmt;

private ResultSet rs;

public Users findUsers(String userName, String password) {

Users user = null;

String sql = "select * from users where userName=? and password=? ";

try {

conn = this.getConn();

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, userName);

pstmt.setString(2, password);

rs = pstmt.executeQuery();

if (rs.next()) {

user = new Users();

user.setUserID(rs.getInt("userID"));

user.setUserName(userName);

user.setPassword(password);

user.setStatus(rs.getInt("status"));

}

} catch (Exception e) {

e.printStackTrace();

} finally {

this.closeAll(conn, pstmt, rs);

}

return user;

}

public boolean findUsers(String userName){

String sql = "select * from users where userName=? ";

try {

conn = this.getConn();

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, userName);

rs = pstmt.executeQuery();

if (rs.next()) {

return true;

}

} catch (Exception e) {

e.printStackTrace();

} finally {

this.closeAll(conn, pstmt, rs);

}

return false;

}

public int insertUser(String userName, String password,int status) {

String sql = "insert into users values(?,?,?) ";

String[] params = new String[] { userName, password ,status+""};

return this.executeSQL(sql, params);

}

public List selectAllUser(){

List list=new ArrayList();

String sql = "select * from users ";

try {

conn = this.getConn();

pstmt = conn.prepareStatement(sql);

rs = pstmt.executeQuery();

while(rs.next()) {

Users user = new Users();

user.setUserID(rs.getInt("userID"));

user.setUserName(rs.getString("userName"));

user.setPassword(rs.getString("password"));

user.setStatus(rs.getInt("status"));

list.add(user);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

this.closeAll(conn, pstmt, rs);

}

return list;

}

public int deleteUserByID(String userID){

String sql="delete from users where userID = ? ";

String[] param = new String[]{ userID };

return this.executeSQL(sql, param);

}

}

展开