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;
}
}
答えて
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)
});
});
- 1. Exchangeへの接続方法
- 2. C++からの接続信号への接続方法は?
- 3. postgresへの接続方法sqlalchemyとの最大接続?
- 4. mysqlテーブルへの接続方法
- 5. プールへの接続を返す方法
- 6. Wifimanager WEP/WAPネットワークへの接続方法
- 7. 接続URLへのアクセス方法
- 8. ミュールリポジトリへの接続方法は?
- 9. Linq - データベースへの接続方法?
- 10. 外部プログラムへの接続方法
- 11. Oracle Dockerイメージへの接続方法
- 12. データベース接続のためのドッカーコンテナへのasp.net web.config接続を指す方法?
- 13. どのようにMySQLに接続しますか? MySQLへの接続方法
- 14. iOS BLEペリフェラルへの接続と接続を維持する最良の方法
- 15. SQL接続の断片化、DBへの接続方法が異なります
- 16. .NetへのOracle接続 - 接続プール
- 17. ブラックベリーサーバー接続アドレスへのhttp接続
- 18. Azure ServiceBus接続への接続
- 19. メモリへの接続
- 20. オンラインデータベースへの接続#
- 21. ホットスポットへの接続
- 22. ユニットテストクラウドストレージへの接続?
- 23. HeidiSQLは、MySQLサーバへの接続 - データベースへの接続に初期
- 24. Rシャイニー接続への赤方偏移
- 25. VS2005接続からTFS2015接続へ
- 26. application.ymlのpostgresqlへの初期サイズ接続の設定方法
- 27. JavaのMySQLとPostgreSQLのAzureデータベースへの接続方法
- 28. パテントのリモートデスクトップへのトンネル接続を定義する方法は?
- 29. navコントローラからタブコントローラへの接続とルートビューコントローラの作成方法
- 30. C#からリモートcouchbaseデータベースへのデータベース接続の作成方法
ようこそ。 [ask]を読むのに少し時間がかかる。あなたが直面している**正確**問題は何ですか? – OldProgrammer