服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

实战jbuilder7+weblogic7存取sql server2000


  第一次写文章,希望大家多多捧场,本文完全根据我的个人经验所写,如有错误,恳请大家指正!
  
  jbuilder7+weblogic7 的配置
  假设jbuilder7和weblogic7安装完毕,操作系统为:windows2000 server(sp2),数据库为: sqlserver2000(sp2)。
  
  
  
  jbuilder7的配置:
  
  1. 修改环境变量temp和tmp 为不带空格的目录如:e:/winnt/temp
  
  2. 启动jbuilder7,选择:tools->configure servers
  
  3. 选中左侧的weblogic application server 6.x+,选中右边的enable server
  
  4. 将general->home directory设为weblogic7的home directory如:e:/bea/weblogic700/server,正常的话jbuilder7将自动为你添好其他的项。
  
  5. 将custom->jdk installation directory设为 jdk的安装目录,如:e:/bea/jdk131_02
  
  6. 将custom->bea home directory设为weblogic7的home director,如:e:/bea
  
  7. 将custom->domain directory设为你的域目录,如:e:/bea/user_projects/mydomain
  
  8. 添好user name, password ,domain name, server name后,单击ok退出。
  
  9. 选择:tools->enterprise setup,单击corba页上的new, 按下表填写相应信息:
  
  name for this configuration = wellogic 7.0
  
  path for orb tools = e:/bea/weblogic700/server
  
  library for projects = weblogic 6.x+ deploy
  
  idl compiler command = idlj.exe
  
  commnad option for output directory = e:/corbaoutput(任意目录,不要有空格)
  
  单击ok退出。
  
  10.选择project->default project properties
  
  在path页的required libraries中将会看到weblogic 6.x+ client和weblogic 6.x+ deploy两项,如果没有,请检查以上步骤是否正确。
  
  11.选择server页,单击single services for all service in project
  
  在下拉列表中选择weblogic application server 6.x+,但击ok退出,配置完毕。
  
  
  
  weblogic7的配置:
  
  1. 启动weblogic7
  
  2. 打开ie6,在地址栏中输入:http://localhost:7001/console
  
  3. 输入用户名和密码
  
  4. 在左边的目录树中选中services->jdbc->connection pools,单击右侧的configure a new jdbc connection pool.,输入以下信息:
  
  configuration->general页:
  
  name = sql server connection pool
  
  url = jdbc:weblogic:mssqlserver4:northwind@localhost
  
  driver classname = weblogic.jdbc.mssqlserver4.driver
  
  properties : user = sa
  
  password = “” <- sa的密码
  
  单击create建立连接池。
  
  targets->server页:
  
  将myserver(服务器名称)移至右侧的列表中,但击单击apply
  
  5. 在左边的目录树中选中services->jdbc->data sources(或者txdata sources),单击右侧的configure a new jdbc connection pool.,输入以下信息:
  
  configuration->general页:
  
  name = sqlserver tx data source
  
  jndi name = sqlserver
  
  pool name = sql server connection pool
  
  选中emulate two-phase commit for non-xa driver和row prefetch enabled
  
  单击create建立数据源。
  
  targets->server页:
  
  将myserver(服务器名称)移至右侧的列表中,但击单击apply,配置完毕。
  
  实战1:连接sqlserver2000
  1. 打开jbuilder7选择file->new project
  
  在name栏中输入sqlserverdemo,directory栏中输入存放路径(不要有空格),其他不变,单击finish。
  
  2. 选择file->new,选择general->application,单击ok。
  
  第一步,第二步和第三步都不用更改,直接finish即可。
  
  3. 回到jbuilder7的集成开发环境中,单击右侧的designer页设计窗体,在窗体中放入一个jscrollpane 和jtextarea 及三个按钮,双击第一个按钮输入以下代码:
  
  try
  
  {
  
  class.forname("weblogic.jdbc.mssqlserver4.driver");
  
  connection con = drivermanager.getconnection("jdbc:weblogic:mssqlserver4:northwind@localhost","sa","");//此处根据你的sqlserver帐户而定。
  
  statement st = con.createstatement();
  
  resultset res = st.executequery("select * from employees");
  
  string line = "";
  
  while (res.next())
  
  line = line + res.getstring("title")+"/n";
  
  jtextarea1.settext(line);
  
  con.close();
  
  }
  
  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")+"/n";
  
  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运行。

扫描关注微信公众号