Search This Blog

Friday, 24 August 2012

Spring And The Actual DB Operations

In the last few posts we saw how we could use Spring to create data-sources to interact with the databases. However we never wrote any code to actually do anything in the database. Spring as discussed earlier provides integration via direct JDBC and also via ORM frameworks. We shall start by investigating Spring's JDBC integration mechanism.
For this I created a simple table
create database asocn;
use asocn;

 CREATE TABLE `person` (
  `ID` INT(11) NOT NULL AUTO_INCREMENT,
  `NAME` VARCHAR(255) DEFAULT NULL,
  `AGE` INT(11) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;
The database I used is MySQL. To represent the data in the table I created a simple POJO class:
public class Person {
    private String name;
    private Long id;
    private Integer age;
//setter-getters
}
Also to perform the CRUD operations I created an interface:
public interface IPersonDAO {

    public List<Person> getAllPersons();

    public int findTotalPersons();

    public Person getPersonById(long personId);

    public void updatePerson(Person person);

    public void save(Person person);

    public void delete(Person person);
}
I plan to use the same set of classes and tables for the examples ahead
Also for all the example I integrated the database via the datasource :
<bean id="c3pDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="org.gjt.mm.mysql.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/asocn" />
    <property name="user" value="root" />
    <property name="password" value="root" />
</bean>
In this case I have integrated with c3p0.

1 comment:

  1. If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (no matter why you broke up) you must watch this video
    right away...

    (VIDEO) Have your ex CRAWLING back to you...?

    ReplyDelete