gpt4 book ai didi

java - 从java中的getter和setter方法获取值以插入Mongodb?

转载 作者:行者123 更新时间:2023-12-01 18:18:39 26 4
gpt4 key购买 nike

如何从java中的getter和setter方法获取值并插入到Mongodb中。我使用下面的代码插入到mongodb中

DBCollection coll = db.getCollection("mycol");
BasicDBObject doc = new BasicDBObject("id","Hello" ).
append("description", "database").
append("likes", 100).
append("url", "http://http://www.flowersofindia.net/").
append("by", "Rose");
coll.insert(doc);

上面给出的是一些硬代码值。但是我需要从我编写的 setter 方法中获取值。这是我的 getter 和 setter 类。

public class Encapsulation {
private String id;
private String product_name;
private String product_url;
private String product_image;
private String product_price;
private String product_src;
private String country;
private String date;
private String Category;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getProduct_url() {
return product_url;
}
public void setProduct_url(String product_url) {
this.product_url = product_url;
}
public String getProduct_image() {
return product_image;
}
public void setProduct_image(String product_image) {
this.product_image = product_image;
}


public String getProduct_price() {
return product_price;
}
public void setProduct_price(String product_price) {
this.product_price = product_price;
}
public String getProduct_src() {
return product_src;
}
public void setProduct_src(String product_src) {
this.product_src = product_src;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return Category;
}
public void setCategory(String category) {
Category = category;
}

}

.有人可以帮忙吗?任何帮助将不胜感激。我是这个环境的新手

最佳答案

下面是使用 setter 和 getter 插入 mongodb 的代码。

请注意

1) 我使用了相同的 Encapsulation 类以及 getter 和 setter。

2) 我已使用 mongo-java-driver-2.12.2.jar 作为 mongo java 驱动程序。

3) mongodb 正在端口 27017 运行。

代码:

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

public class MongoInsert {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {


Encapsulation bean=new Encapsulation();
// Setting values
bean.setId("value1");
bean.setProduct_name("value2");
bean.setProduct_image("value3");
bean.setProduct_price("value4");
bean.setProduct_src("value5");
bean.setProduct_url("value6");
bean.setDate("value7");
bean.setCountry("value8");
bean.setCategory("value9");

MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("Test");
DBCollection coll = db.getCollection("mycol");

//getting values and assigning to mongo field
BasicDBObject doc = new BasicDBObject("id", bean.getId()).
append("Product_name", bean.getProduct_name()).
append("Product_image", bean.getProduct_image()).
append("Product_price", bean.getProduct_price()).
append("Product_src", bean.getProduct_src()).
append("Product_url", bean.getProduct_url()).
append("Date", bean.getDate()).
append("Country", bean.getCountry()).
append("Category", bean.getCategory());

coll.insert(doc);
}
}

希望有帮助。

关于java - 从java中的getter和setter方法获取值以插入Mongodb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28209125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com