www.pudn.com > Easy_Buy电子商务系统.rar > RegisterBean.java


//package eshop;
import java.util.*;
import java.sql.*;

public class RegisterBean {
  Connection conn;
  boolean registerResult;

  public RegisterBean() {
    registerResult=false;
  }

  public void setConn(Connection conn){
    this.conn=conn;
  }

  public boolean register(String username,String password,String question,String answer){
    try {
      Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
      String sql1="SELECT * FROM Customers WHERE Username='"+username+"'";
      ResultSet rsLogin=stmt.executeQuery(sql1);
      if(rsLogin.next()){
          this.registerResult=false;
      }else{
          String sql2="INSERT INTO Customers(Username,Password,Question,Answer) VALUES('"+username+"','"+password+"','"+question+"','"+answer+"')";
          stmt.executeUpdate(sql2);
          this.registerResult=true;
      }
      rsLogin.close();
      stmt.close();
    }catch(SQLException e){
        e.printStackTrace();
    }
    return this.registerResult;
  }
}