Thứ Sáu, 23 tháng 8, 2013

DSJ

public class Database {
    private static Connection conn = null;
   
    public static Connection getConection(){
        try {
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
            }
                conn = java.sql.DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=BanHang", "sa", "123456");
                       
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
       
            return conn;
    }
   
    public void addTailieu(Tailieu tl){
        PreparedStatement psmt = null;
        try {
            String query = "insert into tailieu(ten,ngonngu,nxb,tacgia,tomtat) values(?,?,?,?,?)";
            psmt = getConection().prepareStatement(query);
            psmt.setString(1, tl.getTen());
            psmt.setString(2, tl.getNgonngu());
            psmt.setString(3, tl.getNxb());
            psmt.setString(4, tl.getTacgia());
            psmt.setString(5, tl.getTomtat());
            psmt.executeUpdate();
            psmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    public void updateTailieu(Tailieu tl){
        PreparedStatement psmt = null;
        try {
            String query = "update tailieu set ten=?,ngonngu=?,nxb=?,tacgia=?,tomtat=? where id=?";
            psmt = getConection().prepareStatement(query);
            psmt.setString(1, tl.getTen());
            psmt.setString(2, tl.getNgonngu());
            psmt.setString(3, tl.getNxb());
            psmt.setString(4, tl.getTacgia());
            psmt.setString(5, tl.getTomtat());
            psmt.setInt(6, tl.getId());
            psmt.executeUpdate();
            psmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    public Tailieu getTailieu(int id){
        PreparedStatement psmt = null;
        Tailieu tl = new Tailieu();
        try {
            psmt = getConection().prepareStatement("select * from tailieu where id=?");
            psmt.setInt(1, id);
            ResultSet rs = psmt.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tl;
    }
   
    public boolean  checkLoginNV(Nhanvien nv){
        PreparedStatement psmt = null;
        boolean check = false;
        try {
            psmt = getConection().prepareStatement("select * from nhanvien where username=? and password=?");
            psmt.setString(1, nv.getUsername());
            psmt.setString(2, nv.getPassword());
            ResultSet rs = psmt.executeQuery();
            if(rs.next()){
                check = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return check;
    }
   
    public ArrayList<Tailieu> getAllTailieu(){
        ArrayList<Tailieu> list = new ArrayList<>();
        try {
            PreparedStatement psmt = getConection().prepareStatement("select * from tailieu");
            ResultSet rs = psmt.executeQuery();
            while(rs.next()){
                Tailieu tl = new Tailieu(rs.getInt(1), rs.getString(2),rs.getString(3) , rs.getString(4), rs.getString(5), rs.getString(6));
                list.add(tl);
            }
            psmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
   
    public void deleteTailieu(int id){
        PreparedStatement psmt = null;
        try {
            psmt = getConection().prepareStatement("delete from tailieu where id=?");
            psmt.setInt(1, id);
            psmt.executeUpdate();
            psmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Không có nhận xét nào:

Đăng nhận xét