import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
Import the Java. Util. HashMap;
import java.util.List;
import java.util.Map;
The import com. WZC. Login. Domain. The User;
/* *
* @ the description database connection with the action class used to add and delete data and returned to the servlet USES
* @ author WANGZIC
*
*/
Public class UserDao {
/* *
* new user
* @ param and user information
* @ return new success
*/
Public Boolean insert (User User) {
The Connection con=getConnection ();
PreparedStatement PSTMT=null;
String SQL="INSERT INTO user (username, nickname and the password, gender, phone, email, address) VALUES (?,?,?,?,? ,? ,? ,? ,? ,? ,?) ";
Boolean res=false;
Try {
PSTMT=con. PrepareStatement (SQL);
PSTMT. SetString (1, user getUsername ());
PSTMT. SetString (2, user getNickname ());
PSTMT. SetString (3, user. GetPassword ());
PSTMT. SetString (4, user getGender ());
PSTMT. SetString (5, user getPhone ());
PSTMT. SetString (6, user getEmail ());
PSTMT. SetString (7, the user. The getAddress ());
Res=(PSTMT executeUpdate ()==1);
} the catch (SQLException e) {
if(! Um participant etMessage (). The contains (" PRIMARY ")) {
e.printStackTrace();
} else {
System. The out. Println (" the user name already exists ");
return false;
}
} the finally {
CloseCon (con, PSTMT);
}
return res;
}
/* *
* according to the user id to delete user information
* @ param userid user id
* @ return to delete the success
*/
Public Boolean delete (Integer userid) {
The Connection con=getConnection ();
PreparedStatement PSTMT=null;
String SQL="delete from the user where userid=?" ;
Boolean res=false;
Try {
PSTMT=con. PrepareStatement (SQL);
PSTMT. SetInt (1, userid);
Res=(PSTMT executeUpdate ()==1);
} the catch (SQLException e) {
e.printStackTrace();
return false;
} the finally {
CloseCon (con, PSTMT);
}
return res;
}
/* *
* update user information
* @ param user modified user information
* @ return update success
*/
Public Boolean update (User User) {
The Connection con=getConnection ();
PreparedStatement PSTMT=null;
String SQL="update user set the username=? The nickname=? Password=? , gender=? , phone=? , email=? Address=? Where userid=?" ;
Boolean res=false;
Try {
PSTMT=con. PrepareStatement (SQL);
PSTMT. SetString (1, user getUsername ());
PSTMT. SetString (2, user getNickname ());
PSTMT. SetString (3, user. GetPassword ());
PSTMT. SetString (4, user getGender ());
PSTMT. SetString (5, user getPhone ());
PSTMT. SetString (6, user getEmail ());
PSTMT. SetString (7, the user. The getAddress ());
PSTMT. SetInt (8, user getUserid ());
Res=(PSTMT executeUpdate ()==1);
} the catch (SQLException e) {
e.printStackTrace();
return false;
} the finally {
CloseCon (con, PSTMT);
}
return res;
}
/* *
* according to the user name lookup user password
* @ param username username
* @ return user information
*/
Public User selectByUsername (String username) {
List
String SQL="select * from the user where the username=?" ;
The Connection con=getConnection ();
PreparedStatement PSTMT=null;
The ResultSet rs;
Try {
PSTMT=con. PrepareStatement (SQL);
PSTMT. SetString (1, username);
Rs=PSTMT. ExecuteQuery ();
List=resultSetToBean (rs);
} the catch (SQLException e) {
e.printStackTrace();
} the finally {
CloseCon (con, PSTMT);
}
The return list. IsEmpty ()? Null: list. Get (0);
}
/* *
* according to the user id of the user information
* @ param userid user id
* @ return user information
*/
Public User selectByUserId (Integer userid) {
List
String SQL="select * from the user where userid=?" ;
The Connection con=getConnection ();
PreparedStatement PSTMT=null;
The ResultSet rs;
Try {
PSTMT=con. PrepareStatement (SQL);
PSTMT. SetInt (1, userid);
Rs=PSTMT. ExecuteQuery ();
List=resultSetToBean (rs);
} the catch (SQLException e) {
e.printStackTrace();
} the finally {
CloseCon (con, PSTMT);
}
The return list. IsEmpty ()? Null: list. Get (0);
}
/* *
* according to the parameters of the incoming query user
* @ param paramMap parameter Map
* @ return user list
*/
Public List
List
SQL=new StringBuilder StringBuilder (" select * from the user where 1=1 ");
List