Advertisement
package decodejava;
public class Customer
{
int id;
String name;
String address;
int age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
}
package decodejava;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
public class CustomerDAO
{
String tableName;
private JdbcTemplate jdbcTemplate;
//Creating a table
public void createTable()
{
String query = "create table customer "
+ "("
+ "id int,"
+ "name varchar(40),"
+ "address varchar(200),"
+ "age int"
+ ")";
jdbcTemplate.execute(query);
}
//Getter for JdbcTemplate
public JdbcTemplate getJdbcTemplate()
{
return jdbcTemplate;
}
//Setter for JdbcTemplate
public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
}
//Adding a customer
public void addCustomer(int id, String name, String address, int age)
{
String query ="insert into Customer values(" + id + ",'" + name + "','" + address + "'," + age + ")";
jdbcTemplate.execute(query);
}
//Deleting a customer
public void deleteCustomer(int id)
{
String query = "delete from Customer where id =" + id ;
jdbcTemplate.execute(query);
}
//Extracting a count of all the customers
public int countCustomer()
{
String query = "select count(*) from Customer";
return jdbcTemplate.queryForObject(query, Integer.class);
}
//Getting a List of all customers from database
public List<Customer_Account> getAllCustomer()
{
String query ="select * from Customer_Account";
return jdbcTemplate.query(query, new ResultSetExtractor<List<Customer_Account>>()
//Anonymous Inner Class implementing ResultSetExtractor interface
{
@Override
public List<Customer_Account> extractData(ResultSet rs) throws SQLException, DataAccessException
{
List<Customer_Account> customers = new ArrayList<Customer_Account>();
while(rs.next())
{
Customer_Account customer = new Customer_Account();
customer.setId(rs.getInt("id"));
customer.setName(rs.getString("name"));
customer.setAmount(rs.getInt("amount"));
customer.setAge(rs.getInt("age"));
customers.add(customer);
}
return customers;
}
});
}
}
Advertisement
package decodejava;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Utility {
public static void main(String[] args)
{
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
CustomerDAO customerDAO = context.getBean("CustomerDAOBean",CustomerDAO.class);
System.out.println("Creating a table in database");
customerDAO.createTable();
System.out.println("Adding the customers");
customerDAO.addCustomer(1, "First Customer", "First Address", 23);
customerDAO.addCustomer(2, "Second Customer", "Second Address", 27);
customerDAO.addCustomer(3, "Third Customer", "Third Address", 21);
System.out.println("Getting all the customers from the database");
List<Customer> allCustomers = customerDAO.getAllCustomer();
for(Customer cust : allCustomers)
{
System.out.println("Customer ID : " + cust.getId());
System.out.println("Customer Name : " + cust.getName());
System.out.println("Customer Address : " + cust.getAddress());
System.out.println("Customer Age : " + cust.getAge());
}
System.out.println("Getting the total count of all the Customers");
System.out.println("Total Customers : " + customerDAO.countCustomer());
System.out.println("Deleting a Customer");
customerDAO.deleteCustomer(2);
System.out.println("Getting the new total count of all the Customers after deleting a customer");
System.out.println("Total Customers : " + customerDAO.countCustomer());
}
}
<?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:util="springframework.org/schema/util"
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
https://www.springframework.org/context/spring-context.xsd
">
<bean id="CustomerDAOBean" class="decodejava.CustomerDAO">
<property name="jdbcTemplate" ref="jdbcTemplateBean"></property>
</bean>
<bean id="jdbcTemplateBean" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceBean"></property>
</bean>
<bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"></property>
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
</beans>
Jul 26, 2018 12:31:13 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1ee0005: startup date [Thu Jul 26 12:31:13 2018]; root of context hierarchy
Jul 26, 2018 12:31:13 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
Jul 26, 2018 12:31:14 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
Creating a table in database
Adding the customers
Getting all the customers from the database
Customer ID : 1
Customer Name : First Customer
Customer Address : First Address
Customer Age : 23
Customer ID : 2
Customer Name : Second Customer
Customer Address : Second Address
Customer Age : 27
Customer ID : 3
Customer Name : Third Customer
Customer Address : Third Address
Customer Age : 21
Getting the total count of all the Customers
Total Customers : 3
Deleting a Customer
Getting the new total count of all the Customers after deleting a customer
Total Customers : 2
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement