Hi Rafal :

Based on the links : http://www.vogella.de/articles/SpringJDBC/article.html

First : you need to add spring-jdbc.jar to your classpath (in addition to
the standard jars described in the above installation link).

Second : Create the POJO of something ( as your domain MODEL ) say :

package domainmodel ;

public class Something{

 private String info1
 private String info2

  ... getter and setter
}

third :
Arrange your DAO interface to something like :

package dao;

import java.util.List;

import javax.sql.DataSource;

import domainmodel.Something;

public interface IDao {

        void setDataSource(DataSource ds);

     public Map<String, List<ServiceMessageVO>> getAllServiceMessages();

     //*** do some VO to MODEL conversion or VO as your MODEL is fine
}


Forth : Create DAO with corresponding jdbctemplate :

                                
package dao.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.ResultSetExtractor;

import domainmodel.something; // or VO in your case

public class SomethingResultSetExtractor implements ResultSetExtractor {

        @Override
        public Object extractData(ResultSet rs) throws SQLException {
                 Something something = new Something();
                        something.setInfo1(rs.getString(1)) ;
               something.setInfo2(rs.getString(2)) ;
           ...
           return something;
        }

}

                                                        
package dao.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public class SomethingRowMapper implements RowMapper {

        @Override
        public Object mapRow(ResultSet rs, int line) throws SQLException {
                
         SomethingResultSetExtractor extractor = new
SomethingResultSetExtractor();
                return extractor.extractData(rs);
        }

}

                        
package dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import dao.mapper.SomethingRowMapper;
import domainmodel.Something;

public class DerbyDao implements IDao {
        private DataSource dataSource;

        public void setDataSource(DataSource ds) {
                dataSource = ds;
        }   <--- inject DataSource

        public void create(String info1, String info2) {
                JdbcTemplate insert = new JdbcTemplate(dataSource);
                insert.update("INSERT INTO SOMETHING (INFOONE, INFOTWO) 
VALUES(?,?)",
                                new Object[] { info1, info2 });
        }

        public List<Something> select(String info1, String info2) {
                JdbcTemplate select = new JdbcTemplate(dataSource);
                return select
                                .query(
                                                "select  INFOONE, INFOTWO from 
SOMETHING where INFOONE = ?
                              AND INFOTWO= ?",
                                                new Object[] { info1, info2 },
                                                new SomethingRowMapper());
        }

        public List<Something> selectAll() {
                JdbcTemplate select = new JdbcTemplate(dataSource);
           ...          
        }

        public void deleteAll() {
                JdbcTemplate delete = new JdbcTemplate(dataSource);
                delete.update("DELETE from SOMETHING");
        }

         ...

}

I guess thats you should figure out them now.


cheers


bb

2010/3/13 Rafał Laczek <rafal_lac...@wp.pl>

> Hi,
>
>
> The Spring is quite new for me but in my project I must use it to
> extract data from database.
> Bellow I send you code.
> Unfortunately loading of the context should be left to the Spring
> listener instead of this explicit creation.
>
> Could you help me please to change this code.
>
> Thank you very much for support.
>
>
> Regards,
>
> Rafal
>
>
> public class JdbcService{
>   private static final Log log = LogFactory.getLog(JdbcService.class);
>
> ApplicationContext ac = new
> ClassPathXmlApplicationContext("application-context.xml");
>
> DataSource datasource = (DataSource) ac.getBean("ADataSource",
> DataSource.class);
>
> public Map<String, List<ServiceMessageVO>> getAllServiceMessages() {
>
>   Map<String, List<ServiceMessageVO>> ret = new HashMap<String,
> List<ServiceMessageVO>>();
>
>            Connection c = null;
>
>            try {
>
>                  c = datasource.getConnection();
>
>                   if ( c == null ) {
>
>                     try {
>
>                             throw new Exception("Data source not found!");
>
>                          } catch (Exception e) {
>
>                             e.printStackTrace();
>
>                          }
>
>                  }else{
>
>                        System.out.println("DATA SOURCE IS FOUND");
>
>                  }
>
>
>  String sql = "SELECT sometging FROM TABLE";
>  PreparedStatement pstm = (PreparedStatement) c.prepareStatement();
>  ResultSet rs = pstm.executeQuery(sql);
>    while (rs.next()) {.........
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Java EE (J2EE) Programming with Passion!" group.
> To post to this group, send email to
> java-ee-j2ee-programming-with-passion@googlegroups.com
> To unsubscribe from this group, send email to
> java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com<java-ee-j2ee-programming-with-passion%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
>
> http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Java EE (J2EE) Programming with Passion!" group.
To post to this group, send email to
java-ee-j2ee-programming-with-passion@googlegroups.com
To unsubscribe from this group, send email to
java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en

Reply via email to