messageboardbean.java

来自「精通NetBeans光盘源代码,很好很好的资料」· Java 代码 · 共 99 行

JAVA
99
字号
package org.netbeans.jms;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.ejb.*;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 * This is the bean class for the MessageBoardBean enterprise bean.
 * Created 2006-5-20 1:13:25
 * @author boyingking
 */
public class MessageBoardBean implements MessageDrivenBean, MessageListener {
    private MessageDrivenContext context;
    
    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click on the + sign on the left to edit the code.">
    
    /**
     * @see javax.ejb.MessageDrivenBean#setMessageDrivenContext(javax.ejb.MessageDrivenContext)
     */
    public void setMessageDrivenContext(MessageDrivenContext aContext) {
        context = aContext;
    }
    
    /**
     * See section 15.4.4 of the EJB 2.0 specification
     * See section 15.7.3 of the EJB 2.1 specification
     */
    public void ejbCreate() {
        // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    }
    
    /**
     * @see javax.ejb.MessageDrivenBean#ejbRemove()
     */
    public void ejbRemove() {
        // TODO release any resource acquired in ejbCreate.
        // The code here should handle the possibility of not getting invoked
        // See section 15.7.3 of the EJB 2.1 specification
    }
    
    // </editor-fold>
    
    public void onMessage(Message aMessage) {
        MapMessage mapmessage = (MapMessage)aMessage;
        String[] val=new String[6];
        try {
            val[0]=mapmessage.getString("bugProduct");
            val[1]=mapmessage.getString("title");
            val[2]=mapmessage.getString("name");
            val[3]=mapmessage.getString("contactType");
            val[4]=mapmessage.getString("content");
            val[5]=new Date().toLocaleString();            
            val[0]=new String(val[0].getBytes(),"ISO-8859-1");
            val[1]=new String(val[1].getBytes(),"ISO-8859-1");
            val[2]=new String(val[2].getBytes(),"ISO-8859-1");
            val[3]=new String(val[3].getBytes(),"ISO-8859-1");
            val[4]=new String(val[4].getBytes(),"ISO-8859-1");
            val[5]=new String(val[5].getBytes(),"ISO-8859-1");
            Connection con=this.getConnection();
            PreparedStatement ps=con.prepareStatement("insert into options values(?,?,?,?,?,?)");
            ps.setString(1,val[0]);
            ps.setString(2,val[1]);
            ps.setString(3,val[2]);
            ps.setString(4,val[3]);
            ps.setString(5,val[4]);
            ps.setString(6,val[5]);
            ps.executeUpdate();
            con.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    private Connection getConnection() throws SQLException{
        Context ctx = null;
        DataSource ds = null;
        String url = "localhost:1099";
        try {
            Hashtable h = new Hashtable( );
            h.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
            h.put(Context.PROVIDER_URL, url);
            ctx = new InitialContext(h);
            ds = (DataSource) ctx.lookup("java:mdBeanDB");//查找数据源
        } catch(Exception ne) {
            System.out.println("UNABLE to get a connection from !");
        }
        return ds.getConnection();//返回数据库连接
    }
}

⌨️ 快捷键说明

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