Last active
December 11, 2015 03:59
-
-
Save sourcerebels/4542305 to your computer and use it in GitHub Desktop.
Open mongodb connection from java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sourcerebels.test.mongodb; | |
import java.net.UnknownHostException; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.DBObject; | |
import com.mongodb.MongoClient; | |
public class App | |
{ | |
public static void main( String[] args ) | |
{ | |
System.out.println( "Fist acces to mongodb from Java!" ); | |
MongoClient client = null; | |
try { | |
client = new MongoClient("host"); | |
DB db = client.getDB("database"); | |
boolean auth = db.authenticate("username", "password".toCharArray()); | |
DBCollection col = db.getCollection("collection"); | |
DBObject obj = col.findOne(); | |
System.out.println(obj); | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} finally { | |
if (client != null) { | |
client.close(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.sourcerebels</groupId> | |
<artifactId>test-mongodb</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>test-mongodb</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.mongodb</groupId> | |
<artifactId>mongo-java-driver</artifactId> | |
<version>2.10.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment