HelloWorld
HelloWorld

Reputation: 39

How to connect CouchDB with Java

I'm new about NOSQL. I use couchDB and ektrop Java API. I tried these code but it gives HTTP 405 error.

protected CouchDbInstance _db;
{       
String dbname = "my_database";
try {
//creates a database with the specified name
CouchDbConnector dbc = _db.createConnector(dbname, true);

//create a simple doc to place into your new database
Map<String, Object> doc = new HashMap<String, Object>();
doc.put("_id", UUID.randomUUID().toString());
doc.put("season", "summer");
doc.put("climate", "arid");
dbc.create(doc);

} catch (Exception e) {

}

Examples on the internet are very complex for me, so I didn't understand anything and i did not find any tutorial, so i have two questions.
-How can i connect db ?
-How can i add/delete/update documents operations ? If you give me examples codes, i will be really happy. Also you can suggest good tutorial. Thanks in advance.

Upvotes: 0

Views: 2511

Answers (1)

Nallamachu
Nallamachu

Reputation: 1488

I am also new to CouchDB/NoSQL. But I am answering my best ignore if it not helps to you.

  1. It seems you are not even opening the session by passing user login credentials.
  2. Also you are directly trying to put Map object into DB create.
Session studentDbSession = new Session("localhost",5984);
Database studentCouchDb = studentDbSession.getDatabase("DBNAME");
Document newdoc = new Document();
Map<String , String> properties = new HashMap<String,String>();
properties.put(STUDENT_KEY_NAME, "REDDY");
properties.put(STUDENT_KEY_MARKS, "90");
properties.put(STUDENT_KEY_ROLL, "007");
newdoc.putAll(properties);
studentCouchDb.saveDocument(newdoc); 

For more information you can also refer Adding Document Using Java Couchdb4j.

Upvotes: 1

Related Questions