標籤:java 登陸驗證 測試 mongodb
1、建立串連使用者
> mongo ip:port
> use test
> db.addUser("root", "123456")
>db.auth("root","123456") (登陸驗證)
2、java串連mongodb的驅動,:https://github.com/mongodb/mongo-java-driver/downloads。
3、測試代碼
import java.net.UnknownHostException;import com.mongodb.DB;import com.mongodb.Mongo;import com.mongodb.MongoException;public class Util {/** * @param args * @throws MongoException * @throws UnknownHostException */public static void main(String[] args) {Mongo m;try {m = new Mongo("192.168.86.1", 27027); //<span style="font-family: Arial;">192.168.86.1為Ip,27027mongoDB的連接埠</span>DB db = m.getDB("test"); if(db.authenticate("root", "123456".toCharArray())){ System.out.println("success");; } else System.out.println("false");} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (MongoException e) {// TODO Auto-generated catch blocke.printStackTrace();} }}
Java串連MongoDB