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

📄 connectjcabean.java

📁 BEA WebLogic Server 8.1大全 = BEA webLogic server 8.1 unleashed (美) Mark Artiges等著 袁毅 ... [等] 译 eng
💻 JAVA
字号:
package jcawebapp;

import javax.ejb.*;
import java.util.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
/**This Class encapsulates the SessionBean for looking up and connecting to
 * our J2EE resource adapter.  the method called is getJCA(String,String) which
 * returns a Vector of String[] containing the data.*/
public class ConnectJCABean implements SessionBean {
  SessionContext sessionContext;
  private String JNDI_NAME = "eis/jcaconn";

  public void ejbCreate() throws CreateException {
    /**@todo Complete this method*/
  }
  public void ejbRemove() {
    /**@todo Complete this method*/
  }
  public void ejbActivate() {
    /**@todo Complete this method*/
  }
  public void ejbPassivate() {
    /**@todo Complete this method*/
  }
  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }
  /**getJCA(String,String) takes 2 input parameters for the airline and the description
   * of the airline and returns a Vector containing*/
  public java.util.Vector getJCA(String name, String founder) throws java.rmi.RemoteException{
        /**Hashtable for returning data*/
            Vector browsedResult = new Vector();
    //This is the connection variable, the same type as returned from our JCA Connector
    Connection con = null;
        try
        {
    //we look up our jca entry in the with the JNDI lookup
    //this name of course matches the name in the weblogic-ra.xml
    //file for the jndi-entry of the resource adapter.
            InitialContext ctx = new InitialContext();
            DataSource dataSource = (DataSource)ctx.lookup(JNDI_NAME);
            con = dataSource.getConnection();
        }catch(Exception e){
          System.out.println("lookup failed");
          e.printStackTrace();
        }

        try
        {
    //Now lets input the entries given from the web site and commit them
    //In a more advanced application, this is where you could use JTA/XA or
    //JMS code to start transactions from multiple databases.
            if(con == null)
                throw new Exception();
            insertData(con,name, founder);
            con.commit();
            String browseQuery = "select NAME, FOUNDER, STARTDATE from EXAMPLES.BANDS";
            Statement pstmt = con.createStatement();
            ResultSet rs = pstmt.executeQuery(browseQuery);

            int i = 0;
            while(rs.next())
            {
             browsedResult.add(new String[]{rs.getString(1),rs.getString(2),rs.getString(3)});

            }
            rs.close();
            pstmt.close();
            con.close();
        }
        catch(SQLException sqe)
        {
            System.out.println("SQL Exception while getting BrowsedResults");
            sqe.printStackTrace();
        }
        catch(Exception e)
        {
            System.out.println("Could Not obtain browsed results " + e.getLocalizedMessage());
        }
        browsedResult.trimToSize();
        return browsedResult;
  }

  private void insertData(Connection con, String name, String founder){

    System.out.println("Name    = " + name);
    System.out.println("Founder = " + founder);
    java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd");
            ;
String insert = "Insert into EXAMPLES.BANDS (NAME, FOUNDER, STARTDATE) values ('" + name + "','" + founder + "', '" + f.format(new java.util.Date()) + "')";
    try{
      Statement pstmt = con.createStatement();
      pstmt.executeUpdate(insert);
      }catch(SQLException e){
      e.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

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