Java couchdb4j数据库连接

时间:2013-12-09 12:35:11

标签: java database json couchdb cloudant

我正在使用couchdb4j与couchDB建立连接。当我连接到我的本地数据库时它工作正常,这是代码:

Session LocalSession= new Session("localhost", 5984);

但我正在尝试与此网站建立联系:stockquotetest.cloudant.com但它无法正常工作,有人能发现问题吗?

Session NewSession = new Session("stockquotetest.cloudant.com/", 5984, "user", "pass");

我收到此错误:INFO:选择了基本身份验证方案

我也得到了这个

java.net.UnknownHostException: https
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.<init>(Unknown Source)
at sun.security.ssl.SSLSocketFactoryImpl.createSocket(Unknown Source)
at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:82)
at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:127)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at com.fourspaces.couchdb.Session.http(Session.java:364)
at com.fourspaces.couchdb.Session.get(Session.java:325)
at com.fourspaces.couchdb.Session.getDatabaseNames(Session.java:167)
at CouchDBTest.ConnectDB(CouchDBTest.java:41)
at Home$4.actionPerformed(Home.java:166)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

当我运行这行代码时:

        Session NewSession = new Session("https://user:pass@stockquotetest.cloudant.com/", 80, true);

2 个答案:

答案 0 :(得分:0)

我重现了这个并在我开始工作之前遇到了另一个问题。首先,6-parameter constructor for Session就是您所追求的目标:

public Session(String host, int port, String user, String pass, boolean usesAuth, boolean secure) {

在上面给出的示例中,这将起作用:

new Session("stockquotetest.cloudant.com", 443, "user", "pass", true, true);

这是因为如果您未将secure设为true,则为URL will not be constructed with https


在此工作之后,下一个错误是Databaseassuming that update_seq is an int。 Cloudant将返回字符串update_seq,而不是整数。我将Database类中的字段,响应解析和getter从int更改为String,以使代码适合我。

答案 1 :(得分:0)

尝试这个...确保你起诉coucjdb4j lib for couchdb connection。

会话studentDbSession =新会话(“localhost”,5984);

数据库studentCouchDb = studentDbSession.getDatabase(“database_name”);

希望这会对你有所帮助。