import java.awt.Button;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 用于启动注册界面。
*
* @version 01.00 2012-8-28 guzhaocheng
* @author gu zhaocheng
*/
public class Register {
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable(){
public void run()
{
new RegisterFrame();
}
});
}
}
/**
* 用于初始化注册界面窗体。
*
* @version 01.00 2012-8-28 guzhaocheng
* @author gu zhaocheng
*/
class RegisterFrame extends JFrame
{
public RegisterFrame()
{
setTitle(TITLE);
setBounds(300,200,DEFAULT_WIDTH,DEFAULT_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
setLayout(null);
panelSignIn = new JPanel();
panelButton = new JPanel();
textSignIn = new JTextField(10);
buttonSignIn = new Button("Register");
panelID = new JPanel();
textID = new JTextField(10);
panelPwd = new JPanel();
textPwd = new JTextField(10);
panelMobile = new JPanel();
textMobile = new JTextField(10);
buttonSignIn.addActionListener(new ButtonListener());
JLabel lbBg;
lbBg = new JLabel(new ImageIcon("F:\\bgReg.jpg")); /* 加入背景图片 */
lbBg.setBounds(0, 0, 500, 400);
panelSignIn.add(textSignIn);
panelSignIn.setBounds(176, 150,120,32);
panelButton.add(buttonSignIn);
panelButton.setBounds(314,150,60,32);
panelID.add(textID);
panelID.setBounds(176, 190,120,32);
panelMobile.add(textMobile);
panelMobile.setBounds(176, 230, 120, 32);
panelPwd.add(textPwd);
panelPwd.setBounds(176, 270,120,32);
add(panelSignIn);
add(panelButton);
add(panelID);
add(panelMobile);
add(panelPwd);
add(lbBg);
}
/**
* 建立一个数据库连接
* @exception 未找到类和数据库SQL异常
* @return Connection 返回一个数据库连接类型
*/
public static Connection con() throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/user";
String usr = "root";
String pwd = "root";
return DriverManager.getConnection(url, usr, pwd);
}
/**
* 用于写入数据库数据使用登录数据
* @return boolean 数据库写入正确返回true,否则返回false
*/
public boolean reg()
{
Connection con;
try {
con = con();
final String sql = "insert into tb_user ( id , name , moblie , password) values (?,?,?,?)";
PreparedStatement pstmt;
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, name);
pstmt.setString(3, mobile);
pstmt.setString(4, passWord);
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return false;
}
/**
* 用于定义一个按钮监听器(实现注册按钮验证以及写入数据库)。
*
* @version 01.00 2012-8-28 guzhaocheng
* @author gu zhaocheng
*/
class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonSignIn)
{
matcherID = patternID.matcher(textID.getText());
matcherMobile = patternMobile.matcher(textMobile.getText());
if(textSignIn.getText().trim().equals("") || textSignIn == null)
JOptionPane.showMessageDialog(null, "We Need Your Name","What's Your Name",JOptionPane.ERROR_MESSAGE);
else
if(!matcherID.find())
JOptionPane.showMessageDialog(null, "We Need Your ID","What's Your ID",JOptionPane.ERROR_MESSAGE);
else
if(!matcherMobile.find())
JOptionPane.showMessageDialog(null, "We Need Your Moblie","What's Your Mobile",JOptionPane.ERROR_MESSAGE);
else
{
id = textID.getText();
name = textSignIn.getText();
mobile = textMobile.getText();
passWord = textPwd.getText();
if(reg())
{
JOptionPane.showMessageDialog(null, "Success","Congraduation!",JOptionPane.OK_OPTION);
RegisterFrame.this.setVisible(false);
}
else
JOptionPane.showMessageDialog(null, "Failed","Register Failed",JOptionPane.OK_OPTION);
}
}
}
}
private static final long serialVersionUID = -8649115223425727109L;
String regexID = "20[01][012]EN\\d{3}";
Pattern patternID = Pattern.compile(regexID);
Matcher matcherID;
String regexMobile = "(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}";
Pattern patternMobile = Pattern.compile(regexMobile);
Matcher matcherMobile;
JPanel panelSignIn;
JPanel panelButton;
JPanel panelID;
JPanel panelPwd;
JPanel panelMobile;
JTextField textSignIn;
JTextField textID;
JTextField textPwd;
JTextField textMobile;
JLabel jLabel;
Button buttonSignIn;
String name;
String id;
String mobile;
String passWord;
static final String TITLE = "用户登录";
static final int DEFAULT_WIDTH = 504;
static final int DEFAULT_HEIGHT = 427;
}
java聊天室 提交预版 注册框
最新推荐文章于 2022-05-21 16:07:00 发布