Phương hướng tiếp theo

Một phần của tài liệu dữ liệu nguồn warehouese trong cơ sở thiết kế data (Trang 107)

Tiếp tục nâng cấp hoàn thiện chương trình đề mô. Thử nghiệm và đánh giá hiệu năng các phương pháp truy nhập cơ sở dữ liệu Web một cách kỹ lưỡng, tỉ mỉ và toàn diện hơn trên mạng không dây và mạng Internet.

Tài liệu tham khảo Tiếng Việt

[1]. Ban biên tập Trung tâm Tin học - Ngoại ngữ Trí Đức - Giáo trình lập trình mạng bằng Java, nhà xuất bản Thống Kê 2003.

[2]. Biện Văn Quang, Công nghệ tác tử trong quản lý mạng Internet_1&2

[3]. Nguyễn Phương Lan - Hoàng Đức Hải, Lập trình ứng dụng Web với JSP/Servlet, Nhà xuất bản Lao Động - Xã Hội

[4]. Nguyễn Đại Thọ - Trường Đại học Công Nghệ, Đại học Quốc gia Hà Nội,

Phát triển tác tử di động tìm kiếm tài liệu trực tuyến

[5]. Nguyễn Đại Thọ - Trường Đại học Công Nghệ, Đại học Quốc gia Hà Nội, Tác tử di động trong xử lý phân tán.

Tiếng Anh

[6]. Bruce Eckel, President, MindView Inc. ©1997 All Rights Reserved - Thinking in Java

[7]. Danny B.Lange - Mitsuru Oshima, Programming and Deploying JavaTM Mobile Agents With AgletsTM

[8]. David Reilly & Michael Reilly - JAVATM network programming and distributed computing, Addison Wesley 2002

[9]. Gerald Brose, Andreas Vogel, Keith Duddy, Java Programming with CORBA

- Third Edition

[10].Jason Brittain - lan F.Darwin, Tomcat : The Definitive Guide

[11].Jason Hunter with Whiliam Crawford, JavaTM Servlet Programming

[12]. JavaTM Remote Method Invocation Specification [13]. RMI (Remote Method Invocation)

[14].Luca Ferrari October 6, 2004 The Aglets 2.0.2 User's Manual

[15].Mark Wutka - Alan Moffet - Kunal Mittal, Sams Teach Yourself Java Server PagesTM 2.0 with Apache Tomcat in 24 Hours

[17].Sam Joseph, Masanori Hattori and Naoki Kase - Learning Improves Mobile Agent Efficiency

[18].Satoshi Hirano, Yoshiji Yasu, Hirotaka Igarashi, Performance Evaluation of Popular Distributed Object Technologies for Java

[19].Stavros Papastavrou, Panos Chrysanthis, George Samaras, Evaggelia Pitoura - Sun Microsystems, Distributed Programming With Java Technology

[20].Stavros Papastavrou, Student Member, IEEE, George Samaras, Senior Member, IEEE, and Evaggelia Pitoura, Member, IEEE, Mobile Agents for World Wide Web Distributed Database Access

[21].Stavros Papastavrou, Panos Chrysanthis, George Samaras, Evaggelia Pitoura,

An Evaluation of the Java-based Approaches to Web Database Access

[22]. Todd M. Thomas, JavaTM Data Access JDBC, JNDI, and JAXP

[23].Giovanni Rimassa (FRAMeTech s.r.l.), Roland Mungenast (PROFACTOR GmbH), Fabio Bellifemine, Giovanni Caire, Tiziana Trucco (TILAB S.p.A.,

formerly CSELT), JADE ADMINISTRATOR’S GUIDE, 10-November-2006

[24].Giovanni Caire (TILAB, formerly CSELT), JADE TUTORIAL JADE

PROGRAMMING FOR BEGINNERS, 04 December 2003

[25].Fabio Bellifemine, Giovanni Caire, Tiziana Trucco (TILAB, formerly

CSELT) Giovanni Rimassa (University of Parma), JADE PROGRAMMER’S

GUIDE, 21-August-2006

[26]. Stavros Papastavrou, Student Member, IEEE, George Samaras, Senior Member, IEEE, and Evaggelia Pitoura, Member, IEEE, Mobile Agent for World Wide Web Distributed Database Access [27].http://aglets.sourceforge.net/ [28].http://www.trl.ibm.com/aglets/ [29].http://java.sun.com [30].http://jade.cselt.it [31].http://jade.tilab.com

Phụ lục 1 : Các cổng giao tiếp

Định nghĩa: Cổng (port) là một cơ chế cho phép dữ liệu được truyền đến đúng ứng dụng đang chạy trên máy tính.

Chúng ta biết rằng mỗi máy tính thường chỉ có một kết nối vật lý với mạng, tất cả các dữ liệu cần truyền cho máy tính khác sẽ được mang đi bởi kết nối này. Tuy nhiên chúng ta cũng lại thấy rằng trên mỗi máy tính không chỉ có một mà có thể có nhiều ứng dụng (chương trình) cùng chạy, và cùng có nhu cầu giao tiếp qua mạng. Do đó dữ liệu máy tính nhận được thông qua kết nối với mạng cần phải được chuyển đến đúng ứng dụng cần nó, vì vậy máy tính cần phải biết dữ liệu mà nó nhận được, được phía gửi hy vọng chuyển đến ứng dụng nào. Vấn đề này được thực hiện thông qua việc sử dụng các cổng:

• Mỗi ứng dụng chạy trên máy tính sẽ được gán cho một số hiệu cổng logic, đó là một con số tự nhiên được biểu diễn bằng 16 bits. Các cổng có giá trị từ 0 đến 65535 (0  216-1). Trong đó các cổng từ 0 tới 1023 bị hạn chế, chúng chủ yếu được sử dụng cho các dịch vụ phổ biến như HTTP, FTP… , các cổng còn lại được sử dụng cho các ứng dụng của người dùng.

• Dữ liệu truyền qua mạng Internet chứa trong nó thông tin để xác định định danh và cổng trên máy tính mà nó cần đến. Định danh của mỗi máy tính được xác định bằng một địa chỉ IP được biểu diễn bằng 32 bit.

Việc gán một số hiệu cổng cho một ứng dụng có thể được thực hiện bởi chương trình hoặc bởi hệ điều hành tùy theo mục đích sử dụng của ứng dụng.

Phụ lục 2 : Một số giao diện chính của chương trình

1. Chương trình sử dụng một tác tử truy nhập một cơ sở dữ liệu Web

* Môi trường thực thi JADE sau khi khởi động

* Tác tử di động được tạo ra trong Container-1

* Tác tử di động di chuyển tử từ Container-1 ban đầu sang Container-2 để truy nhập cơ sở dữ liệu trên Container-2

* Tác tử di động di chuyển từ Container-2 về Container-3 để trả dữ liệu kết quả cho client

2. Chương trình sử dụng 2 tác tử truy nhập cơ sở dữ liệu Web

* Tác tử AppletAgent được tạo ra trong Container-4

* Tác tử di động MobileAgent di chuyển đến Container-2 để lấy dữ liệu, trong khi tác tử di động MbileAgent1 di chuyển đến Container-3 để lấy dữ liệu.

* Tác tử di động MobileAgent và MobileAgent1 sau khi lấy dữ liệu xong, di chuyển đến Container-4 trả kết quả cho client.

3. Chương trình sử dụng một tác tử di động truy nhập nhiều cơ sở dữ liệu Web

* Môi trường thực thi JADE sau khi khởi động xong

* Tác tử di động MobileAgent được tạo ra trong Container-1

* Tác tử di động MobileAgent di chuyển đến Container-3 để lấy dữ liệu

Phụ lục 3 : Một số đoạn mã nguồn chương trình

1. Truy nhập cơ sở dữ liệu web bằng phương pháp Java Socket

* Mã nguồn phía Server

import java.net.* ; import java.io.* ; import java.sql.* ;

public class SocketServer {

static int PORT = 9999 ;

static String _driver = "sun.jdbc.odbc.JdbcOdbcDriver" ; static String _url = "jdbc:odbc:SinhVien" ;

static Connection con = null; static Statement stmt = null ; static ResultSet r = null;

//--- public static void main(String agrs[])

{ try { Class.forName(_driver) ; con = DriverManager.getConnection(_url,"","") ; } catch(java.lang.ClassNotFoundException e) {

System.out.println("Khong Tim Thay Lop Driver") ; System.exit(1) ;

}

catch(java.sql.SQLException e) {

System.out.println("Khong Mo Duoc Ket Noi Toi CSDL") ; System.exit(1) ;

} try {

ServerSocket server = new ServerSocket(PORT) ; int localPort = server.getLocalPort() ;

System.out.println("Socket Server is listening on Port: "+localPort+".") ;

/* Khi Gap Lenh Nay Chuong Trinh Se DUNG LAI * Cho Den Khi Co Yeu Cau Tu Client Chuyen Toi * Thi Chuong Trinh Moi Chay Tiep

*/

while(true) {

Socket client = server.accept() ; // Tao Socket Tra Loi Client

String destName =

client.getInetAddress().getHostAddress() ;

System.out.println("Accepted Connection to " + destName + " on Port" + destPort + ".") ; BufferedReader inStream =

new BufferedReader(

new InputStreamReader(client.getInputStream())) ; // Tao Ra Doi Tuong Viet Du Lieu Len socket_server PrintWriter outStream =

new PrintWriter( new BufferedWriter( new

OutputStreamWriter(client.getOutputStream())),true) ; boolean finished = false ;

do {

String inLine = inStream.readLine() ; System.out.println("Received : "+inLine) ; if(inLine.equalsIgnoreCase("END"))

{

finished = true ; break ; }

try {

stmt = con.createStatement() ; r = stmt.executeQuery(inLine) ; // Doc Cau Truc Cua ResultSet

ResultSetMetaData metaData = r.getMetaData() ;

int columnCount = metaData.getColumnCount() ;

String outLine = new String("") ; // Lay Tieu De Cot

for(int i = 1 ; i <= columnCount ; i++) outLine = new

String(outLine+metaData.getColumnLabel(i)+"\t") ;

outLine = new String(outLine+"\n") ; // Lay Gia Tri Cac Truong Du Lieu

while(r.next()) {

for(int i = 1 ; i <= columnCount ; i++) outLine = new

String(outLine+r.getString(i)+"\t") ;

outLine = new String(outLine+"\n") ;

}

outStream.println(outLine) ;

outStream.println("END") ; // Bao Hieu Cho Client Het Du Lieu

System.out.println("Kich Thuoc Khoi Du Lieu : "+outLine.length()) ; System.out.println(outLine) ; } catch(java.sql.SQLException e) { System.err.println("Cannot create SQL statement") ; } } while(!finished) ; client.close() ; } } catch(IOException e) { System.out.println(e) ; } } }

* Mã nguồn phía Clients

import java.applet.Applet ; import java.awt.* ; import javax.swing.* ; import java.net.* ; import java.io.* ; import java.util.Date ;

public class AppletXemDiem extends Applet {

JPanel panel = null ;

JTextArea textArea = null ; Socket connection = null; PrintWriter out = null; BufferedReader in = null;

//--- public void init()

{

panel = new JPanel() ;

panel.setBackground(Color.white) ; textArea = new JTextArea(100, 0) ; textArea.setEditable(false) ; textArea.setText("") ;

setLayout(new BorderLayout()) ; panel.add("Center",textArea) ; add("Center",panel) ;

try {

// Thoi Diem Bat Dau Tinh Thoi Gian Date now = new Date() ;

long start = now.getTime() ; // Tao Ket Noi Toi Socket_Server

connection = new Socket("127.0.0.1",SocketServer.PORT) ; // Tao Ra Doi Tuong Doc Vung Dem Cua socket_client

in = new BufferedReader( new

InputStreamReader(connection.getInputStream())) ;

// Tao Ra Doi Tuong Viet Du Lieu Len socket_client out = new PrintWriter(

new BufferedWriter(

new OutputStreamWriter(

connection.getOutputStream())),true) ; String query = new String("Select * From DIEM") ;

out.println(query) ;

// Nhan Du Lieu Gui Tu Socket_Server try

{

while(true) {

String st = new String("") ; st = in.readLine() ;

if(st.equalsIgnoreCase("END")) break ; textArea.append(st+"\n") ;

}

// Thoi Diem Ket Thuc Tinh Thoi Gian Date end = new Date() ;

long finish = end.getTime() ;

// Thoi Gian Thuc Hien

int time = (int)(finish - start) ;

JOptionPane.showMessageDialog(new JFrame(), "Thoi Gian Bat Dau : "+start+

"\nThoi Gian Ket Thuc : "+finish+

"\nThoi Gian Thuc Hien : "+time,"Thong Bao", JOptionPane.INFORMATION_MESSAGE) ;

}

catch(IOException ex) {

} }

catch(Exception e) {

JOptionPane.showMessageDialog(new JFrame(),"Loi e : "+e +"\nin = "+in+"\nout = "+out+"\nconnection =

"+connection,"Thong Bao",JOptionPane.ERROR_MESSAGE) ; }

}

//--- public void destroy()

{ try { out.println("END") ; connection.close() ; } catch(IOException e) { JOptionPane.showMessageDialog(new JFrame(),e,"Thong Bao",JOptionPane.ERROR_MESSAGE) ; } } }

2. Truy nhập cơ sở dữ liệu Web bằng phương pháp Servlet

import javax.servlet.*; import javax.servlet.http.*; import java.sql.*;

public class FindAll extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, java.io.IOException { resp.setContentType("text/html"); java.io.PrintWriter out=new java.io.PrintWriter(resp.getOutputStream()); out.println("<html>"); out.println("<head>"); out.println("<title>Danh sach</title>"); out.println("</head>"); out.println("<h2><center>"); out.println("DANH SACH"); out.println("</center></h2>"); out.println("<br>"); String drivername="sun.jdbc.odbc.JdbcOdbcDriver"; String connectionURL="jdbc:odbc:SinhVien"; String username=""; String password="";

String sqlQuery="SELECT * FROM DIEM"; //Doi tuong ket noi JDBC

Connection con=null;

//Doi tuong thuc hien cau lenh truy van Statement stmt=null;

ResultSet rs=null; try

{

//Tao trinh dieu khien

Class.forName(drivername).newInstance(); //Tao ket noi

con=DriverManager.getConnection(connectionURL,username,password); //Tao cau lenh truy van

stmt=con.createStatement();

//Thuc thi cau lenh truy van va nhan ket qua tra ve rs=stmt.executeQuery(sqlQuery);

//Tao bang hien thi

out.println("<center><table border>"); //trich du lieu

ResultSetMetaData rsmd=rs.getMetaData(); int columnCount=rsmd.getColumnCount(); //Dinh dang dong tieu de

out.println("<tr>");

for(int i=0; i<columnCount; i++) { out.println("<th>"+rsmd.getColumnLabel(i+1)+"</th>"); } out.println("</tr>"); while(rs.next()) { out.println("<tr>"); //Dinh dang tung cot

for(int i=0; i<columnCount;i++) { out.println("<td>"+rs.getString(i+1)+"</td>"); } //het dong out.println("</tr>"); } //Ket bang out.println("</table></center>"); out.println("</html>"); out.flush(); out.close(); }catch(Exception ex){ out.println(ex); } } }

3. Truy nhập cơ sở dữ liệu web bằng phương pháp RMI

* Định nghĩa giao diện chứa phương thức từ xa

import java.rmi.Remote ;

import java.rmi.RemoteException ;

public interface DataAccess extends Remote {

public String getData(String query) throws RemoteException ; }

* Cài đặt phương thức gọi từ xa

import java.rmi.* ;

import java.rmi.server.* ; import java.sql.* ;

import javax.swing.* ;

public class RemoteAccess extends UnicastRemoteObject implements DataAccess

{

private ResultSet rs = null ; private Statement stmt = null ; private Connection con = null ;

private String driver = "sun.jdbc.odbc.JdbcOdbcDriver" ; private String url = "jdbc:odbc:SinhVien" ;

private String data=null ;

//--- public RemoteAccess() throws RemoteException

{

super() ; }

//--- public String getData(String query) throws RemoteException {

System.out.println("Phuong Thuc getData() Duoc Goi") ; // Ket Noi CSDL

try {

// Nap Driver

Class.forName(driver) ; // Thiet Lap Ket Noi CSDL

con = DriverManager.getConnection(url) ; }

catch(java.lang.ClassNotFoundException e) {

System.out.println("Khong Tim Thay Lop Driver") ; }

catch(java.sql.SQLException e) {

System.out.println("Khong Nhan Duoc Ket Noi") ; }

// Truy Cap CSDL Bang SQL try

{

System.out.println("Khoi Lenh Truy Van Data Duoc Goi") ; stmt = con.createStatement() ;

rs = stmt.executeQuery("Select * From DIEM") ; data = new String("") ;

// Doc Cau Truc Cua ResultSet

ResultSetMetaData metaData = rs.getMetaData() ; int columnCount = metaData.getColumnCount() ; // Lay Tieu De Cot

data = new

String(data+metaData.getColumnLabel(i)+"\t") ; data = new String(data+"\n") ; // Lay Gia Tri Cac Truong Du Lieu while(rs.next())

{

for(int i = 1 ; i <= columnCount ; i++)

data = new String(data+rs.getString(i)+"\t") ; data = new String(data+"\n") ;

}

System.out.println(data) ; }

catch(java.sql.SQLException e) {

JOptionPane.showMessageDialog(new JFrame(),"Khong Thuc Hien Duoc Cau Lenh SQL","Thong Bao",JOptionPane.CLOSED_OPTION) ;

}

return data ; }

//--- public static void main(String[] args)

{

// Tao Che Do Bao Mat

if(System.getSecurityManager() == null)

System.setSecurityManager(new RMISecurityManager()) ; // Dang Ky Doi Tuong Tu Xa Voi rmiregistry

String name = "//127.0.0.1/DataAccess" ; try

{

DataAccess remoteAccess = new RemoteAccess() ; Naming.rebind(name, remoteAccess) ;

System.out.println("Remote Data Server San Sang") ; }

catch(java.rmi.RemoteException e) {

JOptionPane.showMessageDialog(new JFrame(),

"Khong Tao Duoc Doi Tuong Tu Xa\nNguyen Nhan Chua Khoi Dong rmiregistry\nHoac Chua Co Policy file","Thong

Bao",JOptionPane.CLOSED_OPTION) ; }

catch(java.net.MalformedURLException e) {

JOptionPane.showMessageDialog(new JFrame(),"Khong Tim Thay Doi Tuong Tu Xa","Thong Bao",JOptionPane.CLOSED_OPTION) ;

} } }

* Phía Clients import java.applet.Applet ; import java.awt.* ; import javax.swing.* ; import java.awt.event.* ; import java.sql.* ; import java.rmi.* ; import java.util.Date ;

public class ClientApplet extends Applet { JTextArea ta ; JPanel panel ; DataAccess dAccess ; String rs = null ; //--- public void init()

{

panel = new JPanel() ;

panel.setBackground(Color.white) ; ta = new JTextArea(100,0) ; ta.setEditable(false) ; setLayout(new BorderLayout()) ; panel.add("Center",ta) ; add("Center",panel) ;

// Cai Che Do Quan Ly Bao Mat RMI

if(System.getSecurityManager() == null)

System.setSecurityManager(new RMISecurityManager()) ;

try {

String name = "//127.0.0.1/DataAccess" ; dAccess = (DataAccess)Naming.lookup(name) ; }

catch(java.rmi.NotBoundException e) {

JOptionPane.showMessageDialog(new JFrame(),"Khong Tim Thay Doi Tuong Tu Xa","Thong Bao",JOptionPane.CLOSED_OPTION) ;

}

catch(java.rmi.RemoteException e) {

JOptionPane.showMessageDialog(new JFrame(),"Khong Tim Thay Doi Tuong Tu Xa","Thong Bao",JOptionPane.CLOSED_OPTION) ;

}

catch(java.net.MalformedURLException e) {

JOptionPane.showMessageDialog(new JFrame(),"Khong Tim Thay Doi Tuong Tu Xa","Thong Bao",JOptionPane.CLOSED_OPTION) ;

} /**

* Thoi Diem Bat Dau Tinh Thoi Gian */

Date now = new Date() ; long start = now.getTime() ;

// Goi Phuong Thuc Tu Xa Lay Du Lieu try

{

rs = dAccess.getData() ;

if(rs != null) ta.setText(rs) ;

else ta.setText("Khong Nhan Duoc Du Lieu") ; }

catch(java.rmi.RemoteException e) {

JOptionPane.showMessageDialog(new JFrame(),"Khong Truy Cap Duoc Doi Tuong Tu Xa","Thong Bao",JOptionPane.CLOSED_OPTION) ;

} /**

* Thoi Diem Ket Thuc Tinh Thoi Gian */

Date end = new Date() ;

long finish = end.getTime() ;

/**

* Thoi Gian Thuc Hien */

int time = (int)(finish - start) ;

JOptionPane.showMessageDialog(new JFrame(), "Thoi Gian Bat Dau : "+start+

"\nThoi Gian Ket Thuc : "+finish+

"\nThoi Gian Thuc Hien : "+time,"Thong Bao", JOptionPane.INFORMATION_MESSAGE) ;

} }

Một phần của tài liệu dữ liệu nguồn warehouese trong cơ sở thiết kế data (Trang 107)