Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package server.rmi;
- import static java.lang.Integer.max;
- import static java.lang.Integer.parseInt;
- import java.net.MalformedURLException;
- import java.rmi.Naming;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.rmi.server.UnicastRemoteObject;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- import model.DiaChi;
- import model.DonHangOffline;
- import model.DonHangOnline;
- import model.GiamGia;
- import model.KhachHang;
- import model.NhanVien;
- import model.OfflineStatistic;
- import model.OnlineStatistic;
- import model.SanPham;
- import model.TheTinDung;
- /**
- *
- * @author TUANTHANH
- */
- public class ServerCtrImpl extends UnicastRemoteObject implements ServerCtr{
- private Connection conn;
- public ServerCtrImpl() throws RemoteException{
- try {
- conn = new DatabaseController().initConection();
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @Override
- public NhanVien kiemtraNhanVien(NhanVien nv) throws RemoteException {
- try {
- PreparedStatement ps = conn.prepareStatement("SELECT [luong]\n" +
- " ,[tendn]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[ChucvuID]\n" +
- " ,[ID]\n" +
- " ,[ten]\n" +
- " ,[ngaysinh]\n" +
- " ,[matkhau]\n" +
- " ,[chucvu]\n" +
- " ,[trangthai]\n" +
- " FROM [httql].[dbo].[Nhanvien]\n" +
- " WHERE tendn = ? AND matkhau = ?");
- ps.setString(1, nv.getTendn());
- ps.setString(2, nv.getMatkhau());
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- nv.setId(rs.getInt("ID"));
- return nv;
- }
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return null;
- }
- @Override
- public ArrayList<SanPham> timkiemSanPham(String s, int code) throws RemoteException {
- ArrayList<SanPham> lsp = new ArrayList<>();
- try {
- PreparedStatement ps = conn.prepareStatement( "SELECT [httql].[dbo].[Sanpham].[ID]\n" +
- " ,[tensp]\n" +
- " ,[gia]\n" +
- " ,[mota]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[NhaCungCapID]\n" +
- " ,[DanhmucID]\n" +
- " ,[trangthai]\n" +
- " ,[nhacc]\n" +
- " ,[danhmuc]\n" +
- " FROM [httql].[dbo].[Sanpham] WHERE [httql].[dbo].[Sanpham].[trangthai] <> 'bixoa';" );
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- if(code == 0)
- {
- if(rs.getString("tensp").equalsIgnoreCase(s))
- {
- SanPham b = new SanPham();
- b.setId(rs.getInt("ID"));
- b.setTen(rs.getString("tensp"));
- b.setGia(rs.getInt("gia"));
- //SanPham b = new SanPham(rs.getInt("id"), rs.getString("tensp"), rs.getString("nhacc"), rs.getString("danhmuc"), rs.getInt("gia"), rs.getString("tencn"), rs.getInt("IDCN"));
- lsp.add(b);
- }
- }else if(code == 1)
- {
- System.out.println(rs.getString("tensp"));
- System.out.println(rs.getString("tensp").indexOf(s));
- if(rs.getString("tensp").indexOf(s) >= 0)
- {
- SanPham b = new SanPham();
- b.setId(rs.getInt("ID"));
- b.setTen(rs.getString("tensp"));
- b.setGia(rs.getInt("gia"));
- //SanPham b = new SanPham(rs.getInt("id"), rs.getString("tensp"), rs.getString("nhacc"), rs.getString("danhmuc"), rs.getInt("gia"), rs.getString("tencn"), rs.getInt("IDCN"));
- lsp.add(b);
- }
- }
- }
- } catch (SQLException ex) {
- Logger.getLogger(DatabaseController.class.getName()).log(Level.SEVERE, null, ex);
- }
- return lsp;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public int searchMaGiamGia(int ma) throws RemoteException {
- try{
- PreparedStatement ps = conn.prepareStatement("SELECT [ID]\n" +
- " ,[giamgia]\n" +
- " ,[trangthai]\n" +
- " FROM [httql].[dbo].[MaGiamGia]\n" +
- " WHERE [ID] = ? AND [trangthai] <> 'bixoa'");
- ps.setInt(1, ma);
- ResultSet rs = ps.executeQuery();
- if(rs.next())
- {
- return rs.getInt("giamgia");
- }
- return 0;
- }catch(Exception e){}
- return 0;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- public int checkIDdonhang(int id)
- {
- try {
- String sql = "SELECT [ID]\n" +
- " ,[ngaydathang]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[MaGiamGiaID]\n" +
- " FROM [httql].[dbo].[DonHang]\n" +
- " WHERE [ID] = ?";
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setInt(1, id);
- ResultSet rs = ps.executeQuery();
- if(rs.next())
- {
- return 0;
- }
- return 1;
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return 0;
- }
- @Override
- public void thanhtoan(DonHangOffline dh) throws RemoteException {
- try {
- int id = 4;
- PreparedStatement ps = conn.prepareStatement("INSERT INTO [dbo].[DonHang]\n" +
- " ([ngaydathang]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[MaGiamGiaID])\n" +
- " VALUES\n" +
- " (?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
- ps.setString(1, dh.getNgay());
- ps.setInt(2, dh.getNv().getMachinhanh());
- ps.setInt(3, dh.getMagiamgia());
- ps.executeUpdate();
- //JOptionPane.showMessageDialog(null, "truoc khi update");
- ResultSet rs = ps.getGeneratedKeys();
- if(rs.next())
- {
- id = rs.getInt(1);
- System.out.println("Khoa chinh la " + id);
- }
- // JOptionPane.showMessageDialog(null, "sau khi update");
- ps = conn.prepareStatement("INSERT INTO [dbo].[DonHangOffline]\n" +
- " ([DonHangID]\n" +
- " ,[NhanvienID])\n" +
- " VALUES\n" +
- " (?, ?)");
- ps.setInt(1, id);
- ps.setInt(2, dh.getNv().getId());
- ps.executeUpdate();
- ArrayList<SanPham> lsp = dh.getLsp();
- for(int i = 1; i <= lsp.size(); i ++)
- {
- ps = conn.prepareStatement("INSERT INTO [dbo].[ChiTietDonHang]\n" +
- " ([STT]\n" +
- " ,[soluong]\n" +
- " ,[SanphamID]\n" +
- " ,[DonHangID])\n" +
- " VALUES\n" +
- " (?, ?, ?, ?)");
- ps.setInt(1, i);
- ps.setInt(2, lsp.get(i - 1).getSoluong());
- ps.setInt(3, lsp.get(i - 1).getId());
- ps.setInt(4, id);
- ps.executeUpdate();
- }
- //throw new UnsupportedOpera tionException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @Override
- public KhachHang kiemtraKhachHang(KhachHang a) throws RemoteException {
- try {
- PreparedStatement ps = conn.prepareStatement( "SELECT * FROM KhachHang WHERE TaiKhoantendn = ? AND matkhau = ?" );
- ps.setString(1, a.getTendn());
- ps.setString(2, a.getMatkhau());
- ResultSet rs = ps.executeQuery() ;
- int dem = 0;
- while(rs.next())
- {
- return a;
- }
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return null;
- }
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- @Override
- public KhachHang capnhatKhachHang(KhachHang a) throws RemoteException {
- try {
- PreparedStatement ps = conn.prepareStatement("UPDATE [dbo].[KhachHang]\n" +
- " SET [TaiKhoantendn] = ?\n" +
- " ,[matkhau] = ?\n" +
- " WHERE [TaiKhoantendn] = ?");
- ps.setString(1, a.getTendn());
- ps.setString(2, a.getMatkhau());
- ps.setString(3, a.getTendn());
- ps.executeUpdate();
- return a;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- return null;
- }
- }
- @Override
- public ArrayList<SanPham> khachhangTimKiem(String s, int code) throws RemoteException {
- ArrayList<SanPham> lsp = new ArrayList<>();
- try {
- PreparedStatement ps = conn.prepareStatement( "SELECT [ID]\n" +
- " ,[tensp]\n" +
- " ,[gia]\n" +
- " ,[mota]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[NhaCungCapID]\n" +
- " ,[DanhmucID]\n" +
- " ,[trangthai]\n" +
- " ,[nhacc]\n" +
- " ,[danhmuc]\n" +
- " FROM [httql].[dbo].[Sanpham] WHERE [trangthai] <> 'bixoa';" );
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- if(code == 0)
- {
- if(rs.getString("tensp").equalsIgnoreCase(s))
- {
- SanPham b = new SanPham();
- b.setId(rs.getInt("ID"));
- b.setTen(rs.getString("tensp"));
- b.setGia(rs.getInt("gia"));
- if(rs.getInt("ChiNhanhID") == 1)
- {
- b.setChinhanh("Hà Đông");
- }else if(rs.getInt("ChiNhanhID") == 2)
- {
- b.setChinhanh("Thanh Xuân");
- }else
- {
- b.setChinhanh("Cầu Giấy");
- }
- //SanPham b = new SanPham(rs.getInt("id"), rs.getString("tensp"), rs.getString("nhacc"), rs.getString("danhmuc"), rs.getInt("gia"), rs.getString("tencn"), rs.getInt("IDCN"));
- lsp.add(b);
- }
- }else if(code == 1)
- {
- System.out.println(rs.getString("tensp"));
- System.out.println(rs.getString("tensp").indexOf(s));
- if(rs.getString("tensp").indexOf(s) >= 0)
- {
- SanPham b = new SanPham();
- b.setId(rs.getInt("ID"));
- b.setTen(rs.getString("tensp"));
- b.setGia(rs.getInt("gia"));
- if(rs.getInt("ChiNhanhID") == 1)
- {
- b.setChinhanh("Hà Đông");
- }else if(rs.getInt("ChiNhanhID") == 2)
- {
- b.setChinhanh("Thanh Xuân");
- }else
- {
- b.setChinhanh("Cầu Giấy");
- }
- //SanPham b = new SanPham(rs.getInt("id"), rs.getString("tensp"), rs.getString("nhacc"), rs.getString("danhmuc"), rs.getInt("gia"), rs.getString("tencn"), rs.getInt("IDCN"));
- lsp.add(b);
- }
- }
- }
- } catch (SQLException ex) {
- Logger.getLogger(DatabaseController.class.getName()).log(Level.SEVERE, null, ex);
- }
- return lsp;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public void thanhtoan(DonHangOnline dh) throws RemoteException {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public KhachHang dangki(KhachHang a) throws RemoteException {
- try{
- PreparedStatement ps = conn.prepareStatement("INSERT INTO [dbo].[KhachHang]\n" +
- " (\n" +
- " [TaiKhoantendn]\n" +
- " ,[matkhau])\n" +
- " VALUES\n" +
- " (?, ?)");
- ps.setString(1, a.getTendn());
- ps.setString(2, a.getMatkhau());
- ps.executeUpdate();
- return a;
- }catch(Exception e)
- {
- return null;
- }
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- public int addDonHangOnline(DonHangOnline dh)
- {
- int id = 4;
- try {
- PreparedStatement ps = conn.prepareStatement("INSERT INTO [httql].[dbo].[DonHang]\n" +
- " ([ngaydathang]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[MaGiamGiaID])\n" +
- " VALUES\n" +
- " (?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
- ps.setString(1, dh.getDate());
- ps.setInt(2, dh.getIdcn());
- ps.setInt(3, dh.getIdgiamgia());
- ps.executeUpdate();
- //JOptionPane.showMessageDialog(null, "truoc khi update");
- ResultSet rs = ps.getGeneratedKeys();
- //System.out.println(rs.getInt(id));
- if(rs.next())
- {
- id = (int) rs.getLong(1);
- System.out.println("Khoa chinh la " + id);
- }
- // JOptionPane.showMessageDialog(null, "sau khi update");
- ps = conn.prepareStatement("INSERT INTO [httql].[dbo].[DonHangOnline]\n" +
- " ([KhachHangTaiKhoantendn]\n" +
- " ,[diachi]\n" +
- " ,[DonHangID])\n" +
- " VALUES\n" +
- " (?, ?, ?)");
- ps.setString(1, dh.getKh().getTendn());
- ps.setString(2, dh.getDiachi());
- ps.setInt(3, id);
- ps.executeUpdate();
- ArrayList<SanPham> lsp = dh.getLsp();
- for(int i = 1; i <= lsp.size(); i ++)
- {
- ps = conn.prepareStatement("INSERT INTO [httql].[dbo].[ChiTietDonHang]\n" +
- " ([STT]\n" +
- " ,[soluong]\n" +
- " ,[SanphamID]\n" +
- " ,[DonHangID])\n" +
- " VALUES\n" +
- " (?, ?, ?, ?)");
- ps.setInt(1, i);
- ps.setInt(2, lsp.get(i - 1).getSoluong());
- ps.setInt(3, lsp.get(i - 1).getId());
- ps.setInt(4, id);
- ps.executeUpdate();
- }
- return 1;
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return 0;
- }
- @Override
- public int thanhtoanOnline(ArrayList<DonHangOnline> dho) throws RemoteException {
- int id = 4;
- for(DonHangOnline dh: dho)
- {
- int res = addDonHangOnline(dh);
- if(res == 0)
- return 0;
- }
- return 1;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public ArrayList<SanPham> themSanPham(SanPham sp) throws RemoteException {
- ArrayList<SanPham> lsp = new ArrayList<>();
- try {
- PreparedStatement ps = conn.prepareStatement("INSERT INTO [dbo].[Sanpham]\n" +
- " ([tensp]\n" +
- " ,[gia]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[trangthai]\n" +
- " )\n" +
- " VALUES\n" +
- " (?, ?, ?, ?)");
- ps.setString(1, sp.getTen());
- ps.setInt(2, sp.getGia());
- ps.setInt(3, sp.getIdchinhanh());
- ps.setString(4, "tontai");
- ps.executeUpdate();
- ps = conn.prepareStatement( "SELECT [httql].[dbo].[Sanpham].[ID]\n" +
- " ,[tensp]\n" +
- " ,[gia]\n" +
- " ,[mota]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[NhaCungCapID]\n" +
- " ,[DanhmucID]\n" +
- " ,[trangthai]\n" +
- " ,[nhacc]\n" +
- " ,[danhmuc]\n" +
- " FROM [httql].[dbo].[Sanpham] WHERE [httql].[dbo].[Sanpham].[trangthai] <> 'bixoa';" );
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- SanPham b = new SanPham();
- b.setId(rs.getInt("ID"));
- b.setTen(rs.getString("tensp"));
- b.setGia(rs.getInt("gia"));
- //SanPham b = new SanPham(rs.getInt("id"), rs.getString("tensp"), rs.getString("nhacc"), rs.getString("danhmuc"), rs.getInt("gia"), rs.getString("tencn"), rs.getInt("IDCN"));
- lsp.add(b);
- }
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return lsp;
- }
- @Override
- public ArrayList<SanPham> suaSanPham(SanPham sp) throws RemoteException {
- ArrayList<SanPham> lsp = new ArrayList<>();
- try{
- PreparedStatement ps = conn.prepareStatement("UPDATE [dbo].[Sanpham]\n" +
- " SET [tensp] = ?\n" +
- " ,[gia] = ?\n" +
- " WHERE [ID] = ?");
- ps.setString(1, sp.getTen());
- ps.setInt(2, sp.getGia());
- ps.setInt(3,sp.getId());
- ps.executeUpdate();
- ps = conn.prepareStatement( "SELECT [httql].[dbo].[Sanpham].[ID]\n" +
- " ,[tensp]\n" +
- " ,[gia]\n" +
- " ,[mota]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[NhaCungCapID]\n" +
- " ,[DanhmucID]\n" +
- " ,[trangthai]\n" +
- " ,[nhacc]\n" +
- " ,[danhmuc]\n" +
- " FROM [httql].[dbo].[Sanpham] WHERE [httql].[dbo].[Sanpham].[trangthai] <> 'bixoa';" );
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- SanPham b = new SanPham();
- b.setId(rs.getInt("ID"));
- b.setTen(rs.getString("tensp"));
- b.setGia(rs.getInt("gia"));
- //SanPham b = new SanPham(rs.getInt("id"), rs.getString("tensp"), rs.getString("nhacc"), rs.getString("danhmuc"), rs.getInt("gia"), rs.getString("tencn"), rs.getInt("IDCN"));
- lsp.add(b);
- }
- }catch(Exception e){}
- return lsp;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public ArrayList<SanPham> xoaSanPham(SanPham sp) throws RemoteException {
- ArrayList<SanPham> lsp = new ArrayList<>();
- try{
- PreparedStatement ps = conn.prepareStatement("UPDATE [dbo].[Sanpham]\n" +
- " SET [trangthai] = ?\n" +
- " WHERE [ID] = ?");
- ps.setString(1, "bixoa");
- ps.setInt(2, sp.getId());
- ps.executeUpdate();
- ps = conn.prepareStatement( "SELECT [httql].[dbo].[Sanpham].[ID]\n" +
- " ,[tensp]\n" +
- " ,[gia]\n" +
- " ,[mota]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[NhaCungCapID]\n" +
- " ,[DanhmucID]\n" +
- " ,[trangthai]\n" +
- " ,[nhacc]\n" +
- " ,[danhmuc]\n" +
- " FROM [httql].[dbo].[Sanpham] WHERE [httql].[dbo].[Sanpham].[trangthai] <> 'bixoa';" );
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- SanPham b = new SanPham();
- b.setId(rs.getInt("ID"));
- b.setTen(rs.getString("tensp"));
- b.setGia(rs.getInt("gia"));
- //SanPham b = new SanPham(rs.getInt("id"), rs.getString("tensp"), rs.getString("nhacc"), rs.getString("danhmuc"), rs.getInt("gia"), rs.getString("tencn"), rs.getInt("IDCN"));
- lsp.add(b);
- }
- }catch(Exception e){}
- return lsp;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public ArrayList<GiamGia> themMa(int parseInt) throws RemoteException {
- ArrayList<GiamGia> lgg = new ArrayList<>();
- try {
- PreparedStatement ps = conn.prepareStatement("INSERT INTO [dbo].[MaGiamGia]\n" +
- " ([giamgia]\n" +
- " ,[trangthai])\n" +
- " VALUES\n" +
- " (?, ?)");
- ps.setInt(1, parseInt);
- ps.setString(2, "tontai");
- ps.executeUpdate();
- ps = conn.prepareStatement("SELECT [ID]\n" +
- " ,[giamgia]\n" +
- " ,[trangthai]\n" +
- " FROM [httql].[dbo].[MaGiamGia]\n" +
- " WHERE [trangthai] <> 'bixoa'");
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- lgg.add(new GiamGia(rs.getInt("ID"), rs.getInt("giamgia")));
- }
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return lgg;
- }
- @Override
- public ArrayList<GiamGia> xoaMaGiamGia(int i) throws RemoteException {
- ArrayList<GiamGia> lgg = new ArrayList<>();
- try {
- PreparedStatement ps = conn.prepareStatement("UPDATE [dbo].[MaGiamGia]\n" +
- " SET [trangthai] = 'bixoa'\n" +
- " WHERE [ID] = ?");
- ps.setInt(1, i);
- ps.executeUpdate();
- ps = conn.prepareStatement("SELECT [ID]\n" +
- " ,[giamgia]\n" +
- " ,[trangthai]\n" +
- " FROM [httql].[dbo].[MaGiamGia]\n" +
- " WHERE [trangthai] <> 'bixoa'");
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- lgg.add(new GiamGia(rs.getInt("ID"), rs.getInt("giamgia")));
- }
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return lgg;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public ArrayList<SanPham> getSanPham() throws RemoteException {
- ArrayList<SanPham> lsp = new ArrayList<>();
- try {
- PreparedStatement ps = conn.prepareStatement( "SELECT [httql].[dbo].[Sanpham].[ID]\n" +
- " ,[tensp]\n" +
- " ,[gia]\n" +
- " ,[mota]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[NhaCungCapID]\n" +
- " ,[DanhmucID]\n" +
- " ,[trangthai]\n" +
- " ,[nhacc]\n" +
- " ,[danhmuc]\n" +
- " FROM [httql].[dbo].[Sanpham] WHERE [httql].[dbo].[Sanpham].[trangthai] <> 'bixoa';" );
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- SanPham b = new SanPham();
- b.setId(rs.getInt("ID"));
- b.setTen(rs.getString("tensp"));
- b.setGia(rs.getInt("gia"));
- //SanPham b = new SanPham(rs.getInt("id"), rs.getString("tensp"), rs.getString("nhacc"), rs.getString("danhmuc"), rs.getInt("gia"), rs.getString("tencn"), rs.getInt("IDCN"));
- lsp.add(b);
- }
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return lsp;
- }
- @Override
- public ArrayList<GiamGia> getMaGiamGia() throws RemoteException {
- ArrayList<GiamGia> lgg = new ArrayList<>();
- try {
- PreparedStatement ps = conn.prepareStatement("SELECT [ID]\n" +
- " ,[giamgia]\n" +
- " ,[trangthai]\n" +
- " FROM [httql].[dbo].[MaGiamGia]\n" +
- " WHERE [trangthai] <> 'bixoa'");
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- lgg.add(new GiamGia(rs.getInt("ID"), rs.getInt("giamgia")));
- }
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return lgg;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- public ArrayList<OfflineStatistic> getDonHangOffline()
- {
- ArrayList<OfflineStatistic> loff = new ArrayList<>();
- try{
- PreparedStatement ps = conn.prepareStatement("SELECT TOP 1000 [DonHangID]\n" +
- " ,[NhanvienID]\n" +
- " FROM [httql].[dbo].[DonHangOffline]");
- ResultSet rs = ps.executeQuery();
- OfflineStatistic off = new OfflineStatistic();
- while(rs.next())
- {
- off.setDhid(rs.getInt("DonHangID"));
- PreparedStatement st1 = conn.prepareStatement("SELECT TOP 1000 [ID]\n" +
- " ,[ngaydathang]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[MaGiamGiaID]\n" +
- " FROM [httql].[dbo].[DonHang] WHERE ID = ?");
- st1.setInt(1, off.getDhid());
- System.out.println(off.getDhid());
- ResultSet rs1 = st1.executeQuery();
- rs1.next();
- off.setDate(rs1.getString("ngaydathang"));
- ps = conn.prepareStatement("SELECT\n" +
- " [soluong]\n" +
- " ,[SanphamID]\n" +
- " ,[DonHangID]\n" +
- " , tensp\n" +
- " ,gia\n" +
- " FROM [httql].[dbo].[ChiTietDonHang], [httql].[dbo].[Sanpham] \n" +
- " WHERE DonHangID = ? AND SanphamID = [httql].[dbo].[Sanpham] .ID;");
- ps.setInt(1, off.getDhid());
- ResultSet rs2 = ps.executeQuery();
- while(rs2.next())
- {
- SanPham sp = new SanPham();
- sp.setId(rs2.getInt("SanphamID"));
- sp.setTen(rs2.getString("tensp"));
- sp.setGia(rs2.getInt("gia"));
- sp.setSoluong(rs2.getInt("soluong"));
- off.getLsp().add(sp);
- }
- loff.add(off);
- }
- }catch(Exception e){
- System.out.println(e);
- }
- return loff;
- }
- public ArrayList<OnlineStatistic> getDonHangOnline()
- {
- ArrayList<OnlineStatistic> lon = new ArrayList<>();
- try{
- PreparedStatement st = conn.prepareStatement("SELECT [KhachHangTaiKhoantendn]\n" +
- " ,[DonHangID]\n" +
- " FROM [httql].[dbo].[DonHangOnline]");
- ResultSet rs = st.executeQuery();
- while(rs.next())
- {
- OnlineStatistic on = new OnlineStatistic();
- on.setIddh(rs.getInt("DonHangID"));
- on.setTedn(rs.getString("KhachHangTaiKhoantendn"));
- PreparedStatement st1 = conn.prepareStatement("SELECT TOP 1000 [ID]\n" +
- " ,[ngaydathang]\n" +
- " ,[ChiNhanhID]\n" +
- " ,[MaGiamGiaID]\n" +
- " FROM [httql].[dbo].[DonHang] WHERE ID = ?");
- st1.setInt(1, on.getIddh());
- System.out.println(on.getIddh());
- ResultSet rs1 = st1.executeQuery();
- rs1.next();
- on.setDate(rs1.getString("ngaydathang"));
- PreparedStatement st2 = conn.prepareStatement("SELECT TOP 1000 [STT]\n" +
- " , [tensp]\n" +
- " ,[gia]\n" +
- " ,[soluong]\n" +
- " ,[SanphamID]\n" +
- " ,[DonHangID]\n" +
- " FROM [httql].[dbo].[ChiTietDonHang], [httql].[dbo].[Sanpham] WHERE [httql].[dbo].[ChiTietDonHang].[SanphamID] = ID AND DonHangID = ?");
- st2.setInt(1, on.getIddh());
- ResultSet rs2 = st2.executeQuery();
- while(rs2.next())
- {
- SanPham tam = new SanPham();
- tam.setId(rs2.getInt("SanphamID"));
- tam.setTen("tensp");
- tam.setGia(rs2.getInt("soluong"));
- tam.setSoluong(rs2.getInt("soluong"));
- on.getLsp().add(tam);
- }
- lon.add(on);
- }
- }catch(Exception e){
- System.out.println(e);
- }
- return lon;
- }
- /*try {
- ArrayList<OnlineStatistic> off = new ArrayList<>();
- PreparedStatement ps = conn.prepareStatement("SELECT [KhachHangTaiKhoantendn]\n" +
- " ,[DonHangID]\n" +
- " FROM [httql].[dbo].[DonHangOnline]");
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- OfflineStatistic off1 = new OfflineStatistic();
- off1.setDhid(rs.getInt("DonHangID"));
- ps = conn.prepareStatement("SELECT [ID]\n" +
- " ,[ngaydathang]\n" +
- " ,[ChiNhanhID]\n" +
- " FROM [httql].[dbo].[DonHang] WHERE [ID] = ?;");
- ps.setInt(1, off1.getDhid());
- ResultSet rs1 = ps.executeQuery();
- while(rs1.next())
- {
- off1.setDate(rs1.getString("ngaydathang"));
- }
- ps = conn.prepareStatement("SELECT [STT]\n" +
- " ,[soluong]\n" +
- " ,[DonHangID]\n" +
- " ,[tensp]\n" +
- " ,[gia]\n" +
- " FROM [httql].[dbo].[ChiTietDonHang], [httql].[dbo].[Sanpham] WHERE [SanphamID] = [ID] AND DonHangID = ?;");
- ps.setInt(1, off1.getDhid());
- ResultSet rs2 = ps.executeQuery();
- while(rs2.next())
- {
- off1
- }
- }
- } catch (SQLException ex) {
- Logger.getLogger(ServerCtrImpl.class.getName()).log(Level.SEVERE, null, ex);
- }
- return off;
- }*/
- @Override
- public String sanphamNhieuNhat() throws RemoteException {
- try{
- PreparedStatement ps = conn.prepareStatement(" SELECT SPID, [httql].[dbo].[Sanpham].tensp FROM\n" +
- " (SELECT TOP 1\n" +
- " [SanphamID] AS SPID\n" +
- " , SUM([soluong]) AS SL\n" +
- " FROM [httql].[dbo].[ChiTietDonHang]\n" +
- " GROUP BY ([SanphamID])\n" +
- " ORDER BY SL DESC) AS THONGKE, [httql].[dbo].[Sanpham]\n" +
- " WHERE THONGKE.SPID = Sanpham.ID;");
- ResultSet rs = ps.executeQuery();
- rs.next();
- return "Sản phẩm bán được nhiều nhất: " + rs.getString("tensp");
- }catch(Exception e){
- System.out.println(e);
- }
- return null;
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- public static void main(String[] args) throws NotBoundException, MalformedURLException, RemoteException {
- ServerCtr c;
- c = (ServerCtr) Naming.lookup("rmi://192.168.0.146:9679/ctr");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement