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

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