package productapplication;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.lang.Integer;
import java.lang.String;
import java.util.Properties;
import java.util.Collection;
import java.io.*;
import javax.ejb.*;
public class StudentDetailTestClient1 {
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 StudentDetailRemote studentDetailRemote = null;
private StudentDetailRemoteHome studentDetailRemoteHome = null;
//Construct the EJB test client
public StudentDetailTestClient1() {
initialize();
}
public void initialize() {
try {
//get naming context
Context context = getInitialContext();
//look up jndi name
Object ref = context.lookup("StudentDetailRemote");
//look up jndi name and cast to Home interface
studentDetailRemoteHome = (StudentDetailRemoteHome)
PortableRemoteObject.narrow(ref,
StudentDetailRemoteHome.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public Context getInitialContext() throws Exception {
String url = "t3://zerg1:7001";
String user = null;
String password = null;
Properties properties;
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 javax.naming.InitialContext(properties);
} catch (Exception e) {
System.out.println("Unable to connect to WebLogic server at " + url);
System.out.println("Please make sure that the server is running.");
throw e;
}
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public StudentDetailRemote create(Integer studentId) {
try {
studentDetailRemote = studentDetailRemoteHome.create(studentId);
} catch (Exception e) {
e.printStackTrace();
}
return studentDetailRemote;
}
public StudentDetailRemote findByPrimaryKey(Integer studentId) {
try {
studentDetailRemote = studentDetailRemoteHome.findByPrimaryKey(
studentId);
} catch (Exception e) {
e.printStackTrace();
}
return studentDetailRemote;
}
public Collection findAllStudents() {
Collection returnValue = null;
try {
returnValue = studentDetailRemoteHome.findAllStudents();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public Integer getStudentId() {
Integer returnValue = null;
if (studentDetailRemote == null) {
System.out.println("Error in getStudentId(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = studentDetailRemote.getStudentId();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public void setStudentName(String studentName) {
if (studentDetailRemote == null) {
System.out.println("Error in setStudentName(): " +
ERROR_NULL_REMOTE);
return;
}
try {
studentDetailRemote.setStudentName(studentName);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getStudentName() {
String returnValue = "";
if (studentDetailRemote == null) {
System.out.println("Error in getStudentName(): " +
ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = studentDetailRemote.getStudentName();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public void setAddress(String address) {
if (studentDetailRemote == null) {
System.out.println("Error in setAddress(): " + ERROR_NULL_REMOTE);
return;
}
try {
studentDetailRemote.setAddress(address);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getAddress() {
String returnValue = "";
if (studentDetailRemote == null) {
System.out.println("Error in getAddress(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = studentDetailRemote.getAddress();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public void setCourse(String course) {
if (studentDetailRemote == null) {
System.out.println("Error in setCourse(): " + ERROR_NULL_REMOTE);
return;
}
try {
studentDetailRemote.setCourse(course);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getCourse() {
String returnValue = "";
if (studentDetailRemote == null) {
System.out.println("Error in getCourse(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = studentDetailRemote.getCourse();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
public StudentDetailRemoteHome getHome() {
return studentDetailRemoteHome;
}
//Main method
private static String fomart(String str) {
try {
byte[] bytes = str.getBytes("ISO8859_1");
return new String(bytes);
} catch (UnsupportedEncodingException ex) {
return "";
}
}
public static void main(String[] args) {
StudentDetailTestClient1 client = new StudentDetailTestClient1();
StudentDetailRemoteHome home = client.getHome();
StudentDetailRemote remote = null;
char choice;
int input;
try {
while (true) {
System.out.println("1。搜索 2。显示所有记录 3。退出");
System.out.println("请选择功能号: ");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
choice = (char) br.read();
switch (choice) {
case '1':
try {
System.out.println("输入学生 ID: ");
BufferedReader br1 = new BufferedReader(new
InputStreamReader(
System.in));
input = Integer.parseInt(br1.readLine());
remote = home.findByPrimaryKey(new Integer(input));
System.out.println("/n学生的详细信息: ");
System.out.println("学生 ID : " + remote.getStudentId());
System.out.println("学生姓名: " +
remote.getStudentName());
System.out.println("住址: " +
remote.getAddress());
System.out.println("课程: " + remote.getCourse());
} catch (ObjectNotFoundException ex) {
System.out.println("无此学生");
System.out.println();
}
break
;
case '2':
java.util.Iterator display = home.findAllStudents().
iterator();
System.out.println("学生ID 学生姓名 住址 课程");
System.out.println("====================================");
while (display.hasNext()) {
StudentDetailRemote records = (StudentDetailRemote)
display.
next();
System.out.print(records.getStudentId());
System.out.print(" " +
records.getStudentName());
System.out.print(" " + records.getAddress());
System.out.print(" " + records.getCourse());
System.out.println();
}
System.out.println("-------------------------------------");
System.out.println();
break;
case '3':
System.out.println("应用程序已经终止");
System.exit(0);
break;
default:
System.out.println("非法选择");
}
}
} catch (Exception error) {
System.out.println(error);
}
}
}
这里不用修改URL,很奇怪为什么仍然可以运行...
请教高手答复~~~