Bai Thuc Hanh Lap Trinh Phan Tan

  • Published on
    08-Apr-2015

  • View
    851

  • Download
    4

Embed Size (px)

Transcript

- 1-

MC LCBi 1 LP TRNH PHN TN I TNG JAVA RMI................... 21.1. Chng trnh RMI Hello ............................................................................. 2 1.1.1. Ci t lp giao din HelloInterface.java ........................................... 2 1.1.2. Ci t lp hin thc HelloImpl.java.................................................. 2 1.1.3. Ci t chng trnh Server : HelloServer.java.................................. 2 1.1.4. Ci t chng trnh Client : HelloClient .java .................................. 3 1.1.5. Bin dch chng trnh ....................................................................... 3 1.2. Chng trnh RMI truy xut c s d liu t xa ......................................... 5 1.2.1. Xy dng c s d liu....................................................................... 5 1.2.2. Xy dng lp DBServerInterface.java................................................ 5 1.2.3. Xy dng lp DBServerImpl.java ...................................................... 5 1.2.4. Xy dng lp DBServer.java.............................................................. 6 1.2.5. Xy dng lp DBClient.java .............................................................. 7 1.3. Chng trnh minh ha k thut gi ngc (callback) trn RMI................ 7 1.3.1. Xy dng lp AtServerInterface.java ................................................. 7 1.3.2. Xy dng lp AtServerImpl.java........................................................ 7 1.3.3. Xy dng chng trnh AtClientInterface.java .................................. 8 1.3.4. Xy dng chng trnh AtClientImpl.java ......................................... 8 1.3.5. Xy dng chng trnh Server.java ................................................... 8 1.3.6. Chng trnh Client.java..................................................................... 8

Bi 2

LP TRNH PHN TN I TNG CORBA ...................... 11

2.1. Chng trnh CORBA Hello ..................................................................... 11 2.1.1. Bc 1 : c t i tng bng ngn ng CORBA IDL ................. 11 2.1.2. Bc 2 : Xy dng i tng Hello bng ngn ng Java ................ 11 2.1.3. Bc 3 : Ci t i tng CORBA Hello ....................................... 11 2.1.4. Bc 4 : Xy dng chng trnh Server........................................... 11 2.1.5. Bc 5 : Xy dng chng trnh Client ........................................... 12 2.1.6. Chy chng trnh ............................................................................ 13

B mn Mng, Khoa CNTT - 2005

- 2-

Bi 1 1.1.

LP TRNH PHN TN I TNG JAVA RMIChng trnh RMI Hello

- Xy dng chng trnh JavaRMI, client gi yu cu add(x,y), sub(x,y) n Server. Server thc hin v tr kt qu v cho client. 1.1.1.*/ import java.rmi.*; public interface HelloInterface extends java.rmi.Remote { public String sayHello() throws java.rmi.RemoteException; public int addItem(int x, int y) throws java.rmi.RemoteException; public int subItem(int x, int y) throws java.rmi.RemoteException; }//class

Ci t lp giao din HelloInterface.java

/* Buoc 1 : Dac ta giao tiep cho lop doi tuong o xa

1.1.2.

Ci t lp hin thc HelloImpl.java

- Ci t chi tit cc phng thc c m t trong lp giao din./* Buoc 2 : Cai dat chi tiet phuong thuc tu xa */ import java.rmi.*; public class HelloImpl implements HelloInterface { public String sayHello() throws java.rmi.RemoteException { System.out.println("Client da su dung phuong thuc 1 cua Server"); return "Hello World"; }//sayHello() public int addItem(int x, int y) throws java.rmi.RemoteException { return (x+y); }//addItem() public int subItem(int x, int y) throws java.rmi.RemoteException { return (x-y); }//subItem() }//class

1.1.3.*/

Ci t chng trnh Server : HelloServer.java

/* Buoc 3 : Khoi tao doi tuong giao tiep tren may chu import java.rmi.*; import java.rmi.server.*; public class HelloServer { public static void main (String args[]) throws java.lang.Exception { //Kien tao doi tuong try { //Kien tao doi tuong HelloImpl obj = new HelloImpl();

B mn Mng, Khoa CNTT - 2005

- 3//Khai bao doi tuong voi may ao JVM java.rmi.server.UnicastRemoteObject.exportObject(obj); System.out.println("* Dang ky doi tuong"); java.rmi.Naming.bind("rmi://127.0.0.1/HelloImpl",obj); System.out.println("* Dang ky doi tuong cho phep truy xuat tu xa : HelloImpl"); System.out.println("* Cho Client truy xuat . . ."); }catch (java.lang.Exception ex){System.out.println(ex);} }//main }//class

Ch : c th gp chng trnh HelloRMIServer.java v HelloImplement.java vo 1 chng trnh nhng phc tp. 1.1.4.*/ import java.rmi.*; public class HelloClient { public static void main (String[] args) { int x = 5; int y = 3; try { //Tim tham chieu cua doi tuong System.out.println("Dang tim doi tuong ..."); HelloInterface obj=(HelloInterface)java.rmi.Naming.lookup("rmi://"+ args[0] +"/HelloImpl"); //Trieu goi cac phuong thuc xu ly cua doi tuong tu xa System.out.println(obj.sayHello()); System.out.println("* Tong = " + obj.addItem(x,y)); System.out.println("* Hieu = " + obj.subItem(x,y)); } catch (java.lang.Exception ex){System.out.println(ex); } } }

Ci t chng trnh Client : HelloClient .java

/* Buoc 4 : Chuong trinh goi doi tuong tu xa

1.1.5.ECHO OFF Echo Echo Echo

Bin dch chng trnh

Tp tin Compiler---------------------------------------------------CHUONG TRINH COMPILE ----------------------------------------------------

Path=C:\jdk1.4\bin Echo -----------Dich cac tap tin *.java --------del *.class javac *.java Echo -----Tao tap tin trung gian _Stub va _Skel cho doi tuong --------rmic HelloImplement Pause

B mn Mng, Khoa CNTT - 2005

- 4-

Tp tin Client.batECHO OFF Echo Echo Echo ---------------------------------------------------CHUONG TRINH RMI CLIENT ----------------------------------------------------

Path=C:\jdk1.4\bin set CLASSPATH=.;%CLASSPATH%; java HelloRMIClient 127.0.0.1 Pause

Tp tin Server.batECHO OFF Echo Echo Echo ---------------------------------------------------CHUONG TRINH RMI SERVER ----------------------------------------------------

Path=C:\jdk1.4\bin set CLASSPATH=.;%CLASSPATH%; Echo -----------Chay chuong trinh RMIRegistry ----------rmiregistry Echo -----------Chay chuong trinh Server dang ky doi tuong ----------java HelloRMIServer 127.0.0.1 Pause

T chc cc chng trnhCLIENT HelloRMIClient.class HelloInterface.class HelloImplement_STUB.class SERVER HelloRMIServer.class HelloImplement_SKEL.class

Th t chy chng trnh : 1. Compiler.bat 2. Serber.bat 3. Client.bat

B mn Mng, Khoa CNTT - 2005

- 5-

1.2.1.2.1.

Chng trnh RMI truy xut c s d liu t xa Xy dng c s d liu

1.2.2.

Xy dng lp DBServerInterface.java

import java.rmi.*; public interface DBServerInterface extends Remote { public String queryDataBase() throws RemoteException; }

1.2.3.

Xy dng lp DBServerImpl.java

import java.sql.*; import java.rmi.*; import java.io.*; public class DBServerImpl implements DBServerInterface { public DBServerImpl() { } //---------------------------------------------------public String queryDataBase() throws RemoteException { return accessDB(); } //---------------------------------------------------public String accessDB() { String stResult=" ";

B mn Mng, Khoa CNTT - 2005

- 6try { //init database Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection = DriverManager.getConnection("jdbc:odbc:TAIKHOAN"); Statement statement= connection.createStatement(); boolean hasResult = statement.execute("SELECT * FROM USER"); if (hasResult) { ResultSet rs= statement.getResultSet(); if (rs != null) stResult= formatResult(rs); } else System.out.println("Khong tim duoc CSDL"); connection.close(); } catch(Exception ex) { System.out.println(ex.toString()); } return stResult; } //-----------------------------------------------------------public String formatResult(ResultSet r) throws SQLException { ResultSetMetaData rmeta= r.getMetaData(); int numColumns = rmeta.getColumnCount(); String text=" "; for(int i=1;i