登錄注冊小代碼,將學過的一些小知識融合在一起進行了使用,加深印象。本例中如果有注釋不詳細的地方,詳見其它博客。
功能介紹:簡單的登錄注冊系統,使用了數據庫sqlserver、單例模式、正則表達式以及圖形化開發等知識。
1、在登錄界面,可以登錄或者注冊用戶。 注冊用戶界面,按照正則表達式規定的格式要求來輸入信息,若有誤,則重新輸入。
2、點擊注冊,首先連接SQLserver數據庫,連接成功則會判斷該用戶名是否已經存在,若存在,則給出提示。反之則進行注冊。
3、登錄界面,點擊登錄按鈕時,首先與數據庫建立連接。按照用戶名和密碼來向數據庫中查找,若有,則登錄成功。反之給出提示。
4、利用單例模式,實現了只創建類SQLserver的一個對象,大大節省了內存開銷 。
程序完整代碼見:https://github.com/chaohuangtianjie994/A-login-register-System
5、基于此,可以進行大量的拓展功能。
代碼如下:
UserRegister.java 登錄界面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
package package1; /* * 功能:登錄界面帶著注冊功能,彈出注冊界面。 * 將注冊的信息保存在數據庫中,并且可以進行登錄操作。 * author:ywq */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class UserRegister extends JFrame implements ActionListener{ //定義登錄界面的組件 JButton jb1,jb2,jb3= null ; JRadioButton jrb1,jrb2= null ; JPanel jp1,jp2,jp3= null ; JTextField jtf= null ; JLabel jlb1,jlb2= null ; JPasswordField jpf= null ; public static void main(String[] args) { UserRegister ur= new UserRegister(); } public UserRegister() { //創建組件 //創建組件 jb1= new JButton( "登錄" ); jb2= new JButton( "注冊" ); jb3= new JButton( "退出" ); //設置監聽 jb1.addActionListener( this ); jb2.addActionListener( this ); jb3.addActionListener( this ); jlb1= new JLabel( "用戶名:" ); jlb2= new JLabel( "密 碼:" ); jtf= new JTextField( 10 ); jpf= new JPasswordField( 10 ); jp1= new JPanel(); jp2= new JPanel(); jp3= new JPanel(); jp1.add(jlb1); jp1.add(jtf); jp2.add(jlb2); jp2.add(jpf); jp3.add(jb1); jp3.add(jb2); jp3.add(jb3); this .add(jp1); this .add(jp2); this .add(jp3); this .setVisible( true ); this .setResizable( false ); this .setTitle( "注冊登錄界面" ); this .setLayout( new GridLayout( 3 , 1 )); this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this .setBounds( 300 , 200 , 300 , 180 ); } @Override public void actionPerformed(ActionEvent e) { //監聽各個按鈕 if (e.getActionCommand()== "退出" ) { System.exit( 0 ); } else if (e.getActionCommand()== "登錄" ) { //調用登錄方法 this .login(); } else if (e.getActionCommand()== "注冊" ) { //調用注冊方法 this .Regis(); } } //注冊方法 public void Regis() { this .dispose(); //關閉當前界面 new UI(); //打開新界面 } //登錄方法 public void login() { SQLserver s= new SQLserver(); s.ConnectSQL(); s.SQLverify(jtf.getText(), jpf.getText()); this .jtf.setText( "" ); this .jpf.setText( "" ); } } |
UI.java 用于注冊的頁面顯示。使用了正則表達式來規定輸入的內容。注冊時候,需要首先判斷用戶名是否存在,若存在,則給出提示,反之進行注冊。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package package1; import java.awt.event.*; import java.awt.*; import javax.swing.*; /* * 注冊界面。 */ class UI extends JFrame implements ActionListener{ //定義組件 JFrame jf; JPanel jp; JLabel jl1,jl2,jl3,jl4; JTextField jtf1,jtf2,jtf3,jtf4; JButton jb1,jb2; public UI() { //初始化組件 jf= new JFrame(); jp= new JPanel(); jl1= new JLabel( "請輸入用戶名:" ); jtf1= new JTextField( 10 ); jtf1.setToolTipText( "用戶名必須為3-6位字母_或者數字" ); jl2= new JLabel( "請輸入密碼:" ); jtf2= new JTextField( 10 ); jtf2.setToolTipText( "密碼必須為6位字母_或者數字" ); jl3= new JLabel( "請輸入姓名:" ); jtf3= new JTextField( 10 ); jtf3.setToolTipText( "姓名必須漢字2-4位" ); jl4= new JLabel( "請輸入學號:" ); jtf4= new JTextField( 10 ); jtf4.setToolTipText( "學號必須為3-6位數字" ); jb1= new JButton( "返回" ); jb1.setToolTipText( "點我返回登錄界面哦" ); jb2= new JButton( "注冊" ); jb1.addActionListener( this ); jb2.addActionListener( this ); jp.setLayout( new GridLayout( 5 , 2 )); jp.add(jl1); jp.add(jtf1); jp.add(jl2); jp.add(jtf2); jp.add(jl3); jp.add(jtf3); jp.add(jl4); jp.add(jtf4); jp.add(jb1); jp.add(jb2); this .add(jp); this .setTitle( "注冊界面" ); this .setBounds( 200 , 100 , 250 , 150 ); this .setVisible( true ); this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // this.setResizable(false); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand()== "返回" ) { this .dispose(); new UserRegister(); // System.out.println("-------"); } else if (e.getActionCommand()== "注冊" ) { //調用注冊方法 this .zhuce(); } } public void zhuce() { String regex1= "\\w{3,6}" ; //用戶名必須是3-6位 boolean flag1=jtf1.getText().matches(regex1); String regex2= "\\w{6}" ; //密碼必須是6位 boolean flag2=jtf2.getText().matches(regex2); String regex3= "[\\u4e00-\\u9fa5]{2,4}" ; //姓名必須是漢字2-4個字 boolean flag3=jtf3.getText().matches(regex3); String regex4= "\\d{3,6}" ; //學號必須是3-6位 boolean flag4=jtf4.getText().matches(regex4); // if(jtf1.getText()==null||jtf2.getText()==null||jtf3.getText()==null||jtf4.getText()==null) if (flag1== false ) { JOptionPane.showMessageDialog( null , "用戶名填寫錯誤,必須為3-6位字母_或者數字" , "提示信息" , JOptionPane.WARNING_MESSAGE); jtf1.setText( "" ); } else if (flag2== false ) { JOptionPane.showMessageDialog( null , "密碼填寫錯誤,必須為6位字母_或者數字" , "提示信息" , JOptionPane.WARNING_MESSAGE); jtf2.setText( "" ); } else if (flag3== false ) { JOptionPane.showMessageDialog( null , "姓名填寫錯誤,必須漢字2-4位" , "提示信息" , JOptionPane.WARNING_MESSAGE); jtf3.setText( "" ); } else if (flag4== false ) { JOptionPane.showMessageDialog( null , "學號填寫錯誤,必須為3-6位數字" , "提示信息" , JOptionPane.WARNING_MESSAGE); jtf4.setText( "" ); } else { //調用注冊方法/先檢查要注冊的用戶名是否存在 SQLserver ss= new SQLserver(); ss.ConnectSQL(); ss.ZhuceVerify(jtf1.getText()); // ss.UserRegis(jtf1.getText(),jtf2.getText(),jtf3.getText(), jtf4.getText()); this .jtf1.setText( "" ); this .jtf2.setText( "" ); this .jtf3.setText( "" ); this .jtf4.setText( "" ); } } } |
SQLserver.java實現了與數據庫的連接以及查詢驗證等各個功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
package package1; import java.sql.*; import javax.swing.JOptionPane; /* * 與數據庫相關的操作,單獨封裝成類 */ class SQLserver { Connection ct; PreparedStatement ps; ResultSet rs; String user,pwd; //將連接數據庫的方法封裝為一個方法 public void ConnectSQL() { try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); //加載驅動 ct=DriverManager.getConnection( "jdbc:odbc:ywq" ); //得到連接 System.out.println( "已成功連接數據庫..." ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //注冊用戶的方法 public void UserRegis(String a,String b,String c,String d) { //創建火箭車 try { ps=ct.prepareStatement( "insert into users values(?,?,?,?)" ); ps.setString( 1 ,a); ps.setString( 2 ,b); ps.setString( 3 ,c); ps.setString( 4 ,d); //執行 int i=ps.executeUpdate(); if (i== 1 ) { JOptionPane.showMessageDialog( null , "注冊成功" , "提示消息" ,JOptionPane.WARNING_MESSAGE); } else { JOptionPane.showMessageDialog( null , "注冊失敗" , "提示消息" ,JOptionPane.ERROR_MESSAGE); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 登錄驗證方法 public void SQLverify(String a,String b) { try { ps=ct.prepareStatement( "select * from users where 用戶名=? and 密碼=? " ); ps.setString( 1 , a); ps.setString( 2 , b); // ResultSet結果集,大家可以把ResultSet理解成返回一張表行的結果集 rs = ps.executeQuery(); if (rs.next()) { user = rs.getString( 1 ); pwd = rs.getString( 2 ); JOptionPane.showMessageDialog( null , "登錄成功?。。?quot; , "提示消息" , JOptionPane.WARNING_MESSAGE); System.out.println( "成功獲取到密碼和用戶名from數據庫" ); System.out.println(user + "\t" + pwd + "\t" ); } else { JOptionPane.showMessageDialog( null , "用戶名或者密碼錯誤,請重新輸入!" , "提示消息" , JOptionPane.ERROR_MESSAGE); } } catch (SQLException e) { e.printStackTrace(); } } //注冊驗證方法,判斷用戶名是否已經存在 public void ZhuceVerify(String a) { try { ps=ct.prepareStatement( "select * from users where 用戶名=?" ); // System.out.println(ps); ps.setString( 1 , a); rs=ps.executeQuery(); if (rs.next()) { JOptionPane.showMessageDialog( null , "該用戶名已經存在" , "提示信息" , JOptionPane.WARNING_MESSAGE); } else { // 進行注冊 UI ui= new UI(); this .UserRegis(ui.jtf1.getText(),ui.jtf2.getText(),ui.jtf3.getText(),ui.jtf4.getText()); } } catch (SQLException e) { e.printStackTrace(); } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/qq_25827845/article/details/51549278