首頁  >  文章  >  Java  >  Java中的JNDI是什麼?

Java中的JNDI是什麼?

WBOY
WBOY原創
2024-08-30 15:06:42806瀏覽

Java命名和目錄介面是Java程式語言中介面的名稱。它是一個 API(應用程式介面),可與伺服器配合使用,並可以使用命名約定從資料庫中取得檔案。命名約定可以是單字或單字。它還可以合併到套接字中,以使用在專案中傳輸資料檔案或平面檔案的伺服器來實現套接字程式設計。它也可以用在瀏覽器中存在許多目錄實例的網頁中。 JNDI 為 Java 使用者提供使用 Java 編碼語言搜尋 Java 物件的工具。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

Java中JNDI的架構

在架構中,我們注意到與 JNDI 相關的不同目錄,它由一個 API 和一個稱為服務提供者介面(SPI)的介面組成。

Java中的JNDI是什麼?

在此圖中,我們注意到 JNDI 架構,它連接到 Java 應用程式。層次上明確提到了JNDI API是在介面之上的,介面是用來連接很多目錄的。下面提到了一些目錄服務。

  • 輕量級目錄存取協定
  • 網域服務。
  • Java 遠端方法呼叫。

以上提到的是JNDI SPI 整合的目錄,並建構了一個具有JNDI 實現可能性的平台。

JNDI 包

Java 中有五個使用 JNDI SPI 的套件。一些套件是javax.naming。 javax.naming 是一個包,其中包含用於存取命名服務的類別和介面。有查找、列表綁定、名稱等功能。第二個是 java.naming.directory。該套件有助於將資料作為物件獲取,並且是 java.naming 目錄的高級版本。還有其他的java套件。命名。事件和java。命名。 spi。

此外,JNDI 在三種最新的 Java 技術中發揮著重要作用。他們是:-

  • JDBC(Java 資料庫連線包
  • JMS(Java 訊息傳遞服務)
  • EJB(企業 Java Bean)

JDBC 用於資料庫處理,JMS 是訊息服務應用程式。 EJB 與 Netbeans 和 Eclipse 平台一起運行,用於運行 Java 程式。這些軟體包與它們所使用的技術一起存在。

JNDI 也與 LDAP 服務提供者一起使用。有一系列用 Java 語言運行程式設計應用程式的程式碼。

Java程式語言中有bind()和look up(),用於命名物件和從目錄中尋找物件。

Context.bind("name", object)

這裡的名稱可以為目錄中的目前物件指定任何名稱。這是設定物件名稱的綁定函數的範例。

Object hello= Context.lookup("name")

在此函數中,物件 hello 在目錄中尋找物件的名稱。也存在用作目錄支援類型的序列化或非序列化資料的變體。

JNDI 及其應用程式廣泛應用於資料分析產業,其中有大量資料需要挖掘,並且某些方面的資料儲存在不同的目錄中,檔案儲存在不同的資料夾中。它在電信業有著廣泛的應用,根據某人每小時的通話費率來計算帳單。

Java 中的 JNDI 範例

這段程式碼是一個選單驅動的程序,要求使用者輸入本金金額,然後根據使用者的需求列印單利、複利以及單利和複利之間的差額。當使用者不想繼續執行該程式時,該程式也會退出。   利率固定為8.5%,產生利息的年資為7年。據此計算所有利率。

建立一個選單驅動的程式來輸入本金額併計算單利、複利及其之間的絕對差。

代碼:

import java.io.*;
class Assignment1
{
public static void main(String[] args) throws Exception
{
BufferedReader ob = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Principal Amount : ");//prompt for entering the principal amount
float P = Float.parseFloat(ob.readLine());//accepting the principal amount
int choice = 0;
do{
choice = 0;// reseting the user's choice
//displaying the Menu of Options
System.out.println("------------- M E N U ----------------");
System.out.println("1 - To Find the Simple Interest");
System.out.println("2 - To Find the Compound Interest");
System.out.println("3 - To Find the Difference between the Simple and Compound Interests");
System.out.println("4 - To Exit The Program");
System.out.print("Enter Choice : ");//prompting for user's choice
choice = Integer.parseInt(ob.readLine());//accepting user's choice
System.out.println("");// line feed between menu and result
switch(choice)
{
case 1://for simple interest
System.out.println("The Simple Interest is Rs."+simple(P));
break;
case 2://for compound interset
System.out.println("The Compound Interest is Rs."+compound(P));
break;
case 3://for difference between simple and compound interests
System.out.println("The Absolute Difference is Rs."+(compound(P)-simple(P)));
break;
case 4:
System.out.println("Program Terminated");
break;
default://for a wrong choice entered by the user
System.out.println("Invalid Option");
}//end of switch(choice)
System.out.println("\n");//linefeed between two consecutive choices by the user
}while(choice!=4);//end of do-while
}//end of main
public static float simple(float p)//to calculate the simple interest
{
return (float)((p*8.5*7.0)/100.0); //returning the simple interest
}//end of simple
public static float compound(float p)//to calculate the compound interest
{
return (p*(float)(Math.pow((1.0+(8.5/100.0)),7.0)-1.0));//returning the compound interest
}//end of compound
}//end of class

輸出:

Java中的JNDI是什麼?

這裡輸入本金額 10000 盧比,然後計算單利和複利以及差額。

結論

本文介紹了 Java 程式的程式設計概念及其在 BlueJ 平台中的應用。此代碼用於計算本金的利率。它返回單利、複利,並根據用戶意願退出。此外,我們還了解 JNDI 如何在目錄和伺服器中使用,以及如何使用物件進行程式設計以及尋找和搜尋目錄中使用的套件。 JNDI 的主要用途是只要存在與其關聯的目錄,並且必須搜尋該目錄以獲取有關資料的有意義的見解。這個概念在 Java 中特別獨特,在 C、C++ 和 Python 等其他程式語言中並不常見。

以上是Java中的JNDI是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:什麼是Java?下一篇:什麼是Java?