📄 contacttestclient1.java
字号:
package cmp1bean;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;public class ContactTestClient1 extends Object { private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first."; private static final int MAX_OUTPUT_LINE_LENGTH = 100; private boolean logging = true; private ContactHome contactHome = null; private Contact contact = null; //Construct the EJB test client public ContactTestClient1() { initialize(); } public void initialize() { long startTime = 0; if (logging) { log("Initializing bean access."); startTime = System.currentTimeMillis(); } try { //get naming context Context context = getInitialContext(); //look up jndi name Object ref = context.lookup("Contact"); //look up jndi name and cast to Home interface contactHome = (ContactHome) PortableRemoteObject.narrow(ref, ContactHome.class); System.out.println("find a record whose key is:"+"Hongbin"); contact=contactHome.findByPrimaryKey("Zhang"); System.out.println("getFirst(): "+contact.getFirst()+ " getLast(): "+contact.getLast()+ " getEmail(): "+contact.getEmail()); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded initializing local bean access through Local Home interface."); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed initializing bean access."); } e.printStackTrace(); } } private Context getInitialContext() throws Exception { String url = "t3://localhost:7001"; String user = null; String password = null; Properties properties = null; try { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, url); if (user != null) { properties.put(Context.SECURITY_PRINCIPAL, user); properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password); } return new InitialContext(properties); } catch(Exception e) { log("Unable to connect to WebLogic server at " + url); log("Please make sure that the server is running."); throw e; } } //---------------------------------------------------------------------------- // Methods that use Home interface methods to generate a Remote interface reference //---------------------------------------------------------------------------- public Contact create(String first, String last, String email) { long startTime = 0; if (logging) { log("Calling create(" + first + ", " + last + ", " + email + ")"); startTime = System.currentTimeMillis(); } try { contact = contactHome.create(first, last, email); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: create(" + first + ", " + last + ", " + email + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: create(" + first + ", " + last + ", " + email + ")"); } e.printStackTrace(); } if (logging) { log("Return value from create(" + first + ", " + last + ", " + email + "): " + contact + "."); } return contact; } public Contact create(String last) { long startTime = 0; if (logging) { log("Calling create(" + last + ")"); startTime = System.currentTimeMillis(); } try { contact = contactHome.create(last); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: create(" + last + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: create(" + last + ")"); } e.printStackTrace(); } if (logging) { log("Return value from create(" + last + "): " + contact + "."); } return contact; } public Contact findByPrimaryKey(String primaryKey) { long startTime = 0; if (logging) { log("Calling findByPrimaryKey(" + primaryKey + ")"); startTime = System.currentTimeMillis(); } try { contact = contactHome.findByPrimaryKey(primaryKey); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: findByPrimaryKey(" + primaryKey + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: findByPrimaryKey(" + primaryKey + ")"); } e.printStackTrace(); } if (logging) { log("Return value from findByPrimaryKey(" + primaryKey + "): " + contact + "."); } return contact; } //---------------------------------------------------------------------------- // Methods that use Remote interface methods to access data through the bean //---------------------------------------------------------------------------- public String getFirst() { String returnValue = ""; if (contact == null) { System.out.println("Error in getFirst(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getFirst()"); startTime = System.currentTimeMillis(); } try { returnValue = contact.getFirst(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getFirst()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getFirst()"); } e.printStackTrace(); } if (logging) { log("Return value from getFirst(): " + returnValue + "."); } return returnValue; } public void setFirst(String first) { if (contact == null) { System.out.println("Error in setFirst(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling setFirst(" + first + ")"); startTime = System.currentTimeMillis(); } try { contact.setFirst(first); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: setFirst(" + first + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: setFirst(" + first + ")"); } e.printStackTrace(); } } public String getLast() { String returnValue = ""; if (contact == null) { System.out.println("Error in getLast(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getLast()"); startTime = System.currentTimeMillis(); } try { returnValue = contact.getLast(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getLast()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getLast()"); } e.printStackTrace(); } if (logging) { log("Return value from getLast(): " + returnValue + "."); } return returnValue; } public String getEmail() { String returnValue = ""; if (contact == null) { System.out.println("Error in getEmail(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getEmail()"); startTime = System.currentTimeMillis(); } try { returnValue = contact.getEmail(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getEmail()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getEmail()"); } e.printStackTrace(); } if (logging) { log("Return value from getEmail(): " + returnValue + "."); } return returnValue; } public void setEmail(String email) { if (contact == null) { System.out.println("Error in setEmail(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling setEmail(" + email + ")"); startTime = System.currentTimeMillis(); } try { contact.setEmail(email); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: setEmail(" + email + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: setEmail(" + email + ")"); } e.printStackTrace(); } } //---------------------------------------------------------------------------- // Utility Methods //---------------------------------------------------------------------------- private void log(String message) { if (message == null) { System.out.println("-- null"); return ; } if (message.length() > MAX_OUTPUT_LINE_LENGTH) { System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ..."); } else { System.out.println("-- " + message); } } //Main method public static void main(String[] args) { ContactTestClient1 client = new ContactTestClient1(); // 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. }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -