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 com.apress.isf.java.model.Document;<br />

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

public class DocumentRepository implements DocumentDAO {<br />

}<br />

private DataSource dataSource;<br />

public void setDataSource(DataSource dataSource) {<br />

this.dataSource = dataSource;<br />

}<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("select * from documents");<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 />

result.add(document);<br />

}<br />

} catch (SQLException ex) {<br />

throw new RuntimeException(ex);<br />

} finally {<br />

if (null != connection) {<br />

try {<br />

connection.close();<br />

} catch (SQLException ex) {<br />

}<br />

}<br />

}<br />

return result;<br />

}<br />

117

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

Saved successfully!

Ooh no, something went wrong!