执行时此代码提供异常消息,但在java中运行时相同的代码正常工作,没有任何错误和异常。
以下代码在java中编写并且正常工作但是相同的逻辑在jsp中不起作用。
import com.mongodb.*;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import java.util.*;
import com.mongodb.client.MongoCursor;
import static com.mongodb.client.model.Filters.*;
import com.mongodb.client.result.DeleteResult;
import static com.mongodb.client.model.Updates.*;
import com.mongodb.client.result.UpdateResult;
import org.json.JSONObject;
public class TestMongo {
public static void main(String[] args) {
String name = "Ram";
long ph = 7077330520L;
String email = "DB@gmail.com";
String password = "qwerty";
String city = "BBSR";
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("user");
collection.drop();
Document doc = new Document("name", name)
.append("ph", ph)
.append("email", email)
.append("password",password)
.append("city", city);
collection.insertOne(doc);
MongoCursor<Document> cursor = collection.find().iterator();
while (cursor.hasNext()) {
//if (cursor.next().getInteger("age") == 20)
System.out.println(cursor.next());
}
}
}
[![<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import ="java.util.List" %>
<%@ page import ="java.util.Set" %>
<%@ page import="com.mongodb.*" %>
<%@ page import="com.mongodb.client.MongoDatabase" %>
<%@ page import="org.bson.Document" %>
<%@ page import="com.mongodb.client.MongoCollection" %>
<%
String name = "Ram";
long ph = 7077330520L;
String email = "DB@gmail.com";
String password = "qwerty";
String city = "BBSR";
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("user");
collection.drop();
Document doc = new Document("name", name)
.append("ph", ph)
.append("email", email)
.append("password",password)
.append("city", city);
collection.insertOne(doc);
%>][1]][1]
答案 0 :(得分:1)
With mongo-java-driver-2.10.1.jar present in your build path surely below code works
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import ="java.util.List" %>
<%@ page import ="java.util.Set" %>
<%@ page import="com.mongodb.*" %>
<%@ page import="com.mongodb.DB" %>
<%@ page import="org.json.JSONObject" %>
<%@ page import="com.mongodb.DBCollection" %>
<%
String name = "Ram";
long ph = 7077330520L;
String email = "DB@gmail.com";
String password = "qwerty";
String city = "BBSR";
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB database = mongoClient.getDB("test");
DBCollection collection = database.getCollection("user");
//collection.drop();
BasicDBObject document = new BasicDBObject();
document.put("name", name);
document.put("ph", ph);
document.put("email", email);
document.put("password",password);
document.put("city", city);
collection.insert(document);
%>
After inserting I have tested as well
> db.user.find().pretty()
{
"_id" : ObjectId("59549f53a5653b5b4303004f"),
"name" : "Ram",
"ph" : NumberLong("7077330520"),
"email" : "DB@gmail.com",
"password" : "qwerty",
"city" : "BBSR"
}
{
"_id" : ObjectId("59549fbda5653b5b43030050"),
"name" : "Ram",
"ph" : NumberLong("7077330520"),
"email" : "DB@gmail.com",
"password" : "qwerty",
"city" : "BBSR"
}
{
"_id" : ObjectId("59549feaa5653b5b43030051"),
"name" : "Ram",
"ph" : NumberLong("7077330520"),
"email" : "DB@gmail.com",
"password" : "qwerty",
"city" : "BBSR"
}
> As I executed many times, number of rows inserted 3 times.
答案 1 :(得分:0)
如果你真的想要使用新的驱动程序,你需要在构建路径中使用它,首先必须删除所有编译错误。编译错误标记为红色,可以从eclipse编辑器中轻松找到