⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysessionejb.java

📁 功能完善的java开发框架
💻 JAVA
字号:
/*
 * Copyright 2003-2005 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package test;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


/**
 * @ejb.bean name="MySession"
 *           display-name="Name for MySession"
 *           description="Description for MySession"
 *           local-jndi-name="ejb/MySession"
 *           type="Stateless"
 *           view-type="local"  
 * 
 * 
 *           
 */
public class MySessionEJB implements SessionBean {

    /**
     * 
     */
    public MySessionEJB() {
        super();
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    /**
     * Default create method
     * 
     * @throws CreateException
     * @ejb.create-method 
     * @ejb.permission role-name = "User"
     *
     */
    public void ejbCreate() throws CreateException {
        try {
            Context ctx = new InitialContext();
            ds = (DataSource) ctx.lookup("java:/TestDS");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    DataSource ds;


    /**
     * 
     * Business method
     * @ejb.interface-method  view-type = "local"
     */
    public Integer count(int value) {
        value = value + 10;
        return new Integer(value);

    }
    /**
     * Business method
     * @ejb.interface-method  view-type = "local"
     * @ejb.permission role-name = "User"
     */
    public test.TestUser getUser(String userId) {        
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        TestUser user = null;
        try {
            conn = ds.getConnection();
            ps = conn.prepareStatement("select * from testuser where userId = ?");
            ps.setString(1, userId);
            rs = ps.executeQuery();
            if (rs.next()){
                user = new TestUser();
                user.setName(rs.getString("name"));
                user.setUserId(userId);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try{
            if (rs != null)
                 rs.close();
            if (ps != null)
                 ps.close();
            if (conn != null)
                conn.close();
            }catch(Exception ex){
                
            }
        }
        return user;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -