你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Linux开发
实战JBuilder7+WebLogic7存取SQL Server2000(2)
 

 catch (Exception ex)
  
  {
  
  jTextArea1.setText("error : "+ex.getMessage());
  
  }
  
  双击第二个按钮输入以下代码
  
  Hashtable ht = new Hashtable();
  
  ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
  
  ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
  
  
  
  try
  
  {
  
  Context ctx = new InitialContext(ht);
  
  DataSource ds = (DataSource)ctx.lookup("SQLServer");
  
  Connection con = ds.getConnection("system","12345678");//此处是WebLogic7
  
  的域用户和密码
  
  Statement st = con.createStatement();
  
  ResultSet res = st.executeQuery("select * from employees");
  
  String line = "";
  
  while (res.next())
  
  line = line + res.getString("notes")+"";
  
  jTextArea1.setText(line);
  
  con.close();
  
  }
  
  catch (Exception ex)
  
  {
  
  jTextArea1.setText("error : "+ex.getMessage());
  
  }
  
  运行WebLogic7,运行程序单击第一个按钮使用JDBC直接连接SQLServer并获取数据,单击第二个按钮使用DataSource连接SQLServer并获取数据。
  
  实战2:Session Bean
  建立一个简单的Bean:
  
  1. 关闭所有工程:File->Close Projects
  
  2. 选择File->New project
  
  在Name栏中输入HelloDemo,Directory栏中输入存放路径(不要有空格),其他不变,单击Finish。
  
  3. 选择File->New->Enterprise->EJB 2.0 Designer单击OK。
  
  在弹出的对话框中单击new建立一个Moudle,在Name中输入HelloMoudle单击OK关闭当前对话框,再次单击OK关闭对话框。
  
  4. 在右侧的工作区中单击右键选择:Create EJB->Session Bean,将Bean Name改为HelloBean
  
  5. 右键单击代表HelloBean的长方形,选择Add->Method
  
  按如下填写:
  
  Method Name = SayHello
  
  Return Type = java.lang.String
  
  Input parameter 不添
  
  Interface = remote
  
  6. 右键单击代表HelloBean的长方形,选择 View Bean Source
  
  按如下填写SayHello():
  
  public java.lang.String SayHello()
  
  {
  
  /**@todo Complete this method*/
  
  return new String(“Hello World “);
  
  }
  
  7.按F9运行,在弹出的对话框中选择Run页,单击New,在configure name处填写Server Runtime Configuration,再选择Run->Server,单击OK关闭当前对话框,单击OK开始编译运行。运行起来之后在左上角的目录树中右键单击HelloModule选择:Deploy options for “HelloModule.jar”->Deploy来发布Bean。
  
  建立客户端:
  
  1. 选择File->New->Enterprise->EJB Test Client单击OK。
  
  选中Genrate method for 特斯廷remote interface calls with default arguments单击OK。
  
  2. 按如下填写main():
  
  public static void main(String[] args)
  
  {
  
  HelloBeanTestClient1 client = new HelloBeanTestClient1();
  
  // Use the client object to call one of the Home interface wrappers
  
  // above, to create a Remote interface reference to the bean.
  
  // If the return value is of the Remote interface type, you can use it
  
  // to access the remote interface methods. You can also just use the
  
  // client object to call the Remote interface wrappers.
  
  client.create();
  
  System.out.println(client.SayHello());
  
  } 选择Run->Run “HelloBeanTestClient1.java” using defaults运行。

(编辑:aniston)

  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089