用CMP访问数据库,简单的,但不理解为什么URL不修改仍然可以运行

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,很奇怪为什么仍然可以运行...

请教高手答复~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值