首先要在Oracle数据库创建表文件,包括建立表头以及关键字(唯一标识符),此次程序所用的表名称为SW_USER_INFO,表头有UNAME、UKEY、USEX等,关键字为UCC,然后添加一条记录,用户名称为system,密码为1.登陆原理为通过链接Oracle数据库,然后搜索表名称,根据关键字锁定记录,获取记录信息与登陆界面输入信息进行比对,信息一致即可登陆成功,反之则不通过。连接Oracle数据库代码如下:
1 import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6
7 public class Loading_F {
8
9 public String name = "";
10 public int sex = -1 ;
11 public String get_Verification_INFO(String UNAME) throws SQLException{
12 String psw = "";
13
14 Connection con = null;
15 try
16 {
17 Class.forName("oracle.jdbc.driver.OracleDriver");
18 String url = "jdbc:oracle:" + "thin:@IP地址:端口号:标识符";
19 String user = "数据库登陆名";
20 String password = "口令";
21 con = DriverManager.getConnection(url, user, password);
22 } catch (Exception e) {
23 e.printStackTrace();
24 }
25
26 PreparedStatement pre = null;
27 ResultSet result = null;
28 String sql = "select * from SW_USER_INFO where UACC=?";
29 pre = con.prepareStatement(sql);
30 pre.setString(1, UNAME);
31 result = pre.executeQuery();
32 while (result.next()){
33 name = result.getString("UNAME");
34 psw = result.getString("UKEY");
35 sex = result.getInt("USEX");
36 }
37 con.close();
38 return psw;
39 }
40 }
这个里面涉及Oracle数据库表的协议(表内容的具体含义),具体协议内容不具体说明,望各位谅解,这个协议由Oracle数据库用户根据使用内容自己定义。接下来就是用户登陆界面的设计主要具有三个功能:登陆、退出和重置。具体代码如下:
1 import java.awt.Color;
2 import java.awt.Dimension;
3 import java.awt.Font;
4 import java.awt.Image;
5 import java.awt.Toolkit;
6 import java.awt.event.KeyAdapter;
7 import java.awt.event.KeyEvent;
8 import java.awt.event.MouseAdapter;
9 import java.awt.event.MouseEvent;
10 import java.sql.SQLException;
11
12 import javax.swing.ImageIcon;
13 import javax.swing.JButton;
14 import javax.swing.JFrame;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JPasswordField;
18 import javax.swing.JTextField;
19 import javax.swing.border.LineBorder;
20
21 public class Loading_F {
22
23 JFrame f1 = new JFrame("OERI-驾校管理系统-登陆");
24 static JTextField jt1 = new JTextField();
25 JPasswordField jt2 = new JPasswordField();
26 JTextField jt3 = new JTextField();
27 JButton btn1 = new JButton("确 定");
28 JButton btn2 = new JButton("退 出");
29 JButton btn3 = new JButton("重置");
30 String str1 = new String();
31
32 public static void main(String[] args) {
33 Loading_F lf = new Loading_F();
34 lf.addFormFunction();
35 lf.setform();
36 }
37 void Start_Loading_Form(){
38 addFormFunction();
39 setform();
40 }
41
42 void setform(){
43 f1.setSize(780, 400);
44 f1.setResizable(false);
45 setFrameCenter(f1);
46
47 String path = "bgp/background01.jpg";
48 ImageIcon background = new ImageIcon(path);
49 JLabel label = new JLabel(background);
50 label.setBounds(0, 0, f1.getWidth(), f1.getHeight());
51 JPanel imagePanel = (JPanel) f1.getContentPane();
52 imagePanel.setOpaque(false);
53 f1.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
54
55 JLabel jl1 = new JLabel("请输入用户信息:");
56 jl1.setBounds(480, 70, 180, 50);
57 jl1.setFont(new Font("宋体", Font.BOLD, 17));
58 jl1.setForeground(Color.WHITE);
59 f1.getLayeredPane().add(jl1, new Integer(Integer.MAX_VALUE));
60
61 JLabel jl2 = new JLabel("用户名:");
62 jl2.setBounds(480, 140, 180, 30);
63 jl2.setFont(new Font("宋体", Font.BOLD, 13));
64 jl2.setForeground(Color.WHITE);
65 f1.getLayeredPane().add(jl2, new Integer(Integer.MAX_VALUE));
66
67 JLabel jl3 = new JLabel("密 码:");
68 jl3.setBounds(480, 180, 180, 30);
69 jl3.setFont(new Font("宋体", Font.BOLD, 13));
70 jl3.setForeground(Color.WHITE);
71 f1.getLayeredPane().add(jl3, new Integer(Integer.MAX_VALUE));
72
73 jt1.setBounds(535, 140, 180, 30);
74 jt1.setBorder(new LineBorder(Color.white));
75 jt1.setFont(new Font("宋体", Font.BOLD, 15));
76 jt1.addMouseListener(new MouseAdapter(){
77 public void mouseEntered(MouseEvent e){
78 jt1.setBorder(new LineBorder(Color.black));
79 }
80 public void mouseExited(MouseEvent e){
81 jt1.setBorder(new LineBorder(Color.white));
82 }
83 });
84 f1.getLayeredPane().add(jt1, new Integer(Integer.MAX_VALUE));
85
86 jt2.setBounds(535, 180, 180, 30);
87 jt2.setEchoChar('*');
88 jt2.setFont(new Font("宋体", Font.BOLD, 15));
89 jt2.setBorder(new LineBorder(Color.white));
90 jt2.addMouseListener(new MouseAdapter(){
91 public void mouseEntered(MouseEvent e){
92 jt2.setBorder(new LineBorder(Color.black));
93 }
94 public void mouseExited(MouseEvent e){
95 jt2.setBorder(new LineBorder(Color.white));
96 }
97 });
98 f1.getLayeredPane().add(jt2, new Integer(Integer.MAX_VALUE));
99
100 btn1.setBounds(488, 240, 65, 30);
101 btn1.setBorder(new LineBorder(Color.gray));
102 btn1.addMouseListener(new MouseAdapter(){
103 public void mouseEntered(MouseEvent e){
104 btn1.setBorder(new LineBorder(Color.black));
105 }
106 public void mouseExited(MouseEvent e){
107 btn1.setBorder(new LineBorder(Color.gray));
108 }
109 });
110 f1.getLayeredPane().add(btn1, new Integer(Integer.MAX_VALUE));
111
112 btn2.setBounds(568, 240, 65, 30);
113 btn2.setBorder(new LineBorder(Color.gray));
114 btn2.addMouseListener(new MouseAdapter(){
115 public void mouseEntered(MouseEvent e){
116 btn2.setBorder(new LineBorder(Color.black));
117 }
118 public void mouseExited(MouseEvent e){
119 btn2.setBorder(new LineBorder(Color.gray));
120 }
121 });
122 f1.getLayeredPane().add(btn2, new Integer(Integer.MAX_VALUE));
123
124 btn3.setBounds(648, 240, 65, 30);
125 btn3.setBorder(new LineBorder(Color.gray));
126 btn3.addMouseListener(new MouseAdapter(){
127 public void mouseEntered(MouseEvent e){
128 btn3.setBorder(new LineBorder(Color.black));
129 }
130 public void mouseExited(MouseEvent e){
131 btn3.setBorder(new LineBorder(Color.gray));
132 }
133 });
134 f1.getLayeredPane().add(btn3, new Integer(Integer.MAX_VALUE));
135
136 jt3.setBounds(490, 323, 240, 30);
137 jt3.setBackground(new Color(255,255,255,0));
138 jt3.setOpaque(false);
139 jt3.setBorder(null);
140 jt3.setFont(new Font("黑体", Font.BOLD, 13));
141 jt3.setForeground(Color.WHITE);
142 jt3.setEditable(false);
143 f1.getLayeredPane().add(jt3, new Integer(Integer.MAX_VALUE));
144
145 Image imageIcon = Toolkit.getDefaultToolkit().getImage("bgp/tubiao.png");
146 f1.setIconImage(imageIcon);
147 f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
148 f1.setVisible(true);
149 }
150 void setFrameCenter(JFrame jf){
151 Toolkit tl = Toolkit.getDefaultToolkit();
152 Dimension d = tl.getScreenSize();
153 double ScreenHeigth = d.getHeight();
154 double ScreenWidth = d.getWidth();
155 int FrameWidth = jf.getWidth();
156 int FrameHeigth = jf.getHeight();
157 int Higth = (int)(ScreenHeigth - FrameHeigth)/2;
158 int Width = (int) (ScreenWidth - FrameWidth)/2;
159 jf.setLocation(Width, Higth);
160 }
161 void Password_Verification() throws SQLException{
162 String name = jt1.getText();
163 char[] passw = jt2.getPassword();
164 String entered_password = new String(passw);
165
166 if(name.equals("")){
167 str1 = "请输入用户名称!";
168 jt3.setText(str1);
169 }else if(entered_password.equals("")){
170 str1 = "请输入密码!";
171 jt3.setText(str1);
172 }else{
173 str1 = "正在验证登录信息…";
174 jt3.setText(str1);
175 function_res.Loading_F frlf = new function_res.Loading_F();
176 String password = frlf.get_Verification_INFO(name);
177 if(password.equals("")){
178 str1 = "未找到该用户!";
179 jt3.setText(str1);
180 }else{
181 if(password.equals(entered_password)){
182 str1 = "信息正确,正在登录…";
183 jt3.setText(str1);
184 f1.setVisible(false);
185 f1.dispose();
186 Major_F mf = new Major_F();
187 mf.str0 = frlf.name;
188 mf.usex = frlf.sex;
189 mf.setform();
190 mf.setFunction();
191 }else{
192 str1 = "密码错误,请在确认无误后再次输入!";
193 jt3.setText(str1);
194 }
195 }
196 }
197 }
198 void addFormFunction(){
199 btn1.addMouseListener(new MouseAdapter(){
200 public void mouseClicked(MouseEvent e){
201 try {
202 Password_Verification();
203 } catch (SQLException e1) {
204 e1.printStackTrace();
205 }
206 }
207 });
208 btn1.addKeyListener(new KeyAdapter(){
209 @SuppressWarnings("static-access")
210 public void keyPressed(KeyEvent ke){
211 if(ke.getKeyChar() == ke.VK_ENTER){
212 try {
213 Password_Verification();
214 } catch (SQLException e1){
215 e1.printStackTrace();
216 }
217 }
218 }
219 });
220
221 btn2.addMouseListener(new MouseAdapter(){
222 public void mouseClicked(MouseEvent e){
223 System.exit(0);
224 }
225 });
226
227 btn3.addMouseListener(new MouseAdapter(){
228 public void mouseClicked(MouseEvent e){
229 jt1.setText("");
230 jt2.setText("");
231 jt3.setText("");
232 }
233 });
234
235 }
236 }
运行效果图如下:
2018年1月27日于青岛
手机扫一扫
移动阅读更方便
你可能感兴趣的文章