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