利用JDBC與MySQL實(shí)現(xiàn)一個(gè)用戶注冊(cè)功能
具體方法如下:
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(" 用 戶 名 ");//創(chuàng)建第一個(gè)標(biāo)簽和文本框
cp.add(custNameLabel);
custNameTextField = new JTextField(10);
cp.add(custNameTextField);
codeLabel = new JLabel(" 輸 入 密 碼 ");//創(chuàng)建第二個(gè)標(biāo)簽和文本框
cp.add(codeLabel);
codeNameTextField = new JTextField(10);
cp.add(codeNameTextField);
recodeLabel = new JLabel("再 次 輸 入 密 碼");//創(chuàng)建第三個(gè)標(biāo)簽和文本框
cp.add(recodeLabel);
recodeNameTextField= new JTextField(10);
cp.add(recodeNameTextField);
//創(chuàng)建按鈕
confirmButton = new JButton("注冊(cè)");
confirmButton.addActionListener(this);
cp.add(confirmButton);
cancelButton = new JButton("取消");
cp.add(cancelButton);
cancelButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("注冊(cè)")){
// 1. 從各個(gè)控件收集用戶輸入的數(shù)據(jù)
String cusName = custNameTextField.getText();//字符串
String code = codeNameTextField.getText();//字符串
String recode = recodeNameTextField.getText();//字符串
//2. 檢測(cè)字段是否為空
if(cusName.equals("")){
JOptionPane.showMessageDialog(getContentPane(), "請(qǐng)?zhí)顚懹脩裘?,
"信息提示框", JOptionPane.INFORMATION_MESSAGE);
return ;
}
else if(code.equals("")){
JOptionPane.showMessageDialog(getContentPane(), "密碼不能為空!",
"信息提示框", JOptionPane.INFORMATION_MESSAGE);
return ;
}
else if(recode.equals("")){
JOptionPane.showMessageDialog(getContentPane(), "必須重復(fù)輸入密碼!",
"信息提示框", JOptionPane.INFORMATION_MESSAGE);
return ;
}
//3.合法性檢查
int length = code.length();
if(!(length >= 6 && length <= 10)){
JOptionPane.showMessageDialog(getContentPane(), "密碼格式錯(cuò)誤!",//密碼長(zhǎng)度限定
"信息提示框", JOptionPane.INFORMATION_MESSAGE);
return ;
}
if (!recode.equals(code))
JOptionPane.showInputDialog(confirmButton, "兩次密碼不一致,請(qǐng)重新輸入");
//4. 連接數(shù)據(jù)庫,構(gòu)造insert語句,向數(shù)據(jù)庫中插入記錄,關(guān)閉與數(shù)據(jù)庫的連接
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)//顯示數(shù)據(jù)插入成功,并清除字段
JOptionPane.showMessageDialog(confirmButton, "注冊(cè)成功!");
else
JOptionPane.showMessageDialog(confirmButton, "注冊(cè)失敗?。ㄙ~號(hào)或密碼已存在)
");
preStmt.executeUpdate();
System.out.println("成功插入數(shù)據(jù)"+ 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);
}
}