分类:
2008-11-16 22:35:03
@Resource(jndiName="java:/DefaultDS") public DataSource customerDB; Resource注释的声明 @Target({TYPE, METHOD, FIELD, PARAMETER}) @Retention(RUNTIME) public @interface Resource { String name() default ""; String resourceType() default ""; AuthenticationType authenticationType() default CONTAINER; boolean shareable() default true; String jndiName() default ""; } public enum Authentication Type { CONTAINER, APPLICATION } @Target(TYPE) @Retention(RUNTIME) public @interface Resources { Resource[] value(); } |
@Inject(jndiName="java:/DefaultDS") public DataSource customerDB; |
@Inject javax.ejb.SessionContext ctx; @Inject javax.ejb.TimerService timer; @Inject javax.ejb.UserTransaction ut; @Inject javax.ejb.EntityManager manager; |
JmsUsers.java package com.kuaff.ejb3.di; import java.util.List; import javax.ejb.Remote; import javax.sql.*; @Remote public interface JmsUsers { public List } |
CounterBean.java package com.kuaff.ejb3.di; import java.util.List; import java.util.ArrayList; import javax.ejb.Stateless; import javax.ejb.Resource; import javax.sql.*; import java.sql.*; @Stateless public class JmsUsersBean implements JmsUsers { @Resource(jndiName="java:/DefaultDS",resourceType="javax.sql.DataSource") public DataSource customerDB; public List { List try { Connection conn = customerDB.getConnection(); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select * from jms_users"); while(rs.next()) { list.add(rs.getString("userid")); } } catch(SQLException e) {} return list; } } |
Client.java package com.kuaff.ejb3.di; import java.util.List; import javax.naming.InitialContext; import javax.naming.NamingException; public class Client { public static void main(String[] args) { InitialContext ctx; try { ctx = new InitialContext(); JmsUsers users = (JmsUsers) ctx.lookup(JmsUsers.class.getName()); List for(String user:jmsUsers) { System.out.printf("用户名:%s%n",user); } } catch (NamingException e) { e.printStackTrace(); } } } |