java基础之登录程序

注:此版本仅供学习使用!

Login.java

  1 import java.awt.Font;
  2 import java.awt.event.*;
  3 import java.sql.*;
  4 
  5 import javax.swing.*;
  6 import javax.swing.border.EmptyBorder;
  7 
  8 @SuppressWarnings("serial")
  9 public class Login extends JFrame {
 10     JLabel jla_tips;
 11     JLabel jla_username;
 12     JTextField jtf_username;
 13     JLabel jla_password;
 14     JPasswordField jpf_password;
 15     JButton jbt_login;
 16     JButton jbt_cancel;
 17     int count=0;
 18 
 19     public Login() {
 20         ImageIcon icon = new ImageIcon("images/favicon.png");
 21         setIconImage(icon.getImage());
 22 
 23         JPanel contentPane = new JPanel();
 24         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 25         setContentPane(contentPane);
 26         contentPane.setLayout(null);
 27 
 28         jla_tips = new JLabel();
 29         jla_tips.setBounds(155, 40, 200, 25);
 30         contentPane.add(jla_tips);
 31 
 32         jla_username = new JLabel("用户名:");
 33         jla_username.setBounds(80, 70, 60, 25);
 34         contentPane.add(jla_username);
 35 
 36         jtf_username = new JTextField();
 37         jtf_username.setHorizontalAlignment(JTextField.LEFT);
 38         jtf_username.setBounds(150, 70, 150, 25);
 39         contentPane.add(jtf_username);
 40         
 41         jla_password = new JLabel("密  码:");
 42         jla_password.setBounds(80, 120, 60, 25);
 43         contentPane.add(jla_password);
 44         
 45         jpf_password = new JPasswordField();
 46         jpf_password.setBounds(150, 120, 150, 25);
 47         contentPane.add(jpf_password);
 48         
 49         // 登录按钮
 50         jbt_login = new JButton("登录");
 51         jbt_login.setFocusPainted(false);
 52         jbt_login.setBounds(150, 170, 70, 30);
 53         contentPane.add(jbt_login);
 54         jbt_login.addActionListener(new loginListener());
 55         jpf_password.addActionListener(new loginListener());
 56 
 57         // 置空按钮
 58         jbt_cancel = new JButton("取消");
 59         jbt_cancel.setFocusPainted(false);
 60         jbt_cancel.setBounds(235, 170, 70, 30);
 61         contentPane.add(jbt_cancel);
 62         jbt_cancel.addActionListener(new cancelListener());
 63         
 64         // 背景图
 65         Icon login3 = new ImageIcon("images/login3.jpg");
 66         JLabel jla_login3 = new JLabel(login3);
 67         jla_login3.setBounds(0, 0, 450, 300);
 68         contentPane.add(jla_login3);
 69         
 70         // 字体设置
 71         Font fontstr = new Font("宋体", Font.PLAIN, 14);
 72         jla_username.setFont(fontstr);
 73         jla_password.setFont(fontstr);
 74         jla_tips.setFont(fontstr);
 75         jbt_login.setFont(fontstr);
 76         jbt_cancel.setFont(fontstr);
 77 
 78         setTitle("登录");
 79         setSize(450, 300);
 80         setLocationRelativeTo(null);
 81         setResizable(false);
 82         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 83         setVisible(true);
 84     }
 85 
 86     public class cancelListener implements ActionListener{
 87         @Override
 88         public void actionPerformed(ActionEvent arg0) {
 89             jtf_username.setText(null);
 90             jpf_password.setText(null);
 91         }
 92         
 93     }
 94     public class loginListener implements ActionListener {
 95         @Override
 96         public void actionPerformed(ActionEvent e) {
 97             String username = jtf_username.getText().trim();
 98             char[] values = jpf_password.getPassword();
 99             String password = new String(values);
100             if (username != null && username.length() != 0 && password != null
101                     && password.length() != 0) {
102                 try {
103                     // 1.注册驱动
104                     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
105                     // 2.建立连接
106                     // 2.1连接串
107                     String DBurl = "jdbc:sqlserver://localhost:1433;DatabaseName=JavaDB";
108                     String DBname="sa";
109                     String DBpwd="940523";
110                     Connection con = DriverManager.getConnection(DBurl,DBname,DBpwd);
111                     
112                     // 创建会话
113 //                    Statement st = con.createStatement();
114 //                    String sql = "select count(*) as cnt from T_user where username='"
115 //                            + username + "' and password='" + password + "'";
116 //                    ResultSet rs = st.executeQuery(sql);
117                     
118                     /*
119                      * 防止SQL注入
120                      */
121                     String sql = "select count(*) as cnt from T_user where username=? and password=?";
122                     PreparedStatement st = con.prepareStatement(sql);
123                     st.setString(1, username);
124                     st.setString(2, password);
125                     String sql2 = "select count(*) as cnt from T_user where username=?";
126                     PreparedStatement st2 = con.prepareStatement(sql2);
127                     st2.setString(1, username);
128                     // 结果集
129                     ResultSet rs = st.executeQuery();
130                     ResultSet rs2 = st2.executeQuery();
131 
132                     rs.next();
133                     rs2.next();
134                     if (rs.getInt("cnt") > 0) {
135                         setVisible(false);
136                         new Hello();
137                     } else if (rs2.getInt("cnt") == 0) {
138                         JOptionPane.showMessageDialog(null, "无该用户!");
139                     }else {
140                         jla_tips.setText("密码错误!");
141                         count++;
142                         if(count==3){
143                             JOptionPane.showMessageDialog(null, "登录失败超过三次!");
144                             System.exit(0);
145                         }
146                     }
147                 } catch (Exception ex) {
148                     ex.printStackTrace();
149                 }
150             } else {
151                 if (username.length() == 0 && password.length() == 0)
152                     jla_tips.setText("帐号与密码均不能为空!");
153                 else if (username.length() == 0)
154                     jla_tips.setText("帐号不能为空!");
155                 else if (password.length() == 0)
156                     jla_tips.setText("密码不能为空!");
157             }
158         }
159     }
160 
161     public static void main(String[] args) {
162         new Login();
163     }
164 }

hello.java

 1 import java.awt.Font;
 2 import javax.swing.*;
 3 import javax.swing.border.EmptyBorder;
 4 
 5 @SuppressWarnings("serial")
 6 public class Hello extends JFrame {
 7     JLabel jla_title;
 8 
 9     public Hello() {
10         ImageIcon icon = new ImageIcon("images/favicon.png");
11         setIconImage(icon.getImage());
12 
13         JPanel contentPane = new JPanel();
14         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
15         setContentPane(contentPane);
16         contentPane.setLayout(null);
17 
18         jla_title = new JLabel();
19         jla_title.setText("欢迎使用本软件!");
20         jla_title.setBounds(120, 100, 200, 25);
21         // 字体设置
22         Font fontstr = new Font("宋体", Font.PLAIN, 26);
23         jla_title.setFont(fontstr);
24         contentPane.add(jla_title);
25 
26         setTitle("欢迎界面");
27         setSize(450, 300);
28         setLocationRelativeTo(null);
29         setResizable(false);
30         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31         setVisible(true);
32     }
33 
34     // public static void main(String[] args) {
35     // new Hello();
36     // }
37 }

下载链接: http://pan.baidu.com/s/1pJAtf1T 密码: 1kkz

原文地址:https://www.cnblogs.com/hehaiyang/p/3678235.html