Tuesday, March 26, 2013

Spring Framework - Example of Database Connectivity with DataSource


After lot of basic tutorials and topics now we start database connectivity .In spring framework DataSource in used to define the database configuration.In XML configuration file use DriverManagerDataSource class is used to set the database configuration like driver,pool,username and password.so understand clearly here below is a simple example-
Directory Structure-
                                                           


class Student.java-

package com.test.student;

public class Student {
int id;
String name;
public int getId() {
return id;
}
public Student(int id, String name) {
super();
setId(id);
setName(name);
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Class DaoImpl.java

package com.test.daoimp;

import java.sql.Connection;
/*import java.sql.DriverManager;*/
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.test.student.Student;
@Component
public class DaoImpl {
Student stu=null;
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public Student getStu(int id)
{
try {

  Connection con=dataSource.getConnection();

  PreparedStatement st=con.prepareStatement("select * from stu where id=?");

  st.setInt(1, id);
  ResultSet rs=st.executeQuery();
 if( rs.next())
  {
  stu=new Student(id,rs.getString("name"));
  }

  con.close();
  st.close();
  rs.close();


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;


}
@PostConstruct
public void initialmethod()
{
System.out.println("Welcome to database");
}

@PreDestroy
public void destroymethod()
{
System.out.println("!!!!!!!!!!!GooD Bye!!!!!!!!");
}

}

@PostConstruct annotation used method call after the constructor is initialized and @Predestroy annotate method is calls when the scope or datasoure object scope is finished ,to call the predestory need to call the method registerShutdownHook by the AbstractApplicationContext object which is define as in main.java class.


Class Main.java-

package com.test.Dao;

/*import java.io.DataInputStream;*/
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.daoimp.DaoImpl;

public class Main {

public static void main(String args[])
{

AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
ctx.registerShutdownHook();
    DaoImpl di=ctx.getBean("daoImpl",DaoImpl.class);
    System.out.println("Student name is="+di.getStu(1).getName());

}

}
spring.xml-

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
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"
xmlns:context="http://www.springframework.org/schema/context">

<context:annotation-config></context:annotation-config>

<context:component-scan base-package="com.test"></context:component-scan>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/Student"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<!-- <property name="initalSize" value="2"></property>
<property name="maxActive" value="5"></property> -->
     </bean>
</beans>

Output-

Welcome to database
Student name is=navin
!!!!!!!!!!!GooD Bye!!!!!!!!


For Further Reading,
General, Java, spring

0 comments:

Post a Comment


 

Site Status

Man Behind Technical Today

Hello, I am Navin Bansal. I am a student of MCA in Rajsthan Institute of Engineering and Technology and owner of this blog. I share my view and ideas among people.