Skip to content

Instantly share code, notes, and snippets.

@mhelmstetter
Last active December 24, 2015 16:29
Show Gist options
  • Save mhelmstetter/6828936 to your computer and use it in GitHub Desktop.
Save mhelmstetter/6828936 to your computer and use it in GitHub Desktop.
Sample Java application connecting to MongoDB via SSL
import javax.net.ssl.SSLSocketFactory;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
public class SSLApp {
public static void main(String args[]) throws Exception {
MongoClientOptions o = new MongoClientOptions.Builder()
.socketFactory(SSLSocketFactory.getDefault())
.build();
MongoClient m = new MongoClient("localhost", o);
DB db = m.getDB( "test" );
DBCollection c = db.getCollection( "foo" );
c.insert(new BasicDBObject("testKey", "testValue"));
System.out.println( c.findOne() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment