第一次寫文章,希望大家多多捧場,本文完全根據我的個人經驗所寫,如有錯誤,懇請大家指正!
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 aclearcase/" target="_blank" >ccess 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運行。
原文轉自:http://www.anti-gravitydesign.com