Java 使用 jdbc 驱动程序连接到 MongoDB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32970871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting to MongoDB using jdbc driver
提问by manoj
Purpose is to connect MongoDB remote server through JAVA:
目的是通过JAVA连接MongoDB远程服务器:
URL = "jdbc:mongo://" + serverIP + ":"
+ port+ "/" +databaseName;
Class.forName("mongodb.jdbc.MongoDriver");
dbConn = getConnection(URL,mongo1, mongo1);
Tried Unity_trial.Jar, mongo_version.jar files but the error comes is 'mongodb.jdbc.MongoDriver' classNameNotFound.
尝试了 Unity_trial.Jar、mongo_version.jar 文件,但错误是'mongodb.jdbc.MongoDriver' classNameNotFound。
If I comment the class.forname line, the next error is
如果我评论 class.forname 行,下一个错误是
URL = "jdbc:mongo://" + serverIP + ":" + port
+ "/" +databaseName;
is not in correct format. Not sure about where I am making the mistake. Thanks for your help in advance.
格式不正确。不确定我在哪里犯了错误。提前感谢您的帮助。
回答by s.meissner
You can checkout this project:
你可以签出这个项目:
https://github.com/erh/mongo-jdbc
https://github.com/erh/mongo-jdbc
There are two examples given.
给出了两个例子。
But in general I would recommend to use the MongoDB Client or some Spring Data abstraction.
但总的来说,我建议使用 MongoDB Client 或一些 Spring Data 抽象。
回答by Andy Guibert
If you are getting a ClassNotFoundException, the issue is that the jar containing the mongodb.jdbc.MongoDriver
class is not on your classpath. If you're not sure what JAR this class is in, I would reccomend getting 7-Zipso that you can inspect the contents of the jar and see for yourself if the class is there.
如果您收到 ClassNotFoundException,则问题在于包含mongodb.jdbc.MongoDriver
该类的 jar不在您的类路径中。如果您不确定这个类是什么 JAR,我建议您使用7-Zip,这样您就可以检查 jar 的内容并亲自查看该类是否在那里。
The correct way to connect to MongoDB with your approach is:
使用您的方法连接到 MongoDB 的正确方法是:
Class.forName("mongodb.jdbc.MongoDriver");
String URL = "jdbc:mongo://<servername>:<port>/<databaseName>";
Connection jdbcConn = DriverManager.getConnection(url,"user","pass");
But MongoDB isn't really meant to be used with JDBC, so if your requirements allow, I would reccomend getting a connection the "mongodb" way.
但是 MongoDB 并不是真的要与 JDBC 一起使用,所以如果您的要求允许,我会建议以“mongodb”方式获得连接。
MongoClient client = new MongoClient("localhost");
For details on how to do it this way, see the MongoDB docs
有关如何以这种方式执行此操作的详细信息,请参阅 MongoDB 文档
回答by Tmac Zhou
I met this question today morning.
The key is missing mongo-java-driver.jar
.
when I add the jar, the project can run normal.
我今天早上遇到了这个问题。钥匙不见了 mongo-java-driver.jar
。添加jar后,项目就可以正常运行了。
回答by DirtyMind
I know its very late to answer but might help someone else. If you are compiling and running your code from cmdthen before compilation set classpath for mongo.jar like below :
我知道现在回答已经很晚了,但可能会帮助其他人。如果您正在从cmd编译和运行代码, 则在编译之前为 mongo.jar 设置类路径,如下所示:
set classpath=C:\DemoProject\java db\Mongo\mongo.jar;
set classpath=C:\DemoProject\java db\Mongo\mongo.jar;
then run your code.
然后运行你的代码。
or if you are using editor like eclipse then add this jar to your lib folder.
或者,如果您使用的是像 eclipse 这样的编辑器,则将此 jar 添加到您的 lib 文件夹中。
回答by Roberto
If you are looking for a MongoDb JDBC driver which does support native MongoDb queries like below, look for DbSchema. The driver is Open Source on Bitbucket. From experience the team is responsive to any features or issues.
如果您正在寻找支持如下所示的本机 MongoDb 查询的 MongoDb JDBC 驱动程序,请查找DbSchema。驱动程序是Bitbucket 上的开源。根据经验,该团队可以对任何功能或问题做出响应。
Class.forName("com.dbschema.MongoDbJdbcDriver");
Properties properties = new Properties();
properties.put("user", "someuser");
properties.put("password", "somepassword" );
Connection con = DriverManager.getConnection("jdbc:mongodb://host1:9160/keyspace1", properties);
// OTHER URL (SAME AS FOR MONGODB NATIVE DRIVER): mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000
String query = "db.sampleCollection().find()";
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery( query );
Object json = rs.getObject(1);