package com.amit.Dao;
public interface AccountDao
{
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);
}
public interface AccountDao
{
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.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.springframework.beans.factory.annotation.Autowired;
import oracle.jdbc.pool.OracleDataSource;
public class AccountDaoImpl implements AccountDao
{
@Autowired //-------> this will inject datasource object into AccountDao (to avoid setters and getters for datasource)
private OracleDataSource datasource;
String status = "";
@Override
public String createAccount(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 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;
}
public void showDetails()
{
}
}
====================================================================
package com.amit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.amit.Dao.AccountDaoImpl;
public class Test {
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountDaoImpl dao = (AccountDaoImpl)context.getBean("dao");
String status = dao.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:annotation-config/> <!-- When we use @annotation in bean we have to use <contest:ann..../> -->
<bean id="datasource" class="oracle.jdbc.pool.OracleDataSource"> <!-- this id "datasource" is not used any where you can change it -->
<property name="URL" value = "jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value = "scott"></property> <!--Driver class Name is not required as it will take automatically -->
<property name="Password" value = "tiger"></property>
</bean>
<bean id="dao" class ="com.amit.Dao.AccountDaoImpl"> <!-- id "dao" is used by IOC container (getbean uses it) -->
<!-- we don't need to set value of datasource, as we are using autowiring ie.
....<property name="datasource" ref ="datasource"></property> -->
</bean>
</beans>
===================================================================
No comments:
Post a Comment