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 />

Now that you have your Schema and Data files, let’s access them, read them, and, of course, initialize the<br />

database. Let’s look at Listing 9-10 to see how you are going to modify the DocumentDAO implementation.<br />

Listing 9-10. DocumentRepository.java<br />

package com.apress.isf.spring.data;<br />

import java.io.IOException;<br />

import java.io.InputStream;<br />

import java.sql.Connection;<br />

import java.sql.ResultSet;<br />

import java.sql.SQLException;<br />

import java.sql.Statement;<br />

import java.util.ArrayList;<br />

import java.util.List;<br />

import java.util.Scanner;<br />

import javax.sql.DataSource;<br />

import org.springframework.core.io.Resource;<br />

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

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

public class DocumentRepository implements DocumentDAO {<br />

private String queryAll;<br />

private DataSource dataSource;<br />

private Resource schema;<br />

private Resource data;<br />

public void setQueryAll(String queryAll) {<br />

this.queryAll = queryAll;<br />

}<br />

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

this.dataSource = dataSource;<br />

}<br />

public void setSchema(Resource schema) {<br />

this.schema = schema;<br />

}<br />

public void setData(Resource data) {<br />

this.data = data;<br />

}<br />

public void initialize() {<br />

try {<br />

InputStream stream = schema.getInputStream();<br />

Scanner scanner = new Scanner(stream);<br />

StringBuilder sql = new StringBuilder();<br />

122

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

Saved successfully!

Ooh no, something went wrong!