利用JDBC與MySQL實現一個用戶注冊功能
具體方法如下:
package cn.edu.hbue.qyf;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class HomeWork6 extends JFrame implements ActionListener{
private JTextField custNameTextField;
private JTextField codeNameTextField;
private JTextField recodeNameTextField;
private JLabel custNameLabel;
private JLabel codeLabel;
private JLabel recodeLabel;
private JButton confirmButton;
private JButton cancelButton;
public HomeWork6(){
setSize(250, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = this.getContentPane();
cp.setLayout(new FlowLayout());
custNameLabel = new JLabel(" 用 戶 名 ");//創建第一個標簽和文本框
cp.add(custNameLabel);
custNameTextField = new JTextField(10);
cp.add(custNameTextField);
codeLabel = new JLabel(" 輸 入 密 碼 ");//創建第二個標簽和文本框
cp.add(codeLabel);
codeNameTextField = new JTextField(10);
cp.add(codeNameTextField);
recodeLabel = new JLabel("再 次 輸 入 密 碼");//創建第三個標簽和文本框
cp.add(recodeLabel);
recodeNameTextField= new JTextField(10);
cp.add(recodeNameTextField);
//創建按鈕
confirmButton = new JButton("注冊");
confirmButton.addActionListener(this);
cp.add(confirmButton);
cancelButton = new JButton("取消");
cp.add(cancelButton);
cancelButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("注冊")){
// 1. 從各個控件收集用戶輸入的數據
String cusName = custNameTextField.getText();//字符串
String code = codeNameTextField.getText();//字符串
String recode = recodeNameTextField.getText();//字符串
//2. 檢測字段是否為空
if(cusName.equals("")){
JOptionPane.showMessageDialog(getContentPane(), "請填寫用戶名!",
"信息提示框", JOptionPane.INFORMATION_MESSAGE);
return ;
}
else if(code.equals("")){
JOptionPane.showMessageDialog(getContentPane(), "密碼不能為空!",
"信息提示框", JOptionPane.INFORMATION_MESSAGE);
return ;
}
else if(recode.equals("")){
JOptionPane.showMessageDialog(getContentPane(), "必須重復輸入密碼!",
"信息提示框", JOptionPane.INFORMATION_MESSAGE);
return ;
}
//3.合法性檢查
int length = code.length();
if(!(length >= 6 && length <= 10)){
JOptionPane.showMessageDialog(getContentPane(), "密碼格式錯誤!",//密碼長度限定
"信息提示框", JOptionPane.INFORMATION_MESSAGE);
return ;
}
if (!recode.equals(code))
JOptionPane.showInputDialog(confirmButton, "兩次密碼不一致,請重新輸入");
//4. 連接數據庫,構造insert語句,向數據庫中插入記錄,關閉與數據庫的連接
Connection conn = JDBCutil.getConn();
String sql = "insert into information(cusName,code,recode) values(?,?,?)";
PreparedStatement preStmt=null;
try {
preStmt= conn.prepareStatement(sql);
preStmt.setString(1, cusName);
preStmt.setString(2, code);
preStmt.setString(3, recode);
//顯示信息提示框
int i = -1;
i = preStmt.executeUpdate();//如果小于1表示插入失敗
if(i >= 1)//顯示數據插入成功,并清除字段
JOptionPane.showMessageDialog(confirmButton, "注冊成功!");
else
JOptionPane.showMessageDialog(confirmButton, "注冊失敗!(賬號或密碼已存在)
");
preStmt.executeUpdate();
System.out.println("成功插入數據"+ preStmt.getUpdateCount() + "條");
} catch (SQLException e1) {
e1.printStackTrace();
}finally{
JDBCutil.closeConn(preStmt,conn);
}
//5. 如插入成功,提示用戶新增成功,并清空字段。插入不成功,提示新增失敗
}else{
HomeWork6.this.dispose();
}
}
public static void main(String[] args){
HomeWork6 insert = new HomeWork6();
insert.setVisible(true);
}
}