package com.amit.Dao;
public interface AccountDao
{
public String create(String accNo, String accName, String accType, int balance);
public String search(String accNo);
public String update(String accNo, String accName, String accType, int balance);
public String delete(String accNo);
}
public interface AccountDao
{
public String create(String accNo, String accName, String accType, int balance);
public String search(String accNo);
public String update(String accNo, String accName, String accType, int balance);
public String delete(String accNo);
}
=====================================================================
package com.amit.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import oracle.jdbc.pool.OracleDataSource;
@Repository("dao") //-->to making it autodiscover //you can also use @component("dao")
public class AccountDaoImpl implements AccountDao
{
@Autowired //-------> this will inject datasource object into AccountDao (setter/getters not required)
private OracleDataSource datasource;
String status = "";
@Override
public String create(String accNo, String accName, String accType, int balance)
{
try
{
Connection con = datasource.getConnection();
PreparedStatement pst = con.prepareStatement("insert into account values (?,?,?,?)");
pst.setString(1, accNo);
pst.setString(2, accName);
pst.setString(3, accType);
pst.setInt(4, balance);
pst.executeUpdate();
status= "success";
}
catch(Exception e)
{
status = "failure";
e.printStackTrace();
}
return status;
}
@Override
public String search(String accNo) {
return null;
}
@Override
public String update(String accNo, String accName, String accType, int balance) {
return null;
}
@Override
public String delete(String accNo) {
return null;
}
public void showDetails()
{
}
}
==================================================================
package com.amit.service;
public interface AccountService
{
public String createAccount(String accNo, String accName, String accType, int balance);
public String searchAccount(String accNo);
public String updateAccount(String accNo, String accName, String accType, int balance);
public String deleteAccount(String accNo);
}
==================================================================
package com.amit.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.amit.Dao.AccountDao;
@Service("accService")
public class AccountServiceImpl implements AccountService
{
@Autowired(required=true)
private AccountDao dao;
@Override
public String createAccount(String accNo, String accName, String accType, int balance)
{
return dao.create(accNo, accName, accType, balance);
}
@Override
public String searchAccount(String accNo)
{
return null;
}
@Override
public String updateAccount(String accNo, String accName, String accType, int balance)
{
return null;
}
@Override
public String deleteAccount(String accNo)
{
return null;
}
}
====================================================================
package com.amit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.amit.Dao.AccountDaoImpl;
import com.amit.service.AccountService;
public class Test
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountService accserv = (AccountService)context.getBean("accService");
String status = accserv.createAccount("abc123", "Amit", "Saving", 20000);
if(status.equalsIgnoreCase("success"))
{
System.out.println("Account created successfully");
}
else
{
System.out.println("Oops! something went wrong");
}
}
}
=======================================================================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.amit.service"/>
<context:component-scan base-package="com.amit.Dao"/> <!-- Aee, IOC go to this package and scan it for (component,repository,service,controller) & no need to check for bean config-->
<bean id="datasource" class="oracle.jdbc.pool.OracleDataSource"> <!--Driver class Name is not required as it will take automatically -->
<property name="URL" value = "jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value = "scott"></property>
<property name="Password" value = "tiger"></property>
</bean>
<!-- <bean id="dao" class ="com.amit.Dao.AccountDaoImpl"> </bean> -->
<!-- Not required when we are using Auto-Discovery/streotype ..As ID (dao) is mentioned in AccontDAOImpl @Repository("dao") -->
</beans>
No comments:
Post a Comment