我以前写的一个完整的userdao:
package com.bank.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import javax.naming.NamingException;
import com.bank.userbean.BankUser;
import com.bank.util.Commons;
/**
* 实现对用户表的增删查改
* @author Administrator
*
*/
public class UserImpl implements UserDao{
public boolean AddAcount(BankUser bankuser) {
if(!this.IsProving(bankuser)){
return false;
}
StringBuffer sql=new StringBuffer();
sql.append("insert into bankuser(USERNAME,USERPWD,CUSTID,ACCOUNTID,CARDID,MOBILE,POSTCODE,EMAIL,ADDRESS) values");
sql.append("(");
sql.append("?,?,?,?,?,?,?,?,?");
sql.append(")");
Connection conn=null;
PreparedStatement stmt=null;
int index=1;
try {
conn=Connect.getConnection();
stmt=conn.prepareStatement(sql.toString());
stmt.setString(index++,bankuser.getUsername() );
stmt.setString(index++,bankuser.getUserpwd());
stmt.setString(index++,UUIDid());
stmt.setString(index++,bankuser.getAccountid());
stmt.setString(index++,bankuser.getCardid());
stmt.setString(index++,bankuser.getMobile());
stmt.setString(index++,bankuser.getPostcode());
stmt.setString(index++,bankuser.getEmail());
stmt.setString(index++,bankuser.getAddress());
int result=stmt.executeUpdate();
if(result>0){
return true;
}
} catch (NamingException | SQLException e) {
e.printStackTrace();
}finally{
Commons.CloseAll(stmt, null, null, conn); //关闭资源
}
return false;
}
public List
return null;
}
public int Delete(BankUser bankuser) {
return 0;
}
public int UpDate(BankUser bankuser) {
if(!Exist){
return 0; //如果用户不存在,则返回0,表示修改失败
}
String sql="update bankuser set password=? where username=?";
Connection conn=null;
PreparedStatement stmt=null;
int result=0;
try {
conn=Connect.getConnection();
stmt=conn.prepareStatement(sql);
stmt.setString(1, bankuser.getPassword());
stmt.setString(2, bankuser.getUsername());
result=stmt.executeUpdate(); //执行成功返回修改行数
return result;
} catch (NamingException | SQLException e) {
e.printStackTrace();
}finally{
Commons.CloseAll(stmt, null, rs, conn); //关闭资源
}
return 0;
}
public String UUIDid(){
return UUID.randomUUID().toString().replace("-", "");
}
/**
* 检查用户名是否存在,若存在分返回true,不存在返回false
*/
public boolean Exist(BankUser bankuser) {
if(!(bankuser.getUsername()!=null && bankuser.getUsername().length()>0)){ //判断用户为空
return true;
}
String sql="select username from bankuser where username=?";
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try {
conn=Connect.getConnection();
stmt=conn.prepareStatement(sql);
stmt.setString(1, bankuser.getUsername());
rs=stmt.executeQuery();
if(rs.next()){ //如果结果集有值,返回true
return true;
}
} catch (NamingException | SQLException e) {
e.printStackTrace();
}finally{
Commons.CloseAll(stmt, null, rs, conn); //关闭资源
}
return false;
}
/**
*
* @param bankuser 校验用户信息是否合法
* @return
*/
public boolean IsProving(BankUser bankuser){
if(Exist(bankuser)){//验证用户名是否存在
return false;
}
if(!Commons.CheckNull(bankuser.getAccountid())){
return false;
}
if(!Commons.CheckNull(bankuser.getAddress())){
return false;
}
if(!Commons.CheckNull(bankuser.getCardid())){
return false;
}
if(!Commons.CheckNull(bankuser.getEmail())){
return false;
}
if(!Commons.CheckNull(bankuser.getMobile())){
return false;
}
if(!Commons.CheckNull(bankuser.getPostcode())){
return false;
}
if(!Commons.CheckNull(bankuser.getUsername())){
return false;
}
if(!Commons.CheckNull(bankuser.getUserpwd())){
return false;
}
if(!(bankuser.getUserpwd()!=bankuser.getUserpwd2())){
return false;
}
return true;
}
}
根据当前用户查到数据库的记录。然后将新密码与记录的旧密码替换一下,在重新存入数据库。
楼主,根据你问的问题,估计你学习这些的时候基本没怎么学。
那么也不好给你讲了。
建议你直接找别人已经做好的吧。