2013-03-17 58 views
1

我正在研究使用Ektorp框架與CouchDB進行通信的Java應用程序。我用下面的代碼來創建一個新的CouchDbInstance:Ektorp CouchDB測試連接

HttpClient httpClient = new StdHttpClient.Builder() 
    .host("localhost") 
    .port("5984") 
    .username("") 
    .password(""); 

/* no user name and password required because, its admin party */ 

CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient); 

現在我想在我創建/修改等文件來測試到CouchDB的連接。沒有測試連接的方法。你有給我的小費嗎?

回答

1

我在我目前的項目中使用EKTORP我們正在與couchDbConnector.getAllDatabases測試這個(),捕DbAccessExceptions。

這比checkIfDbExists()完全不知道CouchDB服務器上配置的實際數據庫的輕微優勢,並且沒有引用任何特定於應用程序的配置來進行連接檢查,只是感覺有點乾淨。

1

您可以使用CouchDbConnector#getDbInfo()

+0

CouchDbInstance不是可能嗎? – konze 2013-03-17 15:53:10

+0

CouchDbInstance#checkIfDbExists沒有副作用 – 2013-03-17 18:57:23

-1

我目前正在使用ektorp進行項目工作,並且正在測試像這樣的數據庫連接。

第1步 - 連接到HttpClient的爲你做

HttpClient httpClient = new StdHttpClient.Builder() 
    .host("localhost") 
    .port("5984") 
    .username("") 
    .password(""); 

/* no user name and password required because, its admin party */ 

CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient); 

第2步 - 創建一個CouchDbInstance

CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient); 

第3步 - 使用CouchDbConnector連接到特定的數據庫你的本地主機CouchDB(在這個例子中,我的數據庫被稱爲專輯,但你只需要輸入你連接的任何數據庫的名稱來代替你正在編碼的專輯)

CouchDbConnector albums = dbInstance.createConnector("albums", true); 

第4步 - 您可以打印出您所連接的數據庫的名稱。

System.out.println(albums.getDatabaseName()); 

你也可以創建一個能夠通過創建一個字符串類型的方法與

return albums.getDatabaseName(); 

我希望這是有益打印您連接到瀏覽器的數據庫名稱的端點。我用ektorp的reference documentation來了解這一點。我花了一段時間才弄清楚如何使用ektorp,我仍在學習!