Injection Of Datasource

Similar documents
Injection Of Entitymanager

Testing Transactions BMT

Alternate Descriptors

Example ear-testing can be browsed at

EJB 2.1 CMP EntityBeans (CMP2)

Example schedule-expression can be browsed at

Example custom-injection can be browsed at

ClassLevelInterceptorOne

Dynamic Datasource Routing

Simple Stateless with Descriptor

Example injection-of-env-entry can be browsed at


Dynamic Implementation

Example simple-mdb can be browsed at

Example cdi-request-scope can be browsed at

Webservice Inheritance

Component Interfaces

CDI Produces Disposes

Example rest-on-ejb can be browsed at

bean-validation-design-by-contract

@Asynchronous Methods

Dynamic DAO Implementation

Arquillian Persistence Extension

Example cdi-produces-field can be browsed at

Custom resources in an EAR archive

@WebService handlers

EJB applications guided by tests Jakub Marchwicki

1. PhP Project. Create a new PhP Project as shown below and click next

Enterprise JavaBeans. Layer:08. Persistence

Stateless Session Bean

EJB - ACCESS DATABASE

WEB SERVICES EXAMPLE 2

SQream Connector JDBC SQream Technologies Version 2.9.3

ApplicationComposer. The TomEE Swiss Knife

Example rest-xml-json can be browsed at

Oracle Database 10g Java Web

Using a CVP VoiceXML application to implement a logical shadow queue for ICM

Example cdi-interceptors can be browsed at

WebSphere Connection Pooling. by Deb Erickson Shawn Lauzon Melissa Modjeski

<<Interface>> EntityBean (from ejb) EJBHome. <<Interface>> CountHome. (from entity) create() findbyprimarykey() <<Interface>> EJBObject.

Practice for JEE5. Using stateless session EJB 3.0 as a business layer, ADF data Model as a model layer and ADF faces as a view layer. Version 1.

Why use a database? You can query the data (run searches) You can integrate with other business systems that use the same database You can store huge

Integration Unit Testing on SAP NetWeaver Application Server

WebLogic-real-Life Documentation

Practice for JEE5 for JDeveloper 11g

Servlet 5.1 JDBC 5.2 JDBC

Oracle EXAM - 1Z Java EE 6 Enterprise JavaBeans Developer Certified Expert Exam. Buy Full Product.

JDBC Java Database Connectivity is a Java feature that lets you connect

SPRING DECLARATIVE TRANSACTION MANAGEMENT

Design and Analysis of Information Systems (MAS)

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

The Many Faces Of Apache Ignite. David Robinson, Software Engineer May 13, 2016

<Insert Picture Here> Productive JavaEE 5.0 Development

Exploring EJB3 With JBoss Application Server Part - 5

MODEL DRIVEN DEVELOPMENT OF COMPONENTS (IS7/IV2009)

Content Services for JDBC Driver User Guide

Database Application Development

Lab1: Stateless Session Bean for Registration Fee Calculation

Dealing with Enterprise Database Challenges

Accessing EJB in Web applications

Programming a Bank Database. We ll store the information in two tables: INTEGER DECIMAL(10, 2)

Develop an Enterprise Java Bean for Banking Operations

Stateful Session Beans

JBuilder EJB. Development Using JBuilder 4 and Inprise Application Server 4.1. Audience. A Step-by-step Tutorial.

JDBC [Java DataBase Connectivity]

Web Applications and Database Connectivity using JDBC (Part II)

Schema Null Cannot Be Resolved For Table Jpa

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

Data Access on Tourism Resources Management System Based on Spring JDBC Jifu Tong

DATABASE DESIGN I - 1DL300

WHAT IS EJB. Security. life cycle management.

Week 11 : Shadowed Fields in Java Lecture Handout Instructor: Deb Deppeler

Exam Questions 1Z0-895

Workbook 5. Introduction. Using a database in Java. Important

CSE 510 Web Data Engineering

These notes add to the JDBC discussion your textbook.

Designing a Persistence Framework

Quiz System Report. Independent Study. Back-end 2 Servlet 8 Front-end 9 Relationship among back-end, servlet and front-end 9

EJB - DEPENDENCY INJECTION

Connessione.java. package model.entity; import java.sql.connection; public class Connessione { public Connection objconn;

Simple Data Source Crawler Plugin to Set the Document Title

V3 EJB Test One Pager

// Static initializer block to load the JDBC Driver static {

Introducing SQL Query Verifier Plugin

Simple JDBC with Spring 2.5

Student Number: Please fill out the identification section above as well as the one on the back page, and read the instructions below. Good Luck!

INF 102 CONCEPTS OF PROG. LANGS ADVERSITY. Instructors: James Jones Copyright Instructors.

Database Application Development

Université Antonine - Baabda

JDBC, Transactions. Niklas Fors JDBC 1 / 38

IBM. IBM WebSphere Application Server Migration Toolkit. WebSphere Application Server. Version 9.0 Release

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Java.sql.sqlexception closed connection at. oracle.jdbc.driver.physicalconnection.pr eparestatement

Developing a JAX-WS EJB Stateless Session Bean Web Service

Introduction to Databases

Introduction to Session beans. EJB - continued

BEAWebLogic Server. Monitoring and Managing with the Java EE Management APIs

NetBeans IDE Field Guide

Döcu Content IBM Notes Domino, DB2 Oracle JDeveloper, WebLogic

OCP JavaEE 6 EJB Developer Study Notes

Transcription:

Injection Of Datasource

Example injection-of-datasource can be browsed at https://github.com/apache/tomee/tree/master/examples/injection-of-datasource Help us document this example! Click the blue pencil icon in the upper right to edit this page. Movie 1

package org.superbiz.injection; /** * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23-0800 (Thu, 27 Dec 2007) $ */ public class Movie { private String director; private String title; private int year; public Movie() { public Movie(String director, String title, int year) { this.director = director; this.title = title; this.year = year; public String getdirector() { return director; public void setdirector(string director) { this.director = director; public String gettitle() { return title; public void settitle(string title) { this.title = title; public int getyear() { return year; public void setyear(int year) { this.year = year; Movies package org.superbiz.injection; 2

import javax.annotation.postconstruct; import javax.annotation.resource; import javax.ejb.stateful; import javax.sql.datasource; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.util.arraylist; import java.util.list; @Stateful public class Movies { /** * The field name "moviedatabase" matches the DataSource we * configure in the TestCase via : * p.put("moviedatabase", "new://resource?type=datasource"); * <p/> * This would also match an equivalent delcaration in an openejb.xml: * <Resource id="moviedatabase" type="datasource"/> * <p/> * If you'd like the freedom to change the field name without * impact on your configuration you can set the "name" attribute * of the @Resource annotation to "moviedatabase" instead. */ @Resource private DataSource moviedatabase; @PostConstruct private void construct() throws Exception { Connection connection = moviedatabase.getconnection(); try { PreparedStatement stmt = connection.preparestatement("create TABLE movie ( director VARCHAR(255), title VARCHAR(255), year integer)"); stmt.execute(); finally { connection.close(); public void addmovie(movie movie) throws Exception { Connection conn = moviedatabase.getconnection(); try { PreparedStatement sql = conn.preparestatement("insert into movie (director, title, year) values (?,?,?)"); sql.setstring(1, movie.getdirector()); sql.setstring(2, movie.gettitle()); sql.setint(3, movie.getyear()); sql.execute(); finally { 3

conn.close(); public void deletemovie(movie movie) throws Exception { Connection conn = moviedatabase.getconnection(); try { PreparedStatement sql = conn.preparestatement("delete from movie where director =? AND title =? AND year =?"); sql.setstring(1, movie.getdirector()); sql.setstring(2, movie.gettitle()); sql.setint(3, movie.getyear()); sql.execute(); finally { conn.close(); public List<Movie> getmovies() throws Exception { ArrayList<Movie> movies = new ArrayList<Movie>(); Connection conn = moviedatabase.getconnection(); try { PreparedStatement sql = conn.preparestatement("select director, title, year from movie"); ResultSet set = sql.executequery(); while (set.next()) { Movie movie = new Movie(); movie.setdirector(set.getstring("director")); movie.settitle(set.getstring("title")); movie.setyear(set.getint("year")); movies.add(movie); finally { conn.close(); return movies; MoviesTest 4

package org.superbiz.injection; import junit.framework.testcase; import javax.ejb.embeddable.ejbcontainer; import javax.naming.context; import java.util.list; import java.util.properties; //START SNIPPET: code public class MoviesTest extends TestCase { public void test() throws Exception { Properties p = new Properties(); p.put("moviedatabase", "new://resource?type=datasource"); p.put("moviedatabase.jdbcdriver", "org.hsqldb.jdbcdriver"); p.put("moviedatabase.jdbcurl", "jdbc:hsqldb:mem:moviedb"); Context context = EJBContainer.createEJBContainer(p).getContext(); Movies movies = (Movies) context.lookup("java:global/injection-ofdatasource/movies"); movies.addmovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); movies.addmovie(new Movie("Joel Coen", "Fargo", 1996)); movies.addmovie(new Movie("Joel Coen", "The Big Lebowski", 1998)); List<Movie> list = movies.getmovies(); assertequals("list.size()", 3, list.size()); for (Movie movie : list) { movies.deletemovie(movie); assertequals("movies.getmovies()", 0, movies.getmovies().size()); Running ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.superbiz.injection.moviestest Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 http://tomee.apache.org/ 5

INFO - openejb.home = /Users/dblevins/examples/injection-of-datasource INFO - openejb.base = /Users/dblevins/examples/injection-of-datasource INFO - Using 'javax.ejb.embeddable.ejbcontainer=true' INFO - Configuring Service(id=Default Security Service, type=securityservice, provider-id=default Security Service) INFO - Configuring Service(id=Default Transaction Manager, type=transactionmanager, provider-id=default Transaction Manager) INFO - Configuring Service(id=movieDatabase, type=resource, provider-id=default JDBC Database) INFO - Found EjbModule in classpath: /Users/dblevins/examples/injection-ofdatasource/target/classes INFO - Beginning load: /Users/dblevins/examples/injection-of-datasource/target/classes INFO - Configuring enterprise application: /Users/dblevins/examples/injection-ofdatasource WARN - Method 'lookup' is not available for 'javax.annotation.resource'. Probably using an older Runtime. INFO - Configuring Service(id=Default Stateful Container, type=container, providerid=default Stateful Container) INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=default Stateful Container) INFO - Auto-linking resource-ref 'java:comp/env/org.superbiz.injection.movies/moviedatabase' in bean Movies to Resource(id=movieDatabase) INFO - Configuring Service(id=Default Managed Container, type=container, providerid=default Managed Container) INFO - Auto-creating a container for bean org.superbiz.injection.moviestest: Container(type=MANAGED, id=default Managed Container) INFO - Enterprise application "/Users/dblevins/examples/injection-of-datasource" loaded. INFO - Assembling app: /Users/dblevins/examples/injection-of-datasource INFO - Jndi(name="java:global/injection-ofdatasource/Movies!org.superbiz.injection.Movies") INFO - Jndi(name="java:global/injection-of-datasource/Movies") INFO - Jndi(name="java:global/EjbModule1508028338/org.superbiz.injection.MoviesTest!org.super biz.injection.moviestest") INFO - Jndi(name="java:global/EjbModule1508028338/org.superbiz.injection.MoviesTest") INFO - Created Ejb(deployment-id=Movies, ejb-name=movies, container=default Stateful Container) INFO - Created Ejb(deployment-id=org.superbiz.injection.MoviesTest, ejbname=org.superbiz.injection.moviestest, container=default Managed Container) INFO - Started Ejb(deployment-id=Movies, ejb-name=movies, container=default Stateful Container) INFO - Started Ejb(deployment-id=org.superbiz.injection.MoviesTest, ejbname=org.superbiz.injection.moviestest, container=default Managed Container) INFO - Deployed Application(path=/Users/dblevins/examples/injection-of-datasource) Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.276 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 6

7