Logincs.java~59~
上传用户: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.   public Logincs(Frame frame, String title, boolean modal) {
  30.     super(frame, title, modal);
  31.     try {
  32.       jbInit();
  33.       pack();
  34.     }
  35.     catch(Exception ex) {
  36.       ex.printStackTrace();
  37.     }
  38.   }
  39.   public Logincs() {
  40.     this(null, "", false);
  41.   }
  42.   private void jbInit() throws Exception {
  43.     panel1.setLayout(borderLayout1);
  44.     jPanel1.setLayout(xYLayout1);
  45.     jLabel1.setText("用户名");
  46.     jLabel2.setText("密码");
  47.     jButton1.setText("登录");
  48.     jButton1.addActionListener(this);
  49.     jButton2.setToolTipText("");
  50.     jButton2.setText("取消");
  51.     jButton2.addActionListener(this);
  52.     jPanel1.setMinimumSize(new Dimension(600, 600));
  53.     jPanel1.setPreferredSize(new Dimension(400, 300));
  54.     jPasswordField1.setToolTipText("");
  55.     database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:odbc:gaokaiSQL", "sa", "", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
  56.     queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "SELECT [user].userName,[user].userPassword,[user].userAuthority n" +
  57.         "FROM [UML_CS].[dbo].[user]", null, true, Load.ALL));
  58.     this.setResizable(false);
  59.     this.setTitle("Login");
  60.     getContentPane().add(panel1);
  61.     panel1.add(jPanel1, BorderLayout.CENTER);
  62.     jPanel1.add(jLabel1,  new XYConstraints(73, 60, 75, 26));
  63.     jPanel1.add(jLabel2,  new XYConstraints(74, 100, 85, 26));
  64.     jPanel1.add(jTextField1,  new XYConstraints(179, 64, 132, 24));
  65.     jPanel1.add(jPasswordField1,  new XYConstraints(179, 105, 133, 28));
  66.     jPanel1.add(jButton1, new XYConstraints(88, 196, 68, 28));
  67.     jPanel1.add(jButton2, new XYConstraints(237, 197, 69, -1));
  68.   }
  69.   public void actionPerformed(ActionEvent e){
  70.     //the user login
  71.     if(e.getSource() == jButton1){
  72.       try{
  73.         String userAuthority = null ;
  74.         String userName = null ;
  75.         String userPassword = null ;
  76.         String testName = jTextField1.getText().toString() ;
  77.         String testPassword = String.valueOf(jPasswordField1.getPassword()) ;
  78.         Statement sql = database1.createStatement() ;
  79.         ResultSet rs = sql.executeQuery("SELECT userName,userPassword , userAuthority FROM [UML_CS].[dbo].[user] WHERE userName = '"+testName+"'") ;
  80.         while(rs.next()){
  81.           userName = rs.getString("userName") ;
  82.           userPassword = rs.getString("userPassword") ;
  83.           userAuthority = rs.getString("userAuthority") ;
  84.         }
  85.         verifyUser(userName,userPassword,testPassword,userAuthority);
  86.         cancel() ;
  87.       }
  88.       catch(SQLException eee){eee.printStackTrace();}
  89.     }
  90.     //exit the System
  91.     else if(e.getSource() == jButton2){
  92.       cancel();
  93.     }
  94.   }
  95.   void cancel(){
  96.     dispose();
  97.   }
  98.   //验证用户名以及密码
  99.   void verifyUser(String userName , String userPassword , String testPassword, String userAuthority){
  100.     if(userName == null){
  101.       JOptionPane.showMessageDialog(null,"用户名不存在,请先注册","verify user",JOptionPane.DEFAULT_OPTION);
  102.     }
  103.     else{
  104.       if(userPassword.equals(testPassword) == false){
  105.         JOptionPane.showMessageDialog(null,"您输入的密码不正确,请重试","verify password" , JOptionPane.DEFAULT_OPTION);
  106.       }
  107.       else{
  108.         JOptionPane.showMessageDialog(null,"success","",JOptionPane.DEFAULT_OPTION);
  109.         verifyUserAuthority(userAuthority) ;
  110.       }
  111.     }
  112.   }
  113.   //验证用户权限
  114.   void verifyUserAuthority(String userAuthority){
  115.     if(userAuthority.equals("admin")){
  116.       AdminWindow adminWindow = new AdminWindow() ;
  117.       Dimension dlgSize = adminWindow.getPreferredSize();
  118.       Dimension frmSize = getSize();
  119.       Point loc = getLocation();
  120.       adminWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  121.       adminWindow.setModal(true);
  122.       adminWindow.pack();
  123.       adminWindow.show();
  124.     }
  125.     else if (userAuthority.equals("customer")){
  126.       CustomerWindow customerWindow =new CustomerWindow() ;
  127.       Dimension dlgSize = customerWindow.getPreferredSize();
  128.       Dimension frmSize = getSize();
  129.       Point loc = getLocation();
  130.       customerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  131.       customerWindow.setModal(true);
  132.       customerWindow.pack();
  133.       customerWindow.show();
  134.     }
  135.     else if (userAuthority.equals("buyer")){
  136.       BuyerWindow buyerWindow =new BuyerWindow() ;
  137.       Dimension dlgSize = buyerWindow.getPreferredSize();
  138.       Dimension frmSize = getSize();
  139.       Point loc = getLocation();
  140.       buyerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  141.       buyerWindow.setModal(true);
  142.       buyerWindow.pack();
  143.       buyerWindow.show();
  144.     }
  145.     else if (userAuthority.equals("saler")){
  146.       SalesWindow salesWindow =new SalesWindow() ;
  147.       Dimension dlgSize = salesWindow.getPreferredSize();
  148.       Dimension frmSize = getSize();
  149.       Point loc = getLocation();
  150.       salesWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  151.       salesWindow.setModal(true);
  152.       salesWindow.pack();
  153.       salesWindow.show();
  154.     }
  155.   }
  156. }