25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9 ■ Adding Persistence to Your <strong>Spring</strong> Application<br />

import javax.sql.DataSource;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.stereotype.Repository;<br />

import com.apress.isf.java.model.Document;<br />

import com.apress.isf.java.model.Type;<br />

import com.apress.isf.spring.data.DocumentDAO;<br />

@Repository("documentDAO")<br />

public class AnnotatedDocumentRepository implements DocumentDAO {<br />

private static final String queryAll = " select d.documentId, d.name, d.location,<br />

d.description as doc_desc," +<br />

"d.typeId, d.created, d.modified, "+<br />

"t.name as type_name, t.description as type_desc," +<br />

"t.extension from documents d join types t on d.typeId = t.typeId";<br />

@Autowired<br />

private DataSource dataSource;<br />

public List getAll() {<br />

List result = new ArrayList();<br />

Connection connection = null;<br />

Statement statement = null;<br />

ResultSet resultSet = null;<br />

Document document = null;<br />

Type type=null;<br />

try {<br />

connection = dataSource.getConnection();<br />

statement = connection.createStatement();<br />

resultSet = statement.executeQuery(queryAll);<br />

while (resultSet.next()) {<br />

document = new Document();<br />

document.setDocumentId(resultSet.getString("documentId"));<br />

document.setName(resultSet.getString("name"));<br />

document.setLocation(resultSet.getString("location"));<br />

document.setCreated(resultSet.getDate("created"));<br />

document.setModified(resultSet.getDate("modified"));<br />

document.setDescription("doc_desc");<br />

type = new Type();<br />

type.setTypeId(resultSet.getString("typeId"));<br />

type.setName(resultSet.getString("type_name"));<br />

type.setDesc(resultSet.getString("type_desc"));<br />

type.setExtension(resultSet.getString("extension"));<br />

document.setType(type);<br />

result.add(document);<br />

}<br />

} catch (SQLException ex) {<br />

throw new RuntimeException(ex);<br />

} finally {<br />

127

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!