效果圖:
先用sqlyog連接配接資料庫建立新的資料庫并建立一個表,如圖:
資料庫連接配接代碼
1 package com.shine.util;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5
6
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
21 public Connection getCon()throws Exception{
22 Class.forName(jdbcName);
23 Connection con= DriverManager.getConnection(dburl, dbUserName, dbPassword);
24 return con;
25
26 }
27
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
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
16 public class UserDao {
17
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
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
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
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
181 protected void resetValueActionperformed(ActionEvent evt) {
182 this.userNameTxt.setText("");
183 this.PasswordTxt.setText("");
184 }
185 }
判斷輸入框是否為空
1 package com.shine.util;
2
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 左對齊