Spring Datasource Config Reading Project

Click here to download eclipse supported ZIP file



This is CustomerDao.java file having the DAO logic to access the database information.


 

    
package com.cv.spring.datasource.dao;

import com.cv.spring.datasource.model.Customer;

/**
 @author Chandra Vardhan
 */
public interface CustomerDao {
  
  public void insert(Customer customer);

  public Customer findByCustomerId(int custId);
}


This is CustomerDaoImpl.java file having the DAO logic to access the database information.


 

    
package com.cv.spring.datasource.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.cv.spring.datasource.model.Customer;

/**
 @author Chandra Vardhan
 */
public class CustomerDaoImpl implements CustomerDao {
  
  private DataSource dataSource;

  public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
  }

  public void insert(Customer customer) {

    String sql = "INSERT INTO CUSTOMER "
        "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
    Connection conn = null;

    try {
      conn = dataSource.getConnection();
      PreparedStatement ps = conn.prepareStatement(sql);
      ps.setInt(1, customer.getCustId());
      ps.setString(2, customer.getName());
      ps.setInt(3, customer.getAge());
      ps.executeUpdate();
      ps.close();

    catch (SQLException e) {
      throw new RuntimeException(e);

    finally {
      if (conn != null) {
        try {
          conn.close();
        catch (SQLException e) {
        }
      }
    }
  }

  public Customer findByCustomerId(int custId) {

    String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";

    Connection conn = null;

    try {
      conn = dataSource.getConnection();
      PreparedStatement ps = conn.prepareStatement(sql);
      ps.setInt(1, custId);
      Customer customer = null;
      ResultSet rs = ps.executeQuery();
      if (rs.next()) {
        customer = new Customer(rs.getInt("CUST_ID"),
            rs.getString("NAME"), rs.getInt("Age"));
      }
      rs.close();
      ps.close();
      return customer;
    catch (SQLException e) {
      throw new RuntimeException(e);
    finally {
      if (conn != null) {
        try {
          conn.close();
        catch (SQLException e) {
        }
      }
    }
  }
}


This is SimpleJdbcTemplateDao.java file having the DAO logic to access the database information.


 

    
package com.cv.spring.datasource.dao;

import java.util.HashMap;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.cv.spring.datasource.model.Customer;


/**
 @author Chandra Vardhan
 */
public class SimpleJdbcTemplateDao implements CustomerDao {


  private JdbcTemplate jdbcTemplate;

  public JdbcTemplate getJdbcTemplate() {
    return jdbcTemplate;
  }

  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }
  public void insert(Customer customer) {

    String sql = "INSERT INTO CUSTOMER "
        "(CUST_ID, NAME, AGE) VALUES (:cust_id, :name, :age)";

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("cust_id", customer.getCustId());
    params.put("name", customer.getName());
    params.put("age", customer.getAge());
    
    jdbcTemplate.update(sql, params);
  }

  @Override
  public Customer findByCustomerId(int custId) {

    return null;
  }
}


This is MainProgram.java file having the source code to execute business logic.


 

    
package com.cv.spring.datasource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cv.spring.datasource.dao.CustomerDao;
import com.cv.spring.datasource.model.Customer;

/**
 @author Chandra Vardhan
 */
public class MainProgram {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
        "spring.xml");

    CustomerDao customerDao = (CustomerDaocontext.getBean("customerDAO");    
    Customer customer = new Customer(100"Chandra"500);
    customerDao.insert(customer);

    // Customer customer1 = customerDAO.findByCustomerId(1);
    // LOGGER.info(customer1);

  }
}


This is Customer.java file having the source code to execute business logic.


 

    
package com.cv.spring.datasource.model;

/**
 @author Chandra Vardhan
 */
public class Customer {

  private String name;
  private int age;
  private int custId;

  public Customer() {
     
   }
  public Customer(int custID, String string, int age1) {
    custId = custID;
    name = string;
    age = age1;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getCustId() {
    return custId;
  }

  public void setCustId(int custId) {
    this.custId = custId;
  }

}




This is pom.xml file having the entries of dependency jars and information to build the application .


	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">.0" encoding="UTF-8"?> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.annotation</groupId> <artifactId>SpringDatasourceConfigReading</artifactId> <name>SpringDatasourceConfigReading</name> <packaging>jar</packaging> <version>1.0</version> <properties> <java-version>1.8</java-version> <log4j-version>1.2.16</log4j-version> <org.springframework-version>4.2.0.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901.jdbc4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>


This is datasource.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/hibernate" />
<property name="username" value="postgres" />
<property name="password" value="password" />
</bean>
</beans>


This is ref.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="customerDAO" class="com.cv.spring.datasource.dao.CustomerDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>


This is spring.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<import resource="ref.xml" />
<import resource="datasource.xml" />

</beans>



This is log4j.properties file having the entries for logging the information into the console/file.



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


No comments:

Post a Comment