User的Dao类

/**对数据库中的表进行增,删,改,查
*/
package com.nsu.dao;

import java.net.PasswordAuthentication;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.omg.CORBA.WCharSeqHelper;

import com.mysql.jdbc.PreparedStatement;
import com.nsu.bean.User;
import com.nsu.util.DButil;

public class UserDao {
private Connection conn;
private java.sql.PreparedStatement pst;//pst预处理
private ResultSet rs;
DButil db = new DButil();

//添加用户,传递参数user
public boolean addUser(User user){
    boolean b=false;
    try {
        //获得数据库的链接
        conn=db.getConn();
        String sql = "insert into users(name,sex,pwd ) values('"+user.getName()+"','"+user.getSex()+"','"+user.getPwd()+"')";
        //private java.sql.PreparedStatement pst;编译
        pst = conn.prepareStatement(sql);
        int a =pst.executeUpdate();
        if(a==1){
            b=true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return b;
}

//删除用户
public boolean delUser(int id) {
    boolean b= false;
    try {
        conn = db.getConn();//1.获得链接
         String sql="delete from users where id="+id;//2.输入sql语句
         pst=conn.prepareStatement(sql);//3.预编译
         int a=pst.executeUpdate();//4.执行
         if(a==1){
             b=true;
         }
         
    } catch (Exception e) {
        
        e.printStackTrace();
    }
 
    return b; 
    
}


//修改用户
public boolean updUser(User user){
    //判断是否修改
    boolean b = false;
    try {
        conn=db.getConn();//1.获得链接
        String sql="update users set name='"+user.getName()+"',sex='"+user.getSex()+"',pwd='"+user.getPwd()+"' where id="+user.getId();//2.输入sql语句
        System.out.println(sql);
        pst=conn.prepareStatement(sql);
        int a =pst.executeUpdate();
        if (a==1){
            b=true;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return b;
}
//查寻
public User selectUserbyId(int id){
    User user=new User();
    try {
        conn=db.getConn();
        String sql="select*from users where id="+id;
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        while (rs.next()){   //判断是否为空
            user.setId(rs.getInt(1)); 
            user.setName(rs.getString(2));
            user.setPwd(rs.getString(3));
            user.setSex(rs.getString(4));
        
        
        }
    }
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 


       catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return user;


    
}
//多项查寻
public ArrayList selectUserAll(){
    ArrayList al = new ArrayList();
    try {
        conn=db.getConn();//获取链接
        String sql = "select * from users";
        pst=conn.prepareStatement(sql);//编译
        rs=pst.executeQuery();
        while(rs.next()){
            User user = new User();
            //rs.getint(1)
            user.setId(rs.getInt(1));
            user.setName(rs.getString(2));
            user.setSex(rs.getString(3));
            user.setPwd(rs.getString(4));
            //将user对象添加到al中
            al.add(user);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return al;
}
//查寻用户名密码与数据库密码是否一致
public boolean loginUser(String username,String pwd){
    boolean b = false;
    String password="";
    try {
        conn = db.getConn();
        String sql = "select pwd from users where usersname";
                pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        if(rs.next()){
            password=rs.getString(1);
            
        }
        if(password.equals(pwd))
            b=true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
    
}

public static void main(String[] args) {
    UserDao ud = new UserDao();
    boolean b =ud.loginUser("admin", "admin");
    System.out.println(b);
    //ud.delUser(1);//(删除)
    
    //添加数据
    //User user = new User();
    //user.setId(8);
    //user.setSex("女");
    //user.setName("zhangsan");
    //user.setPwd("123456");
    //System.out.println(ud.addUser(user));
    
    //User user = ud.selectUserbyId(6);
    //ArrayList al = new ArrayList();
    //al = ud.selectUserAll();//调用ud方法
    //for(int i=0;i

你可能感兴趣的:(User的Dao类)