我CONTROLL數據插入或如下MongoDB的支票插入
public boolean addUser(User user) throws Exception {
boolean isRegister=false;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
String sql="insert into users(username,email,pasword) values(?,?,?)";
conn=dataSource.getConnection();
ps=conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, kullanici.getEmail());
ps.setString(3, kullanici.getPassword());
int success=ps.executeUpdate();
if(success>0){
isRegister=true;
}
return isRegister;
}finally{
baglantiSonlandir(conn, null, ps);
}
}
如何能夠做到在MongoDB中這個過程在mysql中沒有插入? 我寫了一些代碼,但我沒有實現數據插入或未插入到db.My MongoDb代碼是波紋管;
public boolean addUser(User user) throws Exception {
boolean isRegister=false;
MongoClient client=null;
MongoDatabase database=null;
try{
client=new MongoClient("localhost", 27017);
database=client.getDatabase("ogryonsis");
MongoCollection collection=database.getCollection("users");
Document document=new Document();
document.put("username", user.getUserName());
document.put("email", user.getEmail());
document.put("password", user.getPassword());
collection.insertOne(document);
//int success ->I didnt find for here appropriate code
if(success>0){
isRegister=true;
}
return isRegister;
}finally{
client.close();
}
}
因爲insertone返回void所以沒有辦法,如果沒有異常被拋出比你可以假設數據已成功寫入,進一步閱讀的嘗試[這個答案](http://stackoverflow.com/questions/40137495 /升級-蒙戈至3-X-嵌件不再視爲返回-寫結果/ 40138542#40138542) –