JAVA--图书管理员登陆系统

效果图:

 

先用sqlyog连接数据库创建新的数据库并创建一个表,如图:

 

数据库连接代码

 

 1 package com.shine.util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 
 6 /**
 7  * 数据库工具类
 8  * @author 75476
 9  *
10  */
11 public class Db_util {
12     private String dburl="jdbc:mysql://localhost:3306/gb_book";//数据库地址连接
13     private String dbUserName="root";
14     private String dbPassword="754764";
15     private String jdbcName="com.mysql.jdbc.Driver";
16     /**
17      * 获取数据库连接
18      * @return
19      * @throws Exception
20      */
21     public Connection getCon()throws Exception{
22         Class.forName(jdbcName);
23         Connection con=    DriverManager.getConnection(dburl, dbUserName, dbPassword);
24         return con;
25         
26     }
27     /**
28      * 关闭数据库连接
29      * @param con
30      * @throws Exception
31      */
32     public void closeCon(Connection con) throws Exception{
33         if(con!=null)
34             con.close();
35     }
36     public static void main(String[] args) {
37         Db_util dbutil=new Db_util();
38         try {
39             dbutil.getCon();
40             System.out.println("数据库连接成功");
41         } catch (Exception e) {
42             // TODO 自动生成的 catch 块
43             e.printStackTrace();
44             System.out.println("数据库连接失败");
45         }
46         
47     }
48 }

用户实体代码,

 1 package com.shine.model;
 2 /**
 3  * 用户实体
 4  * @author 75476
 5  *
 6  */
 7 public class User {
 8     private int id;
 9     private String userName;
10     private String Password;
11     
12     public User() {
13         super();
14         
15     }
16     
17     public User(String userName, String password) {
18         super();
19         this.userName = userName;
20         Password = password;
21     }
22 
23     
24     public int getId() {
25         return id;
26     }
27     public void setId(int id) {
28         this.id = id;
29     }
30     public String getUserName() {
31         return userName;
32     }
33     public void setUserName(String userName) {
34         this.userName = userName;
35     }
36     public String getPassword() {
37         return Password;
38     }
39     public void setPassword(String password) {
40         Password = password;
41     }
42     
43     
44 }

 

用户dao类

 1 package com.shine.dao;
 2 
 3 
 4 
 5 import java.sql.Connection;
 6 import java.sql.PreparedStatement;
 7 import java.sql.ResultSet;
 8 
 9 import com.shine.model.User;
10 
11 /**
12  * 用户Dao类
13  * @author 75476
14  *
15  */
16 public class UserDao {
17     /**
18      * 登陆验证
19      * @param con
20      * @param user
21      * @return
22      * @throws Exception
23      */
24     public User login(Connection con,User user)throws Exception{
25         User resultUser=null;
26         String sql="select * from t_user where userName=? and Password=?";
27         PreparedStatement pstmt =con.prepareStatement(sql);
28         pstmt.setString(1, user.getUserName());
29         pstmt.setString(2, user.getPassword());
30         ResultSet rs=pstmt.executeQuery();
31         if(rs.next()){
32             resultUser=new User();
33             resultUser.setId(rs.getInt("id"));
34             resultUser.setUserName(rs.getString("userName"));
35             resultUser.setPassword(rs.getString("Password"));
36         }
37         return resultUser;
38         
39     }
40 
41 }

 

登陆界面设计,动作监听,这里用的是windowbuilder插件

  1 package com.shine.view;
  2 
  3 import java.awt.EventQueue;
  4 import java.awt.Font;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import java.sql.Connection;
  8 
  9 import javax.swing.GroupLayout;
 10 import javax.swing.GroupLayout.Alignment;
 11 import javax.swing.ImageIcon;
 12 import javax.swing.JButton;
 13 import javax.swing.JFrame;
 14 import javax.swing.JLabel;
 15 import javax.swing.JOptionPane;
 16 import javax.swing.JPanel;
 17 import javax.swing.JPasswordField;
 18 import javax.swing.JTextField;
 19 import javax.swing.border.EmptyBorder;
 20 
 21 import com.shine.dao.UserDao;
 22 import com.shine.model.User;
 23 import com.shine.util.Db_util;
 24 import com.shine.util.Stringutil;
 25 
 26 public class logOn extends JFrame {
 27 
 28     private Db_util dbutil=new Db_util();
 29     private UserDao userDao=new UserDao();
 30     private JPanel contentPane;
 31     private JTextField userNameTxt;
 32     private JPasswordField PasswordTxt;
 33 
 34     /**
 35      * Launch the application.
 36      */
 37     public static void main(String[] args) {
 38         EventQueue.invokeLater(new Runnable() {
 39             public void run() {
 40                 try {
 41                     logOn frame = new logOn();
 42                     frame.setVisible(true);
 43                 } catch (Exception e) {
 44                     e.printStackTrace();
 45                 }
 46             }
 47         });
 48     }
 49 
 50     /**
 51      * Create the frame.
 52      */
 53     public logOn() {
 54         setResizable(false);
 55         setTitle("\u7BA1\u7406\u5458\u767B\u9646");
 56         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 57         setBounds(100, 100, 665, 532);
 58         contentPane = new JPanel();
 59         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 60         setContentPane(contentPane);
 61         
 62         JLabel lblNewLabel = new JLabel("\u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF");
 63         lblNewLabel.setFont(new Font("宋体", Font.BOLD, 22));
 64         lblNewLabel.setIcon(new ImageIcon(logOn.class.getResource("/imags/558952.gif")));
 65         
 66         JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D");
 67         lblNewLabel_1.setIcon(new ImageIcon(logOn.class.getResource("/imags/userName.png")));
 68         lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 20));
 69         
 70         JLabel lblNewLabel_2 = new JLabel("\u5BC6  \u7801");
 71         lblNewLabel_2.setIcon(new ImageIcon(logOn.class.getResource("/imags/password.png")));
 72         lblNewLabel_2.setFont(new Font("宋体", Font.BOLD, 20));
 73         
 74         userNameTxt = new JTextField();
 75         userNameTxt.setColumns(10);
 76         
 77         PasswordTxt = new JPasswordField();
 78         
 79         JButton btnNewButton = new JButton("\u767B \u9646");
 80         btnNewButton.addActionListener(new ActionListener() {
 81             public void actionPerformed(ActionEvent e) {
 82                 logInActionperformed(e);
 83             }
 84         });
 85         btnNewButton.setIcon(new ImageIcon(logOn.class.getResource("/imags/login.png")));
 86         btnNewButton.setFont(new Font("宋体", Font.BOLD, 18));
 87         
 88         JButton button = new JButton("\u91CD \u7F6E");
 89         button.addActionListener(new ActionListener() {
 90             public void actionPerformed(ActionEvent e) {
 91                 resetValueActionperformed(e);
 92             }
 93         });
 94         button.setIcon(new ImageIcon(logOn.class.getResource("/imags/reset.png")));
 95         button.setFont(new Font("宋体", Font.BOLD, 18));
 96         GroupLayout gl_contentPane = new GroupLayout(contentPane);
 97         gl_contentPane.setHorizontalGroup(
 98             gl_contentPane.createParallelGroup(Alignment.LEADING)
 99                 .addGroup(gl_contentPane.createSequentialGroup()
100                     .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
101                         .addGroup(gl_contentPane.createSequentialGroup()
102                             .addGap(163)
103                             .addComponent(lblNewLabel))
104                         .addGroup(gl_contentPane.createSequentialGroup()
105                             .addGap(101)
106                             .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
107                                 .addGroup(gl_contentPane.createSequentialGroup()
108                                     .addComponent(lblNewLabel_1)
109                                     .addGap(19))
110                                 .addGroup(gl_contentPane.createSequentialGroup()
111                                     .addComponent(lblNewLabel_2)
112                                     .addGap(18)))
113                             .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
114                                 .addComponent(userNameTxt, GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
115                                 .addComponent(PasswordTxt, GroupLayout.PREFERRED_SIZE, 267, GroupLayout.PREFERRED_SIZE)))
116                         .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
117                             .addContainerGap()
118                             .addComponent(btnNewButton)
119                             .addGap(94)
120                             .addComponent(button, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)))
121                     .addGap(179))
122         );
123         gl_contentPane.setVerticalGroup(
124             gl_contentPane.createParallelGroup(Alignment.LEADING)
125                 .addGroup(gl_contentPane.createSequentialGroup()
126                     .addGap(35)
127                     .addComponent(lblNewLabel)
128                     .addGap(54)
129                     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
130                         .addComponent(lblNewLabel_1)
131                         .addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
132                     .addGap(47)
133                     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
134                         .addComponent(lblNewLabel_2)
135                         .addComponent(PasswordTxt, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
136                     .addGap(68)
137                     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
138                         .addComponent(button, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
139                         .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
140                     .addContainerGap(95, Short.MAX_VALUE))
141         );
142         contentPane.setLayout(gl_contentPane);
143     }
144     /**
145      * 登陆事件
146      * @param e
147      */
148 private void logInActionperformed(ActionEvent e) {
149         String userName=this.userNameTxt.getText();
150         String Password=new String(this.PasswordTxt.getPassword());
151         if(Stringutil.isEmpty(userName)){
152             JOptionPane.showMessageDialog(null, "用户名不可为空");
153             return;
154             
155         }
156         if(Stringutil.isEmpty(Password)){
157             JOptionPane.showMessageDialog(null, "密码不可为空");
158             return;
159         }
160         User user=new User(userName,Password);
161         Connection con=null;
162         try {
163             con=dbutil.getCon();
164             User currentUser=userDao.login(con, user);
165             if(currentUser!=null){
166                 JOptionPane.showMessageDialog(null, "登陆成功");
167                 
168             }else{
169                 JOptionPane.showMessageDialog(null, "用户名或密码错误!");
170             }
171         } catch (Exception e1) {
172             // TODO 自动生成的 catch 块
173             e1.printStackTrace();
174         }
175     }
176 
177 /**
178  * 重置事件处理
179  * @param e
180  */
181     protected void resetValueActionperformed(ActionEvent evt) {
182         this.userNameTxt.setText("");
183         this.PasswordTxt.setText("");
184     }
185 }

  

判断输入框是否为空

 1 package com.shine.util;
 2 /**
 3  * 字符串工具类
 4  * @author 75476
 5  *
 6  */
 7 public class Stringutil {
 8     public static  boolean isEmpty(String str){
 9     if(str==null||"".equals(str.trim())){
10         return true;
11         
12     }else{
13         return false;
14     }
15     }
16     public static  boolean isNotEmpty(String str){
17         if(str!=null||"".equals(str.trim())){
18             return true;
19             
20         }else{
21             return false;
22         }
23         }
24     }

 

 

附上一些常用的快捷键 

Alt + /  内容提示:
Ctrl + 1   提示
Ctrl + shift + O  导包
Ctrl + Shift + F  格式化代码块
Alt+上下键  代码位置调换 
Ctrl+/   添加/除去单行注释 
Ctrl+Shift+/   添加多行注释 
Ctrl+Shift+\   删除多行注释 
window->reset perspective  重置透视图 
Ctrl+Shift+X   更改为大写
Ctrl+Shift+Y  更改为小写 
 Ctrl+Alt+向下键   复制行
Ctrl+单击 需要查看源码的类
Ctrl+Shift+T  需要查看源码的类

shift+tab 左对齐

 

posted @ 2018-07-31 18:41  9529  阅读(4434)  评论(0编辑  收藏  举报