在上一次的博客中实现了一个仿QQ的登录界面,现在将实现一个注册QQ账号的功能,在本文中将制作一个QQ注册的页面,然后通过一些swing中的组件来获取用户的信息,最后通过点击注册按钮将注册信息保存到MySQL数据库中。
1.使用Navicat for MySQL创建一个数据库,取名为qq,再创建一个表,用来存储用户信息,取名为user,在user中添加一些属性,基本属性有:id, name, password, repassword, introduction, sex, pwdprotect, answer。这些都对应着后面的注册页面内容。如图所示:
2.设计整个注册页面的布局,基本思路就是使用BorderLayout,北部插入一张图片,南部用来摆放相关组件,页面效果如图所示:
创建一个RegistWindow类,这里用来构建注册页面的总构架,为了使代码结构更清晰,具体的添加面板以及在面板中添加组件的操作就放到另外一个类中来实现。现再创建一个CreatePanelForRegeist类,用来实现具体的组件添加以及为JButton实现点击事件。两个类代码如下:
package com.zys.regeist;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RegistWindow extends JFrame{
public static final String TITLE="QQ注册";
public static final int WIDTH=400;
public static final int HEIGHT=600;
public static final int X=440;
public static final int Y=100;
public RegistWindow() {
this.setSize(WIDTH,HEIGHT);
this.setLocation(X,Y);
this.setTitle(TITLE);
BorderLayout borderlayout=new BorderLayout();
this.setLayout(borderlayout);
JPanel panel_nor=CreatePanelForRegeist.CreateNorthPanel();
this.add(panel_nor,BorderLayout.NORTH);
JPanel panel_sou=CreatePanelForRegeist.CreateSouthPanel();
this.add(panel_sou,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false); //禁止改变窗口大小
this.setVisible(true);
}
public static void main(String[] args) {
new RegistWindow();
}
}
package com.zys.regeist;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.zys.database.RegistUser;
import com.zys.login.MyLineBorder;
public class CreatePanelForRegeist implements ActionListener{
/**
* 昵称框
*/
public static JTextField user_text=new JTextField();
/**
*密码框
*/
public static JPasswordField pwd_text=new JPasswordField();
/**
* 确认密码框
*/
public static JPasswordField repwd_text=new JPasswordField();
/**
*个性签名框
*/
public static JTextField introduce_text=new JTextField();
/**
*性别框
*/
public static JComboBox sex_combox=new JComboBox();
/**
*设置密保框
*/
public static JComboBox pwdprotect_combox=new JComboBox();
/**
* 密保答案框
*/
public static JTextField answer_text=new JTextField();
/**
* 按钮
*/
public static ImageIcon image=new ImageIcon("images/btn_regeist.png");
public static JButton btn=new JButton(image);
public static String getInfo(){
return answer_text.getText();
}
/**
*创建北部面板
*/
public static JPanel CreateNorthPanel() {
JPanel panel=new JPanel();
panel.setLayout(null);
panel.setPreferredSize(new Dimension(0,97));
ImageIcon image=new ImageIcon("images/regeist1.png");
JLabel label_nor=new JLabel(image);
label_nor.setBounds(0,0,520,100);
panel.add(label_nor);
return panel;
}
/**
*创建南部面板
*/
public static JPanel CreateSouthPanel(){
JPanel panel =new JPanel();
JLabel label_sou=new JLabel();
panel.setPreferredSize(new Dimension(0,505));
panel.setBackground(new Color(176,224,230));
label_sou.setBounds(0, 0, 400, 300);
panel.add(label_sou);
MyLineBorder myLineBorder = new MyLineBorder(new Color(192, 192, 192), 1 , true);
/**
* 添加昵称和输入框
*/
JLabel user =new JLabel("昵 称");
user.setBounds(90,70,100,25);
user.setFont(new Font("微软雅黑",0,14));
user_text.setBounds(170, 65, 160, 30);
user_text.setBorder(myLineBorder);
panel.add(user);
panel.add(user_text);
/**
* 密码及密码框
*/
JLabel pwd=new JLabel("密 码");
pwd.setBounds(90,110,100,25);
pwd.setFont(new Font("微软雅黑",0,14));
pwd_text.setBounds(170, 105, 160, 30);
pwd_text.setBorder(myLineBorder);
panel.add(pwd_text);
panel.add(pwd);
/**
* 确认密码及确认密码框
*/
JLabel repwd=new JLabel("确认密码");
repwd.setBounds(90,150,100,25);
repwd.setFont(new Font("微软雅黑",0,14));
repwd_text.setBounds(170, 145, 160, 30);
repwd_text.setBorder(myLineBorder);
panel.add(repwd_text);
panel.add(repwd);
/**
* 个人说明及文本框
*/
JLabel introduce=new JLabel("个性签名");
introduce.setBounds(90,190,100,25);;
introduce.setFont(new Font("微软雅黑",0,14));
introduce_text.setBounds(170, 185, 160, 30);
introduce_text.setBorder(myLineBorder);
panel.add(introduce_text);
panel.add(introduce);
/**
* 性别
*/
JLabel sex=new JLabel("性 别");
sex.setBounds(90,230,100,25);
sex.setFont(new Font("微软雅黑",0,14));
sex_combox.setBounds(170, 225, 160, 30);
sex_combox.addItem("男");
sex_combox.addItem("女");
sex_combox.setFont(new Font("微软雅黑",0,14));
panel.add(sex);
panel.add(sex_combox);
/**
* 设置密保
*/
JLabel pwdprotect=new JLabel("密保问题");
pwdprotect.setBounds(90,270,100,25);
pwdprotect.setFont(new Font("微软雅黑",0,14));
pwdprotect_combox.setBounds(170, 265, 160, 30);
pwdprotect_combox.addItem("我的名字是?");
pwdprotect_combox.addItem("我同桌的名字是?");
pwdprotect_combox.addItem("我生日是?");
pwdprotect_combox.setFont(new Font("微软雅黑",0,14));
//密保答案
JLabel pwdanswer=new JLabel("密保答案");
pwdanswer.setBounds(90,305,100,25);
pwdanswer.setFont(new Font("微软雅黑",0,14));
answer_text.setBounds(170, 305, 160, 30);
panel.add(answer_text);
panel.add(pwdanswer);
panel.add(pwdprotect);
panel.add(pwdprotect_combox);
/**
* 注册按钮
*/
btn.setBounds(120,400,image.getIconWidth()-120,image.getIconHeight()-15);
btn.addActionListener(new CreatePanelForRegeist());
panel.add(btn);
panel.setLayout(null);
return panel;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn){
/**
* 将用户信息注册保存到数据库
*/
RegistUser regist=new RegistUser();
try {
regist.Regist();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
3.导入MYSQL数据库的驱动,具体实现过程如下:右键工程名--->Build Path----->Configure Build Path----->Libraries------>Add External JARs ----->选择MySQL驱动在电脑中的位置,如果没有驱动包的话,可以点击该链接下载驱动包。最终结果如图:
4.编写数据库相关代码,需要实现的类有ConnectDatabase(链接数据库)类,GetInfoFromRegeistWnd(获取组件值)类,RegistUser(注册)类,具体代码如下:
package com.zys.database;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectDatabase {
public static final String USER="root";
public static final String PASSWORD="zengyongsheng";
public static final String URL="jdbc:mysql://localhost/qq?characterEncoding=utf-8"; //解决中文乱码
public static Connection ConnectionMySQL() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=null;
conn=DriverManager.getConnection(URL,USER,PASSWORD);
return conn;
}
}
注意:这里要注意代码段中的注释部分,在设置URL时可以设置字符集编码,解决中文乱码的问题,否则存储数剧会有如下结果:
package com.zys.database;
import com.zys.regeist.*;
public class GetInfoFromRegeistWnd {
/**
* 获取昵称
*/
public String getName(){
return CreatePanelForRegeist.user_text.getText();
}
/**
* 获取密码
*/
public String getPassword(){
return CreatePanelForRegeist.pwd_text.getText();
}
/**
* 获取确认密码
*/
public String getRePassword(){
return CreatePanelForRegeist.repwd_text.getText();
}
/**
* 获取个性签名框
*/
public String getIntroduction(){
return CreatePanelForRegeist.introduce_text.getText();
}
/**
* 获取性别
*/
public String getSex(){
return CreatePanelForRegeist.sex_combox.getSelectedItem()+"";
}
/**
* 获取密保问题
*/
public String getPwdProtect(){
return CreatePanelForRegeist.pwdprotect_combox.getSelectedItem()+"";
}
/**
* 获取密保答案
*/
public String getAnswer(){
return CreatePanelForRegeist.answer_text.getText();
}
}
设计这个类在功能方面并没有什么特殊的,主要是为了防止出现所有的代码堆积在一个类中的情况,这么做是为了结构清晰点。
package com.zys.database;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class RegistUser {
public String id;
public String name;
public String password;
public String repassword;
public String introduction;
public String sex;
public String pwdprotect;
public String answer;
public void update(String id,String name,String password,String repassword,String introduction,String sex,String pwdprotect,String answer) throws Exception{
String sql="INSERT INTO user(id,name,password,repassword,introduction,sex,pwdprotect,answer) VALUES(?,?,?,?,?,?,?,?)";
System.out.println(sql);
Connection conn=ConnectDatabase.ConnectionMySQL();
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, password);
pstmt.setString(3, name);
pstmt.setString(4, repassword);
pstmt.setString(5, introduction);
pstmt.setString(6, sex);
pstmt.setString(7, pwdprotect);
pstmt.setString(8, answer);
System.out.println(sql);
pstmt.executeUpdate(sql);
conn.close();
}
public void Regist() throws Exception {
GetInfoFromRegeistWnd regist=new GetInfoFromRegeistWnd();
id="sdf";
name=regist.getName();
password=regist.getPassword();
repassword=regist.getRePassword();
introduction=regist.getIntroduction();
sex=regist.getSex();
pwdprotect=regist.getPwdProtect();
answer=regist.getAnswer();
System.out.println(name);
update(id,name,password,repassword,introduction,sex,pwdprotect,answer);
}
}
这个类实现了将注册页面中的数据保存到数据库中,这里用到了一条SQL语句:INSERT INTO user(a) VALUES(?)。这里没有使用拼接字符串的方法来实现插入数据,而是通过占位符的方式来实现功能。
注意:如果使用占位符的方式会出现这种报错信息,如图所示:
这是因为如下这条语句的原因,这里应该使用不带参数的executeUpdate方法,将其改为“pstmt.executeUpdate()”即可。因为带参数的方法不支持使用“?”来进行参数传递。
pstmt.executeUpdate(sql);
4.做到这里已将大概的内容做出来了,运行测试:
说明:这次主要是实现保存数据到数据库的功能,至于要完善功能,还要几个方向需要实现:
1.自动生成ID,也就是将来用来登录的账号
2.制作一个注册成功和失败的页面,成功的页面需要提供用户账号
3.需要实现验证密码和确认密码的一致性
4.设置相关内容的长度
这次先做到这里,下次将完善上述功能,并实现登录功能。