抱歉,我是 mongodb 连接代码的新手 我尝试将数据库从 eclipse(android)插入到 mongodb 但我的数据库中没有发生任何事情
当我使用“show dbs”本地 0.03125GB testdb <> 签入 mongo.exe
注意:抱歉,由于需要声誉,我无法发布图片。更新问题(2013 年 11 月 13 日)
package com.example.cobanih;
import java.util.Date;
import java.util.Set;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
public class MongoConnection extends Activity{
/*static final String domain = "localhost";
static final int port = 27107;
static final String database = "test";*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LongOperation MyTask= new LongOperation();
MyTask.execute();
}
private class LongOperation extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
insert();
return "Executed";
}
@Override
protected void onPostExecute(String result) {
// txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
public boolean insert(){
System.out.println("check");
try {
System.out.println("check2");
MongoClient mongoClient = new MongoClient("localhost",27017);
System.out.println("check3");
DB db = mongoClient.getDB("testdb");
DBCollection table = db.getCollection("user");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document = new BasicDBObject();
document.put("name", "donny");
document.put("age", 30);
document.put("createdDate", new Date());
table.insert(document);
/**** Find and display ****/
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name","donny");
DBCursor cursor = table.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
mongoClient.close();
}
catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
}
这是日志:
11-12 13:05:50.822: I/System.out(6069): check
11-12 13:05:50.822: I/System.out(6069): check2
11-12 13:05:54.262: I/dalvikvm(6069): Could not find method java.lang.management.ManagementFactory.getRuntimeMXBean, referenced from method org.bson.types.ObjectId.<clinit>
11-12 13:05:54.262: W/dalvikvm(6069): VFY: unable to resolve static method 5446: Ljava/lang/management/ManagementFactory;.getRuntimeMXBean ()Ljava/lang/management/RuntimeMXBean;
11-12 13:05:54.272: D/dalvikvm(6069): VFY: replacing opcode 0x71 at 0x0071
11-12 13:05:54.462: W/org.bson.ObjectId(6069): null
11-12 13:05:54.462: W/org.bson.ObjectId(6069): java.net.SocketException
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.net.NetworkInterface.rethrowAsSocketException(NetworkInterface.java:224)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.net.NetworkInterface.collectIpv4Address(NetworkInterface.java:190)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.net.NetworkInterface.getByName(NetworkInterface.java:118)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.net.NetworkInterface.getNetworkInterfacesList(NetworkInterface.java:283)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.net.NetworkInterface.getNetworkInterfaces(NetworkInterface.java:274)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at org.bson.types.ObjectId.<clinit>(ObjectId.java:357)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at com.mongodb.Bytes.<clinit>(Bytes.java:226)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at com.mongodb.DBTCPConnector.<clinit>(DBTCPConnector.java:32)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at com.mongodb.Mongo.<init>(Mongo.java:193)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at com.mongodb.MongoClient.<init>(MongoClient.java:138)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at com.mongodb.MongoClient.<init>(MongoClient.java:126)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at com.mongodb.MongoClient.<init>(MongoClient.java:115)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at com.example.cobanih.MongoConnection.insert(MongoConnection.java:59)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:37)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:1)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.lang.Thread.run(Thread.java:856)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at libcore.io.Posix.socket(Native Method)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): at java.net.NetworkInterface.collectIpv4Address(NetworkInterface.java:175)
11-12 13:05:54.462: W/org.bson.ObjectId(6069): ... 19 more
11-12 13:05:55.122: I/dalvikvm(6069): Could not find method java.lang.management.ManagementFactory.getPlatformMBeanServer, referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.<init>
11-12 13:05:55.142: W/dalvikvm(6069): VFY: unable to resolve static method 5445: Ljava/lang/management/ManagementFactory;.getPlatformMBeanServer ()Ljavax/management/MBeanServer;
11-12 13:05:55.142: D/dalvikvm(6069): VFY: replacing opcode 0x71 at 0x0003
11-12 13:05:55.242: D/dalvikvm(6069): GC_CONCURRENT freed 198K, 10% free 2974K/3292K, paused 72ms+78ms, total 214ms
11-12 13:05:55.262: E/dalvikvm(6069): Could not find class 'javax.management.ObjectName', referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.createObjectName
11-12 13:05:55.262: W/dalvikvm(6069): VFY: unable to resolve new-instance 866 (Ljavax/management/ObjectName;) in Lcom/mongodb/util/management/jmx/JMXMBeanServer;
11-12 13:05:55.262: D/dalvikvm(6069): VFY: replacing opcode 0x22 at 0x0000
11-12 13:05:55.272: W/dalvikvm(6069): VFY: unable to resolve exception class 863 (Ljavax/management/MalformedObjectNameException;)
11-12 13:05:55.272: W/dalvikvm(6069): VFY: unable to find exception handler at addr 0x6
11-12 13:05:55.282: W/dalvikvm(6069): VFY: rejected Lcom/mongodb/util/management/jmx/JMXMBeanServer;.createObjectName (Ljava/lang/String;)Ljavax/management/ObjectName;
11-12 13:05:55.282: W/dalvikvm(6069): VFY: rejecting opcode 0x0d at 0x0006
11-12 13:05:55.282: W/dalvikvm(6069): VFY: rejected Lcom/mongodb/util/management/jmx/JMXMBeanServer;.createObjectName (Ljava/lang/String;)Ljavax/management/ObjectName;
11-12 13:05:55.282: W/dalvikvm(6069): Verifier rejected class Lcom/mongodb/util/management/jmx/JMXMBeanServer;
11-12 13:05:55.602: I/System.out(6069): check3
11-12 13:05:55.852: W/com.mongodb.tcp(6069): Exception executing isMaster command on localhost/127.0.0.1:27017
11-12 13:05:55.852: W/com.mongodb.tcp(6069): java.io.IOException: couldn't connect to [localhost/127.0.0.1:27017] bc:java.net.SocketException: socket failed: EACCES (Permission denied)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBPort._open(DBPort.java:214)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBPort.go(DBPort.java:107)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBPort.go(DBPort.java:88)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBPort.findOne(DBPort.java:143)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBPort.runCommand(DBPort.java:148)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:620)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:254)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBCollection.insert(DBCollection.java:75)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBCollection.insert(DBCollection.java:59)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.mongodb.DBCollection.insert(DBCollection.java:104)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.example.cobanih.MongoConnection.insert(MongoConnection.java:71)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:37)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:1)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-12 13:05:55.852: W/com.mongodb.tcp(6069): at java.lang.Thread.run(Thread.java:856)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): Exception executing isMaster command on localhost/127.0.0.1:27017
11-12 13:05:56.022: W/com.mongodb.tcp(6069): java.io.IOException: couldn't connect to [localhost/127.0.0.1:27017] bc:java.net.SocketException: socket failed: EACCES (Permission denied)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBPort._open(DBPort.java:214)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBPort.go(DBPort.java:107)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBPort.go(DBPort.java:88)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBPort.findOne(DBPort.java:143)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBPort.runCommand(DBPort.java:148)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:527)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:174)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBCollection.insert(DBCollection.java:75)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBCollection.insert(DBCollection.java:59)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.mongodb.DBCollection.insert(DBCollection.java:104)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.example.cobanih.MongoConnection.insert(MongoConnection.java:71)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:37)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:1)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-12 13:05:56.022: W/com.mongodb.tcp(6069): at java.lang.Thread.run(Thread.java:856)
11-12 13:05:56.123: W/com.mongodb(6069): emptying DBPortPool to localhost/127.0.0.1:27017 b/c of error
11-12 13:05:56.123: W/com.mongodb(6069): java.io.IOException: couldn't connect to [localhost/127.0.0.1:27017] bc:java.net.SocketException: socket failed: EACCES (Permission denied)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBPort._open(DBPort.java:214)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBPort.go(DBPort.java:107)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBPort.go(DBPort.java:84)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBPort.say(DBPort.java:79)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:181)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBCollection.insert(DBCollection.java:75)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBCollection.insert(DBCollection.java:59)
11-12 13:05:56.123: W/com.mongodb(6069): at com.mongodb.DBCollection.insert(DBCollection.java:104)
11-12 13:05:56.123: W/com.mongodb(6069): at com.example.cobanih.MongoConnection.insert(MongoConnection.java:71)
11-12 13:05:56.123: W/com.mongodb(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:37)
11-12 13:05:56.123: W/com.mongodb(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:1)
11-12 13:05:56.123: W/com.mongodb(6069): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-12 13:05:56.123: W/com.mongodb(6069): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-12 13:05:56.123: W/com.mongodb(6069): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-12 13:05:56.123: W/com.mongodb(6069): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-12 13:05:56.123: W/com.mongodb(6069): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-12 13:05:56.123: W/com.mongodb(6069): at java.lang.Thread.run(Thread.java:856)
11-12 13:05:56.332: D/dalvikvm(6069): GC_CONCURRENT freed 221K, 11% free 3144K/3532K, paused 75ms+73ms, total 198ms
11-12 13:05:56.492: D/dalvikvm(6069): GC_CONCURRENT freed 5K, 7% free 3531K/3772K, paused 73ms+6ms, total 112ms
11-12 13:05:56.492: D/dalvikvm(6069): WAIT_FOR_CONCURRENT_GC blocked 33ms
11-12 13:05:56.732: D/dalvikvm(6069): GC_CONCURRENT freed 5K, 6% free 3935K/4160K, paused 72ms+73ms, total 189ms
11-12 13:05:56.912: D/dalvikvm(6069): GC_CONCURRENT freed 6K, 5% free 4358K/4568K, paused 73ms+5ms, total 120ms
11-12 13:05:56.912: D/dalvikvm(6069): WAIT_FOR_CONCURRENT_GC blocked 27ms
11-12 13:05:57.102: D/dalvikvm(6069): GC_CONCURRENT freed 7K, 5% free 4901K/5108K, paused 72ms+5ms, total 120ms
11-12 13:05:57.102: D/dalvikvm(6069): WAIT_FOR_CONCURRENT_GC blocked 29ms
11-12 13:05:57.362: D/dalvikvm(6069): GC_CONCURRENT freed 10K, 4% free 5638K/5832K, paused 72ms+5ms, total 121ms
11-12 13:05:57.362: D/dalvikvm(6069): WAIT_FOR_CONCURRENT_GC blocked 27ms
11-12 13:05:57.752: D/dalvikvm(6069): GC_CONCURRENT freed 13K, 3% free 6621K/6816K, paused 74ms+75ms, total 205ms
11-12 13:05:58.232: D/dalvikvm(6069): GC_CONCURRENT freed 18K, 2% free 7957K/8116K, paused 74ms+79ms, total 213ms
11-12 13:05:58.232: D/dalvikvm(6069): WAIT_FOR_CONCURRENT_GC blocked 11ms
11-12 13:05:58.792: W/System.err(6069): com.mongodb.MongoException$Network: can't say something
11-12 13:05:58.804: W/System.err(6069): at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:194)
11-12 13:05:58.812: W/System.err(6069): at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
11-12 13:05:58.812: W/System.err(6069): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
11-12 13:05:58.823: W/System.err(6069): at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
11-12 13:05:58.832: W/System.err(6069): at com.mongodb.DBCollection.insert(DBCollection.java:75)
11-12 13:05:58.842: W/System.err(6069): at com.mongodb.DBCollection.insert(DBCollection.java:59)
11-12 13:05:58.852: W/System.err(6069): at com.mongodb.DBCollection.insert(DBCollection.java:104)
11-12 13:05:58.852: W/System.err(6069): at com.example.cobanih.MongoConnection.insert(MongoConnection.java:71)
11-12 13:05:58.862: W/System.err(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:37)
11-12 13:05:58.872: W/System.err(6069): at com.example.cobanih.MongoConnection$LongOperation.doInBackground(MongoConnection.java:1)
11-12 13:05:58.882: W/System.err(6069): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-12 13:05:58.892: W/System.err(6069): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-12 13:05:58.902: W/System.err(6069): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-12 13:05:58.902: W/System.err(6069): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-12 13:05:58.922: W/System.err(6069): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-12 13:05:58.922: W/System.err(6069): at java.lang.Thread.run(Thread.java:856)
11-12 13:05:58.942: W/System.err(6069): Caused by: java.io.IOException: couldn't connect to [localhost/127.0.0.1:27017] bc:java.net.SocketException: socket failed: EACCES (Permission denied)
11-12 13:05:58.952: W/System.err(6069): at com.mongodb.DBPort._open(DBPort.java:214)
11-12 13:05:58.962: W/System.err(6069): at com.mongodb.DBPort.go(DBPort.java:107)
11-12 13:05:58.962: W/System.err(6069): at com.mongodb.DBPort.go(DBPort.java:84)
11-12 13:05:58.982: W/System.err(6069): at com.mongodb.DBPort.say(DBPort.java:79)
11-12 13:05:58.982: W/System.err(6069): at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:181)
11-12 13:05:58.982: W/System.err(6069): ... 15 more
这有什么问题我调试时没有错误..提前谢谢:)