2017-08-20 10 views
-2
public boolean createNewAccount(CustomerDTO customerDTO, AccountDTO accountDTO, Connection connection) throws NamingException, SQLException { 
     CustomerDAO customerDAO=dAOFactory.createCustomerDAO(); 
     AcountDAO acountDAO=dAOFactory.createAcountDAO(); 

     connection.setAutoCommit(false); 
     try{ 

     boolean createNewAccount = customerDAO.createNewAccount(customerDTO); 

     if (createNewAccount) { 

      boolean createNewaccount = acountDAO.createNewaccount(accountDTO); 

       if (createNewaccount) { 

        connection.commit(); 
        return true; 


       }else{ 
        connection.rollback(); 
        return false; 
       } 

     }else{ 
      connection.rollback(); 
      return false; 
     } 
     }finally{ 
      connection.setAutoCommit(true); 
      return true; 
     } 
} 
+0

ようこそ。 [ask]を読むのに少し時間がかかる。あなたが直面している**正確**問題は何ですか? – OldProgrammer

答えて

0

create database abcbank; abcbankを使用します。

、NOT NULLテーブルの顧客( NIC VARCHAR(100)を作成し、 firstnmae VARCHAR(100)、 姓のVARCHAR(100)、 アドレスVARCHAR(100)、 接触INT(10)、 CONSTRAINT PRIMARY KEY( nic) );

テーブルアカウント( accNo VARCHAR(100)NULL NOT、 NIC VARCHAR(100)NOT NULL、 バランスフロート(10)、 CONSTRAINT PRIMARY KEY(accNo、NIC)、 CONSTRAINT FOREIGN KEY(NIC)を作成参照顧客(ニック) 更新カスケードON DELETE CASCADE );

テーブルトランザクションの独立を作成する( transNo VARCHAR(100)NULL NOT、 accNo VARCHAR(100)NOT NULL、 NIC VARCHAR(100)NOT NULL、 量フロート(10)NOT NULL、 transactiontype VARCHAR(100) 、 日付、 時刻の時間、 CONSTRAINT PRIMARY KEY(transNo、accNo、NIC)、 CONSTRAINT FOREIGN KEY(accNo)REFERENCESアカウント(accNo) UPDATE CASCADE ON ON CASCADEを削除 CONSTRAINT FOREIGN KEY(NIC)を参照アカウント(ニック) 更新カスケードON DELETE CASCADE );

 <Resource auth="Container" 
      driverClassName="com.mysql.jdbc.Driver" 
      maxActive="50" 
      maxAge="10000" 
      maxIdle="10" 
      maxWait="100000" 
      name="jdbc/thogakade" 
      password="deshan" 
      testOnBorrow="true" 
      type="javax.sql.DataSource" 
      url="jdbc:mysql://localhost:3306/thogakade?autoReconnect=true" 
      username="root" validationQuery="SELECT 1"/> 

resouseのabstrctクラス

保護された抽象データソースcreateDataSourceは、()は、NamingExceptionをスローします。

public DataSource getDataSource()throws NamingException{ 
    DataSource dataSource=createDataSource(); 
    return dataSource; 
} 

impl  
@Override 
protected DataSource createDataSource() throws NamingException { 
    InitialContext context=new InitialContext(); 
    DataSource dataSource=(DataSource) context.lookup("java:comp/env/jdbc/thogakade"); 
    return dataSource; 
} 

dao impl 

public boolean createNewAccount(CustomerDTO dTO) throws NamingException, SQLException { 
    String sql="INSERT INTO CUSTOMER VALUES(?,?,?,?,?)"; 
    PreparedStatement stm=dataSource.getConnection().prepareStatement(sql); 
    stm.setString(1, dTO.getNic()); 
    stm.setString(2, dTO.getFirstName()); 
    stm.setString(3, dTO.getLastName()); 
    stm.setString(4, dTO.getAddress()); 
    stm.setInt(5, dTO.getTpno()); 
    int res=stm.executeUpdate(); 
    return (res>0); 
} 

@Override 
public CustomerDTO serchCustomerByNIC(String nic) throws NamingException, SQLException { 
    String sql="SELECT * FROM CUSTOMER WHERE nic='"+nic+"'"; 
    CustomerDTO customerDTO=null; 
    Statement s=dataSource.getConnection().createStatement(); 
    ResultSet rst=s.executeQuery(sql); 
    if (rst.next()) { 
     customerDTO=new CustomerDTO(rst.getString(1),rst.getString(2),rst.getString(3),rst.getString(4),rst.getInt(5)); 
    } 
    return customerDTO; 
} 

public ArrayList<TransationDTO> getAllTransation() throws NamingException, SQLException { 
    String sql = "SELECT * from transation"; 
    Statement statement = dataSource.getConnection().prepareStatement(sql); 
    ResultSet rst = statement.executeQuery(sql); 
    ArrayList<TransationDTO> lsit = new ArrayList<>(); 
    while (rst.next()) { 
     if (lsit == null) { 
      lsit = new ArrayList<>(); 
     }else{ 
      TransationDTO dTO=new TransationDTO(rst.getString(1),rst.getString(2),rst.getString(3),rst.getFloat(4),rst.getString(5),rst.getString(6),rst.getString(6)); 
      lsit.add(dTO); 
     } 

    } 
    return lsit; 
} 
    @Override 
public boolean updateAccount(AccountDTO accountDTO) throws NamingException, SQLException { 
    String sql="UPDATE account SET balance=?,nic=? WHERE accNo=?"; 
    PreparedStatement stm = dataSource.getConnection().prepareStatement(sql); 
    stm.setString(1, accountDTO.getAccountNo()); 
    stm.setString(2, accountDTO.getNic()); 
    stm.setFloat(3, accountDTO.getBalance()); 
    int res = stm.executeUpdate(); 
    return (res > 0); 
} 

daofacimpl 
private Resource resource; 

public DAOFactoryImpl(Resource resource) { 
    this.resource = resource; 
} 

@Override 
public CustomerDAO createCustomerDAO() throws NamingException, SQLException { 
    DataSource dataSource=resource.getDataSource(); 
    CustomerDAO customerDAO=new CustomerDaoImpl(dataSource); 
    return customerDAO; 
} 

サービスleyer //

サービスIMPL
プライベートDAOFactoryのdAOFactory。

public CustomerServiveImpl(DAOFactory dAOFactory) { 
    this.dAOFactory = dAOFactory; 
} 



@Override 
public CustomerDTO serchCustomerByNIC(String nic) throws NamingException, SQLException { 
    CustomerDAO customerDAO=dAOFactory.createCustomerDAO(); 
    return customerDAO.serchCustomerByNIC(nic); 
} 

@Override 
public boolean createNewAccount(CustomerDTO customerDTO, AccountDTO accountDTO, Connection connection) throws NamingException, SQLException { 
    CustomerDAO customerDAO=dAOFactory.createCustomerDAO(); 
    AcountDAO acountDAO=dAOFactory.createAcountDAO(); 

    connection.setAutoCommit(false); 
    try{ 

    boolean createNewAccount = customerDAO.createNewAccount(customerDTO); 

    if (createNewAccount) { 

     boolean createNewaccount = acountDAO.createNewaccount(accountDTO); 

      if (createNewaccount) { 

       connection.commit(); 
       return true; 


      }else{ 
       connection.rollback(); 
       return false; 
      } 

    }else{ 
     connection.rollback(); 
     return false; 
    } 
    }finally{ 
     connection.setAutoCommit(true); 
     return true; 
    } 


} 

service facimpl 

private DAOFactory dAOFactory; 

public ServiceFactoryImpl(DAOFactory dAOFactory) { 
    this.dAOFactory = dAOFactory; 
} 

@Override 
public CustomerService createCustomerService() throws NamingException, SQLException { 
    CustomerService customerService=new CustomerServiveImpl(dAOFactory); 
    return customerService; 
} 

web..serv 

Resource resource = new ResourceImpl(); 
DAOFactory dAOFactory = new DAOFactoryImpl(resource); 
ServiceFactory serviceFactory = new ServiceFactoryImpl(dAOFactory); 

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    PrintWriter writer = response.getWriter(); 
    try { 
     String accno=request.getParameter("accno"); 
     AcountService acountService=serviceFactory.createAcountService(); 
     CustomerAccDetailDTO customeraccde = acountService.searchByAccNo(accno); 

     Gson gson=new GsonBuilder().create(); 
     String list = gson.toJson(customeraccde); 
     writer.write(list); 


    } catch (NamingException ex) { 
     ServletException exception=new ServletException(ex); 
     Logger.getLogger(SercletDepositeCustomerSearch.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (SQLException ex) { 
     ServletException exception=new ServletException(ex); 
     Logger.getLogger(SercletDepositeCustomerSearch.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (Exception ex) { 
     ServletException exception=new ServletException(ex); 
     Logger.getLogger(SercletDepositeCustomerSearch.class.getName()).log(Level.SEVERE, null, ex); 
    }finally{ 
     if (writer!=null) { 
      writer.close(); 
     } 





      ajx 

$( "#のserchc")をクリックします(関数(){ するvar QS = + $( "#のaccno")valが()。 "accno =?";。

$.getJSON("SercletDepositeCustomerSearch"+qs,function(respoce){ 
    alert(qs); 
    $("#fname").val(respoce.firstName) ; 
    $("#sname").val(respoce.lastName); 
    $("#nic").val(respoce.nic); 
    $("#add").val(respoce.address) 
    $("#balance").val(respoce.balance); 
    $("#tp").val(respoce.contact) 
}); 

});

関連する問題