001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.superbiz.injection;
017:
018: import javax.annotation.PostConstruct;
019: import javax.annotation.Resource;
020: import javax.ejb.Stateful;
021: import javax.sql.DataSource;
022: import java.sql.Connection;
023: import java.sql.PreparedStatement;
024: import java.sql.ResultSet;
025: import java.util.List;
026: import java.util.ArrayList;
027:
028: @Stateful(name="Movies")
029: public class MoviesImpl implements Movies {
030:
031: /**
032: * The field name "movieDatabase" matches the DataSource we
033: * configure in the TestCase via :
034: * p.put("movieDatabase", "new://Resource?type=DataSource");
035: *
036: * This would also match an equivalent delcaration in an openejb.xml:
037: * <Resource id="movieDatabase" type="DataSource"/>
038: *
039: * If you'd like the freedom to change the field name without
040: * impact on your configuration you can set the "name" attribute
041: * of the @Resource annotation to "movieDatabase" instead.
042: */
043: @Resource
044: private DataSource movieDatabase;
045:
046: @PostConstruct
047: private void construct() throws Exception {
048: Connection connection = movieDatabase.getConnection();
049: try {
050: PreparedStatement stmt = connection
051: .prepareStatement("CREATE TABLE movie ( director VARCHAR(255), title VARCHAR(255), year integer)");
052: stmt.execute();
053: } finally {
054: connection.close();
055: }
056: }
057:
058: public void addMovie(Movie movie) throws Exception {
059: Connection conn = movieDatabase.getConnection();
060: try {
061: PreparedStatement sql = conn
062: .prepareStatement("INSERT into movie (director, title, year) values (?, ?, ?)");
063: sql.setString(1, movie.getDirector());
064: sql.setString(2, movie.getTitle());
065: sql.setInt(3, movie.getYear());
066: sql.execute();
067: } finally {
068: conn.close();
069: }
070: }
071:
072: public void deleteMovie(Movie movie) throws Exception {
073: Connection conn = movieDatabase.getConnection();
074: try {
075: PreparedStatement sql = conn
076: .prepareStatement("DELETE from movie where director = ? AND title = ? AND year = ?");
077: sql.setString(1, movie.getDirector());
078: sql.setString(2, movie.getTitle());
079: sql.setInt(3, movie.getYear());
080: sql.execute();
081: } finally {
082: conn.close();
083: }
084: }
085:
086: public List<Movie> getMovies() throws Exception {
087: ArrayList<Movie> movies = new ArrayList<Movie>();
088: Connection conn = movieDatabase.getConnection();
089: try {
090: PreparedStatement sql = conn
091: .prepareStatement("SELECT director, title, year from movie");
092: ResultSet set = sql.executeQuery();
093: while (set.next()) {
094: Movie movie = new Movie();
095: movie.setDirector(set.getString("director"));
096: movie.setTitle(set.getString("title"));
097: movie.setYear(set.getInt("year"));
098: movies.add(movie);
099: }
100:
101: } finally {
102: conn.close();
103: }
104: return movies;
105: }
106:
107: }
|