📄 studenttestclient1.java
字号:
package cmpsample;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;public class StudentTestClient1 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 StudentRemoteHome studentRemoteHome = null; private StudentRemote studentRemote = null; //Construct the EJB test client public StudentTestClient1() { 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("StudentRemote"); //look up jndi name and cast to Home interface studentRemoteHome = (StudentRemoteHome) PortableRemoteObject.narrow(ref, StudentRemoteHome.class); 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://zhaoqiang: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 StudentRemote create(Integer stuid) { long startTime = 0; if (logging) { log("Calling create(" + stuid + ")"); startTime = System.currentTimeMillis(); } try { studentRemote = studentRemoteHome.create(stuid); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: create(" + stuid + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: create(" + stuid + ")"); } e.printStackTrace(); } if (logging) { log("Return value from create(" + stuid + "): " + studentRemote + "."); } return studentRemote; } public StudentRemote findByPrimaryKey(Integer stuid) { long startTime = 0; if (logging) { log("Calling findByPrimaryKey(" + stuid + ")"); startTime = System.currentTimeMillis(); } try { studentRemote = studentRemoteHome.findByPrimaryKey(stuid); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: findByPrimaryKey(" + stuid + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: findByPrimaryKey(" + stuid + ")"); } e.printStackTrace(); } if (logging) { log("Return value from findByPrimaryKey(" + stuid + "): " + studentRemote + "."); } return studentRemote; } //---------------------------------------------------------------------------- // Methods that use Remote interface methods to access data through the bean //---------------------------------------------------------------------------- public Integer getStuid() { Integer returnValue = null; if (studentRemote == null) { System.out.println("Error in getStuid(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getStuid()"); startTime = System.currentTimeMillis(); } try { returnValue = studentRemote.getStuid(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getStuid()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getStuid()"); } e.printStackTrace(); } if (logging) { log("Return value from getStuid(): " + returnValue + "."); } return returnValue; } public void setStuname(String stuname) { if (studentRemote == null) { System.out.println("Error in setStuname(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling setStuname(" + stuname + ")"); startTime = System.currentTimeMillis(); } try { studentRemote.setStuname(stuname); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: setStuname(" + stuname + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: setStuname(" + stuname + ")"); } e.printStackTrace(); } } public String getStuname() { String returnValue = ""; if (studentRemote == null) { System.out.println("Error in getStuname(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getStuname()"); startTime = System.currentTimeMillis(); } try { returnValue = studentRemote.getStuname(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getStuname()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getStuname()"); } e.printStackTrace(); } if (logging) { log("Return value from getStuname(): " + returnValue + "."); } return returnValue; } public void setLockerid(Integer lockerid) { if (studentRemote == null) { System.out.println("Error in setLockerid(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling setLockerid(" + lockerid + ")"); startTime = System.currentTimeMillis(); } try { studentRemote.setLockerid(lockerid); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: setLockerid(" + lockerid + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: setLockerid(" + lockerid + ")"); } e.printStackTrace(); } } public Integer getLockerid() { Integer returnValue = null; if (studentRemote == null) { System.out.println("Error in getLockerid(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getLockerid()"); startTime = System.currentTimeMillis(); } try { returnValue = studentRemote.getLockerid(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getLockerid()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getLockerid()"); } e.printStackTrace(); } if (logging) { log("Return value from getLockerid(): " + returnValue + "."); } return returnValue; } public void executeRemoteCallsWithDefaultArguments() { if (studentRemote == null) { System.out.println("Error in executeRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE); return ; } getStuid(); setStuname(""); getStuname(); setLockerid(null); getLockerid(); } //---------------------------------------------------------------------------- // 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) { StudentTestClient1 client = new StudentTestClient1(); client.findByPrimaryKey(new Integer(1)); System.out.println("stuid="+client.getStuid()); System.out.println("stuname="+client.getStuname()); System.out.println("lockerid="+client.getLockerid()); //client.create(new Integer(4));(调用该语句可以新建Student实例) }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -