Java Mongodb 简明教程

Java & MongoDB - Environment Setup

Install MongoDB database

按照 MongoDB - Environment 中的说明进行 MongoDB 安装

Follow the MongoDB installation steps using MongoDB - Environment

Install Java

可以免费从以下链接下载 Java SE −

Java SE can be downloaded for free from the following link −

你可以根据自己的操作系统下载一个版本。

You download a version based on your operating system.

按照说明下载 Java,并运行 .exe 在你的计算机上安装 Java。在计算机上安装 Java 后,你需要设置环境变量来指向正确的安装目录。

Follow the instructions to download Java, and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set environment variables to point to correct installation directories.

Setting Up the Path for Windows 2000/XP

假设你已将 Java 安装在 c:\Program Files\java\jdk 目录中 −

Assuming you have installed Java in c:\Program Files\java\jdk directory −

  1. Right-click on 'My Computer' and select 'Properties'.

  2. Click on the 'Environment variables' button under the 'Advanced' tab.

  3. Now, alter the 'Path' variable so that it also contains the path to the Java executable. For example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.

Setting Up the Path for Windows 95/98/ME

假设你已将 Java 安装在 c:\Program Files\java\jdk 目录中 −

Assuming you have installed Java in c:\Program Files\java\jdk directory −

  1. Edit the 'C:\autoexec.bat' file and add the following line at the end − SET PATH=%PATH%;C:\Program Files\java\jdk\bin

Setting Up the Path for Linux, UNIX, Solaris, FreeBSD

环境变量 PATH 应设置为指向已安装 Java 二进制文件的位置。如果你在这方面遇到问题,请参阅 shell 文档。

Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this.

例如,如果你使用 bash 作为 shell,则需要在“‘.bashrc: export PATH=/path/to/java:$PATH’”的末尾添加以下行

For example, if you use bash as your shell, then you would add the following line at the end of your '.bashrc: export PATH=/path/to/java:$PATH'

要编写 Java 程序,你需要一个文本编辑器。市场上有更复杂的 IDE 可用。但目前,你可以考虑以下选项之一:

To write Java programs, you need a text editor. There are even more sophisticated IDEs available in the market. But for now, you can consider one of the following −

  1. Notepad − On Windows machine, you can use any simple text editor like Notepad (recommended for this tutorial) or TextPad.

  2. Netbeans − It is a Java IDE that is open-source and free. It can be downloaded from https://netbeans.org/index.html.

  3. Eclipse − It is also a Java IDE developed by the Eclipse open-source community and can be downloaded from www.eclipse.org/.

Java MongoDB Driver

  1. You need to download the jar mongo-java-driver.jar. Make sure to download the latest release of these jar files.

  2. You need to include the downloaded jar files into your classpath.

  3. We are using mongo-java-driver-3.12.8.jar to run examples.