Logincs.java~66~
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:8k
源码类别:

Java编程

开发平台:

Java

  1. package uml_gaokai;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import com.borland.jbcl.layout.*;
  5. import java.awt.event.*;
  6. import com.borland.dx.sql.dataset.*;
  7. import java.sql.*;
  8. import javax.swing.border.*;
  9. /**
  10.  * <p>Title: </p>
  11.  * <p>Description: </p>
  12.  * <p>Copyright: Copyright (c) 2003</p>
  13.  * <p>Company: </p>
  14.  * @author unascribed
  15.  * @version 1.0
  16.  */
  17. public class Logincs extends JDialog implements ActionListener{
  18.   private JPanel panel1 = new JPanel();
  19.   private BorderLayout borderLayout1 = new BorderLayout();
  20.   private JPanel jPanel1 = new JPanel();
  21.   private XYLayout xYLayout1 = new XYLayout();
  22.   private JLabel jLabel1 = new JLabel();
  23.   private JLabel jLabel2 = new JLabel();
  24.   private JTextField jTextField1 = new JTextField();
  25.   private JButton jButton1 = new JButton();
  26.   private JButton jButton2 = new JButton();
  27.   private JPasswordField jPasswordField1 = new JPasswordField();
  28.   private Database database1 = new Database();
  29.   private QueryDataSet queryDataSet1 = new QueryDataSet();
  30.   private JLabel jLabel3 = new JLabel();
  31.   private JLabel jLabel4 = new JLabel();
  32.   private JLabel jLabel5 = new JLabel();
  33.   private TitledBorder titledBorder1;
  34.   private TitledBorder titledBorder2;
  35.   private JLabel jLabel6 = new JLabel();
  36.   public Logincs(Frame frame, String title, boolean modal) {
  37.     super(frame, title, modal);
  38.     try {
  39.       jbInit();
  40.       pack();
  41.     }
  42.     catch(Exception ex) {
  43.       ex.printStackTrace();
  44.     }
  45.   }
  46.   public Logincs() {
  47.     this(null, "", false);
  48.   }
  49.   private void jbInit() throws Exception {
  50.     titledBorder1 = new TitledBorder("");
  51.     titledBorder2 = new TitledBorder("");
  52.     panel1.setLayout(borderLayout1);
  53.     jPanel1.setLayout(xYLayout1);
  54.     jLabel1.setForeground(Color.blue);
  55.     jLabel1.setText("用户名");
  56.     jLabel2.setForeground(Color.blue);
  57.     jLabel2.setText("密码");
  58.     jButton1.setForeground(UIManager.getColor("List.selectionBackground"));
  59.     jButton1.setBorder(BorderFactory.createRaisedBevelBorder());
  60.     jButton1.setText("登录");
  61.     jButton1.addActionListener(this);
  62.     jButton2.setForeground(UIManager.getColor("Menu.selectionBackground"));
  63.     jButton2.setBorder(BorderFactory.createRaisedBevelBorder());
  64.     jButton2.setToolTipText("");
  65.     jButton2.setText("取消");
  66.     jButton2.addActionListener(this);
  67.     jPanel1.setBorder(BorderFactory.createEtchedBorder());
  68.     jPanel1.setMinimumSize(new Dimension(600, 600));
  69.     jPanel1.setPreferredSize(new Dimension(400, 300));
  70.     jPasswordField1.setBorder(titledBorder1);
  71.     jPasswordField1.setToolTipText("");
  72.     database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:odbc:gaokaiSQL", "sa", "", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
  73.     queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "SELECT [user].userName,[user].userPassword,[user].userAuthority n" +
  74.         "FROM [UML_CS].[dbo].[user]", null, true, Load.ALL));
  75.     this.setResizable(false);
  76.     this.setTitle("Login");
  77.     jLabel3.setBackground(Color.white);
  78.     jLabel3.setForeground(Color.green);
  79.     jLabel3.setBorder(BorderFactory.createEtchedBorder());
  80.     jLabel3.setText("                                        小型超市管理系统");
  81.     jLabel4.setBorder(BorderFactory.createEtchedBorder());
  82.     jLabel4.setText(" ——制作人:高凯");
  83.     jLabel5.setBorder(BorderFactory.createEtchedBorder());
  84.     jLabel5.setToolTipText("");
  85.     jLabel5.setText("——版本号: 1.1");
  86.     jTextField1.setBorder(titledBorder2);
  87.     jLabel6.setBorder(BorderFactory.createEtchedBorder());
  88.     getContentPane().add(panel1);
  89.     panel1.add(jPanel1, BorderLayout.WEST);
  90.     jPanel1.add(jLabel3,     new XYConstraints(-1, 0, 395, 30));
  91.     jPanel1.add(jLabel5,     new XYConstraints(274, 30, 121, 32));
  92.     jPanel1.add(jLabel4, new XYConstraints(127, 32, 143, 30));
  93.     jPanel1.add(jPasswordField1, new XYConstraints(143, 196, 132, 24));
  94.     jPanel1.add(jTextField1, new XYConstraints(143, 136, 132, 24));
  95.     jPanel1.add(jLabel2, new XYConstraints(23, 195, 60, 25));
  96.     jPanel1.add(jLabel1, new XYConstraints(23, 134, 60, 25));
  97.     jPanel1.add(jLabel6,   new XYConstraints(-2, 65, 396, 204));
  98.     jPanel1.add(jButton1, new XYConstraints(90, 280, 68, 28));
  99.     jPanel1.add(jButton2, new XYConstraints(193, 281, 69, 28));
  100.   }
  101.   public void actionPerformed(ActionEvent e){
  102.     //the user login
  103.     if(e.getSource() == jButton1){
  104.       try{
  105.         String userAuthority = null ;
  106.         String userName = null ;
  107.         String userPassword = null ;
  108.         String testName = jTextField1.getText().toString() ;
  109.         String testPassword = String.valueOf(jPasswordField1.getPassword()) ;
  110.         Statement sql = database1.createStatement() ;
  111.         ResultSet rs = sql.executeQuery("SELECT userName,userPassword , userAuthority FROM [UML_CS].[dbo].[user] WHERE userName = '"+testName+"'") ;
  112.         while(rs.next()){
  113.           userName = rs.getString("userName") ;
  114.           userPassword = rs.getString("userPassword") ;
  115.           userAuthority = rs.getString("userAuthority") ;
  116.         }
  117.         verifyUser(userName,userPassword,testPassword,userAuthority);
  118.         cancel() ;
  119.       }
  120.       catch(SQLException eee){eee.printStackTrace();}
  121.     }
  122.     //exit the System
  123.     else if(e.getSource() == jButton2){
  124.       cancel();
  125.     }
  126.   }
  127.   void cancel(){
  128.     dispose();
  129.   }
  130.   //验证用户名以及密码
  131.   void verifyUser(String userName , String userPassword , String testPassword, String userAuthority){
  132.     if(userName == null){
  133.       JOptionPane.showMessageDialog(null,"用户名不存在,请先注册","verify user",JOptionPane.DEFAULT_OPTION);
  134.     }
  135.     else{
  136.       if(userPassword.equals(testPassword) == false){
  137.         JOptionPane.showMessageDialog(null,"您输入的密码不正确,请重试","verify password" , JOptionPane.DEFAULT_OPTION);
  138.       }
  139.       else{
  140.         JOptionPane.showMessageDialog(null,"success","",JOptionPane.DEFAULT_OPTION);
  141.         verifyUserAuthority(userAuthority) ;
  142.       }
  143.     }
  144.   }
  145.   //验证用户权限
  146.   void verifyUserAuthority(String userAuthority){
  147.     if(userAuthority.equals("admin")){
  148.       AdminWindow adminWindow = new AdminWindow() ;
  149.       Dimension dlgSize = adminWindow.getPreferredSize();
  150.       Dimension frmSize = getSize();
  151.       Point loc = getLocation();
  152.       adminWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  153.       adminWindow.setModal(true);
  154.       adminWindow.pack();
  155.       adminWindow.show();
  156.     }
  157.     else if (userAuthority.equals("customer")){
  158.       CustomerWindow customerWindow =new CustomerWindow() ;
  159.       Dimension dlgSize = customerWindow.getPreferredSize();
  160.       Dimension frmSize = getSize();
  161.       Point loc = getLocation();
  162.       customerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  163.       customerWindow.setModal(true);
  164.       customerWindow.pack();
  165.       customerWindow.show();
  166.     }
  167.     else if (userAuthority.equals("buyer")){
  168.       BuyerWindow buyerWindow =new BuyerWindow() ;
  169.       Dimension dlgSize = buyerWindow.getPreferredSize();
  170.       Dimension frmSize = getSize();
  171.       Point loc = getLocation();
  172.       buyerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  173.       buyerWindow.setModal(true);
  174.       buyerWindow.pack();
  175.       buyerWindow.show();
  176.     }
  177.     else if (userAuthority.equals("saler")){
  178.       SalesWindow salesWindow =new SalesWindow() ;
  179.       Dimension dlgSize = salesWindow.getPreferredSize();
  180.       Dimension frmSize = getSize();
  181.       Point loc = getLocation();
  182.       salesWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  183.       salesWindow.setModal(true);
  184.       salesWindow.pack();
  185.       salesWindow.show();
  186.     }
  187.     else if (userAuthority.equals("manager")){
  188.      ManagementWindow managementWindow =new ManagementWindow() ;
  189.      Dimension dlgSize = managementWindow.getPreferredSize();
  190.      Dimension frmSize = getSize();
  191.      Point loc = getLocation();
  192.      managementWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  193.      managementWindow.setModal(true);
  194.      managementWindow.pack();
  195.      managementWindow.show();
  196.     }
  197.   }
  198. }