package frame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class SignUpFrame extends JFrame implements ActionListener {
Box boxHOne,boxHTwo,boxVOne,boxVTwo,boxHThree;
JTextField userName,passWord,rePassWord;
JLabel name,psw,rePsw,tip;
JButton signUp;
public SignUpFrame(){
setTitle("用户注册");
setLayout(new FlowLayout());
boxHOne=Box.createHorizontalBox();
boxVOne=Box.createVerticalBox();
boxVTwo=Box.createVerticalBox();
boxHThree=Box.createHorizontalBox();
name=new JLabel("用户名:");
psw=new JLabel("密码");
rePsw=new JLabel("确认密码");
boxVOne.add(name);
boxVOne.add(psw);
boxVOne.add(rePsw);
userName=new JTextField(15);
passWord=new JTextField(15);
rePassWord=new JTextField(15);
boxVTwo.add(userName);
boxVTwo.add(passWord);
boxVTwo.add(rePassWord);
boxHOne.add(boxVOne);
boxHOne.add(Box.createHorizontalStrut(10));
boxHOne.add(boxVTwo);
tip=new JLabel("注意前后密码一致,否则将注册失败");
tip.setFont(new Font("楷体",Font.PLAIN,16));
tip.setForeground(Color.RED);
signUp=new JButton("注册");
signUp.addActionListener(this);
boxHTwo=Box.createHorizontalBox();
boxHTwo.add(signUp);
boxHThree.add(tip);
add(boxHOne);
add(boxHTwo);
add(boxHThree);
setBounds(1100,500,320,250);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==signUp){
if(passWord.getText()==rePassWord.getText()){
dataCreate();
new SignUpFrame();
}
else if(passWord.getText()!=rePassWord.getText()){
tip.setText("前后两次密码输入不一致,请重新输入");
}
}
}
public void dataCreate(){
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch (Exception e){
e.printStackTrace();
}
try {
String uri = "jdbc:mysql://localhost:3306/pushbox?useUnicode=true&characterEncoding=utf8&&useSSL=false";
String user = "root";
String password = "123456";
con = DriverManager.getConnection(uri, user, password);
sql = con.createStatement();
sql.executeUpdate("insert into user value "+"('"+userName.getText()+"','"+passWord.getText()+"')");
con.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}