我是java / mysql的新手,我想添加电子邮件验证,但我该怎么办呢?我尝试使用在线的大量代码,但它没有用。请帮忙!谢谢你。
这是我班级的1岁以下
package customerApp.ui;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import customerApp.entity.Customer;
import java.awt.event.ActionEvent;
import java.sql.DriverManager;
import customerApp.dataAccess.CustomerDA;
public class NextPanel extends MasterPanel {
private JTextField txtfName;
private JTextField txtlName;
private JTextField txtEmail;
private JTextField txtDateOfBirth;
private JTextField txtUserId;
private JPasswordField passwordField;
private JTextField txtContactNumber;
private JTextField txtAddress;
private JPasswordField passwordField_1;
/**
* Create the panel.
*/
public NextPanel(JFrame mf) {
super(mf);
JLabel lblFirstName = new JLabel("");
lblFirstName.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (26).png")));
lblFirstName.setBounds(55, 155, 148, 38);
add(lblFirstName);
JLabel lblLastName = new JLabel("");
lblLastName.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (27).png")));
lblLastName.setBounds(55, 204, 144, 38);
add(lblLastName);
JLabel label = new JLabel("");
label.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (28).png")));
label.setBounds(55, 253, 114, 38);
add(label);
JLabel label_1 = new JLabel("");
label_1.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (29).png")));
label_1.setBounds(55, 302, 94, 38);
add(label_1);
JLabel label_2 = new JLabel("");
label_2.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (30).png")));
label_2.setBounds(53, 351, 165, 38);
add(label_2);
JLabel label_3 = new JLabel("");
label_3.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (35).png")));
label_3.setBounds(53, 400, 109, 39);
add(label_3);
JLabel label_4 = new JLabel("");
label_4.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (32).png")));
label_4.setBounds(55, 450, 136, 38);
add(label_4);
JLabel label_5 = new JLabel("");
label_5.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (33).png")));
label_5.setBounds(55, 547, 205, 38);
add(label_5);
JLabel label_6 = new JLabel("");
label_6.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (34).png")));
label_6.setBounds(55, 596, 121, 38);
add(label_6);
txtfName = new JTextField();
txtfName.setBounds(297, 163, 200, 30);
add(txtfName);
txtfName.setColumns(10);
txtlName = new JTextField();
txtlName.setBounds(297, 212, 200, 30);
add(txtlName);
txtlName.setColumns(10);
final JRadioButton Femalebtn = new JRadioButton("Female");
Femalebtn.setBounds(307, 253, 76, 23);
Femalebtn.setSelected(true);
add(Femalebtn);
final JRadioButton Malebtn = new JRadioButton("Male");
Malebtn.setBounds(417, 253, 64, 23);
Malebtn.setSelected(true);
add(Malebtn);
// Group the radio buttons
ButtonGroup group = new ButtonGroup();
group.add(Femalebtn);
group.add(Malebtn);
txtEmail = new JTextField();
txtEmail.setBounds(297, 302, 200, 30);
add(txtEmail);
txtEmail.setColumns(10);
txtDateOfBirth = new JTextField();
txtDateOfBirth.setBounds(297, 351, 200, 30);
add(txtDateOfBirth);
txtDateOfBirth.setColumns(10);
txtUserId = new JTextField();
txtUserId.setBounds(297, 400, 200, 30);
add(txtUserId);
txtUserId.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(297, 450, 200, 30);
add(passwordField);
txtContactNumber = new JTextField();
txtContactNumber.setBounds(297, 555, 200, 30);
add(txtContactNumber);
txtContactNumber.setColumns(10);
txtAddress = new JTextField();
txtAddress.setBounds(297, 596, 200, 30);
add(txtAddress);
txtAddress.setColumns(10);
JButton btnOK = new JButton("");
btnOK.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (22).png")));
btnOK.setBounds(472, 637, 70, 39);
add(btnOK);
btnOK.addActionListener(new ActionListener() {
private Customer customer;
public void actionPerformed(ActionEvent arg0) {
actionPerformedOk();
JPanel contentPane = new UpdatePanel(myFrame);
myFrame.setContentPane(contentPane);
myFrame.setVisible(true);
}
private void actionPerformedOk() {
if (validateInput()) {
String fName = txtfName.getText();
String lName = txtlName.getText();
String email = txtEmail.getText();
String dateOfBirth = txtDateOfBirth.getText();
String userId = txtUserId.getText();
String contactNumber = txtContactNumber.getText();
String address = txtAddress.getText();
String gender = "";
if (Malebtn.isSelected())
gender = "Male";
else
gender = "Female";
// create a membership object based on the input values
customer = new Customer(fName,lName,gender,email,dateOfBirth,userId,contactNumber,address);
actionPerformedCreate();
}
// create an object of expenses based on the input values
customer = new Customer(txtfName.getText(),txtlName.getText(),"",txtEmail.getText(),txtDateOfBirth.getText(),
txtUserId.getText(),passwordField.getText(),txtContactNumber.getText(),txtAddress.getText());
}
public void actionPerformedCreate(){
if (CustomerDA.createCustomer(customer)) {
JOptionPane.showMessageDialog(myFrame,
"Record created successfully", "Alert",
JOptionPane.INFORMATION_MESSAGE);
// reset text field for next record.
txtfName.setText("");
txtlName.setText("");
txtEmail.setText("");
txtDateOfBirth.setText("");
txtUserId.setText("");
passwordField.setText("");
txtContactNumber.setText("");
txtAddress.setText("");
} else
JOptionPane.showMessageDialog(myFrame,
"Database Error. Record not created.", "Alert",
JOptionPane.ERROR_MESSAGE);
}
private boolean validateInput() {
boolean result = true;
Boolean exist = CustomerDA.checkAvailiability(txtUserId.getText());
String msg = "";
int msgType = JOptionPane.ERROR_MESSAGE;
// retrieve the user input from the text box/area provided
String fName = txtfName.getText();
String lName = txtlName.getText();
String email = txtEmail.getText();
String dateOfBirth = txtDateOfBirth.getText();
String userId = txtUserId.getText();
String contactNumber = txtContactNumber.getText();
String address = txtAddress.getText();
if (fName.length() == 0)
msg += "Please enter first name.\n";
if (lName.length() == 0)
msg += "Please enter last name.\n";
if (email.length() == 0)
msg += "Please enter email.\n";
if (dateOfBirth.length() != 10)
msg += "Please enter date in DD-MM-YYYY format.\n";
if(userId.equals(userId))
msg += "User name is in use. Try again\n";
if (contactNumber.length() == 0)
msg += "Please enter contact number.\n";
if (address.length() == 0)
msg += "Please enter address.\n";
if (msg.length() == 0)
result = true;
else
JOptionPane.showMessageDialog(myFrame, msg, "Alert", msgType);
return result;
}});
JLabel label_7 = new JLabel("");
label_7.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (36).png")));
label_7.setBounds(55, 499, 224, 34);
add(label_7);
passwordField_1 = new JPasswordField();
passwordField_1.setBounds(297, 503, 200, 30);
add(passwordField_1);
JButton checkavailiability = new JButton("");
checkavailiability.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Boolean exist = CustomerDA.checkAvailiability(txtUserId.getText());
if(exist)
JOptionPane.showMessageDialog(myFrame,
"User name in use!", "Alert",
JOptionPane.ERROR_MESSAGE);
else
JOptionPane.showMessageDialog(myFrame,
"Username not in use", "Alert",
JOptionPane.INFORMATION_MESSAGE);
}
});
checkavailiability.setIcon(new ImageIcon(NextPanel.class.getResource("/customerApp/images/button (39).png")));
checkavailiability.setBounds(507, 400, 155, 30);
add(checkavailiability);
}
}
这是另一部分
package customerApp.dataAccess;
import java.awt.Component;
import java.sql.PreparedStatement;
import static java.lang.System.*;
import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import customerApp.entity.Customer;
public class CustomerDA {
protected static boolean txtUserId;
private static Component myFrame;
private static Customer customer;
/**
* Purpose: This method gets the max id from the customer table
* and returns it to the calling problem
* Input: Nil
* Return: int
*/
private static int getNextCustomerId() {
// declare local variables
int id = 0;
ResultSet rs = null;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;
// step 1 - connect to database
db.getConnection();
// step 2 - declare the SQL statement
dbQuery = "SELECT MAX(id) FROM customer";
pstmt = db.getPreparedStatement(dbQuery);
// step 3 - execute query
try {
rs = pstmt.executeQuery();
if (rs.next()) { // first record found
id = rs.getInt(1) + 1;
}
} catch (Exception e) {
e.printStackTrace();
}
// step 4 - close connection
db.terminate();
return id;
}
/**
* Purpose: This method converts a ResultSet object into an
* Expense object
* Input: ResultSet object
* Return: Expense object
*/
private static Customer convertToCustomer(ResultSet rs) throws SQLException {
Customer customer;
int id = rs.getInt("id");
String fName = rs.getString("fName");
String lName = rs.getString("lName");
String gender = rs.getString("gender");
String email = rs.getString("email");
String dateOfBirth = rs.getString("dateOfBirth");
String userId = rs.getString("userId");
String password = rs.getString("password");
String contactNumber = rs.getString("contactNumber");
String address = rs.getString("address");
customer = new Customer(id, fName, lName, gender,
email, dateOfBirth, userId, password,
contactNumber, address);
return customer;
}
/**
* Purpose: This method takes in an Expense object and
* store it into the database.
* Input: Expense object
* Return: boolean
*/
public static boolean createCustomer(Customer customer) {
// declare local variables
boolean success = false;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;
// step 1 - establish connection to database
db.getConnection();
// step 2 - declare the SQL statement
dbQuery = "INSERT INTO customer(id, fName, lName, gender, email, dateOfBirth, UserId, password, contactNumber, address) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
pstmt = db.getPreparedStatement(dbQuery);
// step 3 - to insert record using executeUpdate method
try {
pstmt.setInt(1, getNextCustomerId());
pstmt.setString(2, customer.getfName());
pstmt.setString(3, customer.getlName());
pstmt.setString(4, customer.getGender());
pstmt.setString(5, customer.getEmail());
pstmt.setString(6, customer.getDateOfBirth());
pstmt.setString(7, customer.getUserId());
pstmt.setString(8, customer.getPassword());
pstmt.setString(9, customer.getContactNumber());
pstmt.setString(10, customer.getAddress());
if (pstmt.executeUpdate() == 1)
success = true;
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
// step 4 - close connection
db.terminate();
return success;
}
/**
* Purpose: This method retrieves all the customer records in the
* database and returns them in an ArrayList.
* Input: Nil
* Return: ArrayList of Expense objects
*/
public static ArrayList<Customer> retrieveAllCustomer() {
// declare local variables
ArrayList<Customer> list = new ArrayList<Customer>();
ResultSet rs = null;
DBController db = new DBController();
String dbQuery;
// Step 1 - connect to database
db.getConnection();
// step 2 - declare the SQL statement
dbQuery = "SELECT * FROM customer";
// step 3 - using DBControlle readRequest method
rs = db.readRequest(dbQuery);
try {
while (rs.next()) {
Customer customer = convertToCustomer(rs);
list.add(customer);
}
} catch (Exception e) {
e.printStackTrace();
}
// step 4 - close connection
db.terminate();
return list;
}
/**
* Purpose: This method retrieves an customer record by id
* and returns it to the calling program.
* Input: int
* Return: Expense object
*/
public static Customer retrieveCustomerById(int id) {
// declare local variables
Customer customer = null;
ResultSet rs = null;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;
// step 1 - connect to database
db.getConnection();
// step 2 - declare the SQL statement
dbQuery = "SELECT * FROM customer WHERE id = ?";
pstmt = db.getPreparedStatement(dbQuery);
// step 3 - execute query
try {
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) { // first record found
customer = convertToCustomer(rs);
}
} catch (Exception e) {
e.printStackTrace();
}
// step 4 - close connection
db.terminate();
return customer;
}
public static boolean updateCustomer(Customer customer) {
//declare local variables
boolean success = false;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;
//step 1 - establish connection to database
db.getConnection();
//step 2 - declare the SQL statement
dbQuery = "UPDATE customer SET fName = ?, lName = ?, gender = ?, email = ?, dateOfBirth = ?, userId = ?, password = ? ,contactNumber = ?, address WHERE id = ?";
pstmt = db.getPreparedStatement(dbQuery);
//step 3 - to update record using executeUpdate method
try {
pstmt.setInt(1, getNextCustomerId());
pstmt.setString(2, customer.getfName());
pstmt.setString(3, customer.getlName());
pstmt.setString(4, customer.getGender());
pstmt.setString(5, customer.getEmail());
pstmt.setString(6, customer.getDateOfBirth());
pstmt.setString(7, customer.getUserId());
pstmt.setString(8, customer.getPassword());
pstmt.setString(9, customer.getContactNumber());
pstmt.setString(10, customer.getAddress());
if (pstmt.executeUpdate() == 1)
success = true;
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(success);
//step 4 - close connection
db.terminate();
return success;
}
public static boolean deleteCustomer(int id) {
//declare local variables
boolean success = false;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;
//step 1 - establish connection to database
db.getConnection();
//step 2 - declare the SQL statement
dbQuery = "DELETE FROM customer WHERE id = ?";
pstmt = db.getPreparedStatement(dbQuery);
//step 3 - to delete record using executeUpdate method
try {
pstmt.setInt(1, id);
if (pstmt.executeUpdate() == 1)
success = true;
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
//step 4 - close connection
db.terminate();
return success;
}
public static void main(String[] args) {
// create Customer object using default constructor
Customer e1 = new Customer(0);
// e1 object uses setter methods to set values
e1.setfName("");
e1.setlName("");
e1.setGender("");
e1.setEmail("");
e1.setDateOfBirth("");
e1.setUserId("");
e1.setPassword("");
e1.setContactNumber("");
e1.setAddress("");
// create record in database
boolean result = CustomerDA.createCustomer(e1);
System.out.println(result); // is the record successfully created?
// retrieve all customer record from database
ArrayList<Customer> eList = CustomerDA.retrieveAllCustomer();
for (int i=0; i<eList.size(); i++){
Customer e = eList.get(i);
// print out the record data using getter methods
System.out.print(e.getfName() + "\t");
System.out.print(e.getlName() + "\t");
System.out.print(e.getGender() + "\t");
System.out.print(e.getEmail() + "\t");
System.out.print(e.getDateOfBirth() + "\t");
System.out.print(e.getUserId() + "\t");
System.out.print(e.getPassword() + "\t");
System.out.print(e.getContactNumber() + "\t");
System.out.print(e.getAddress() + "\n");
}
}
public static Boolean checkAvailiability(String UserId) {
ResultSet rs = null;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;
// step 1 - connect to database
db.getConnection();
// step 2 - declare the SQL statement
dbQuery = "SELECT * FROM Customer WHERE UserId = ?";
pstmt = db.getPreparedStatement(dbQuery);
// step 3 - execute query
try {
pstmt.setString(1, UserId);
rs = pstmt.executeQuery();
if (rs.next()) { // first record found
return true;
}
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
}