Friday 9 November 2012

sample mongodb

http://city81.blogspot.co.uk/2012/07/using-spring-data-to-access-mongodb.html


SpringMongoConfig.java



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.config.AbstractMongoConfiguration;
import com.mongodb.Mongo;


@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {

@Override
public @Bean Mongo mongo() throws Exception {

return new Mongo("localhost");
}

@Override
public @Bean MongoTemplate mongoTemplate() throws Exception {

return new MongoTemplate(mongo(),"blog","users");
}
}



MongoApp.java (Main)


import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;

import com.mongodb.DB;

import com.mongodb.DBCollection;

import com.mongodb.DBCursor;

import com.mongodb.Mongo;

import com.mongodb.MongoException;

public class MongoApp {
public static void main(String[] args) throws UnknownHostException {
try {
// connect to mongoDB
Mongo mongo = new Mongo("localhost", 27017);
// if database doesn't exists, mongoDB will create it
DB db = mongo.getDB("blog");
// Get collection from MongoDB, database named "mydb"
// if collection doesn't exists, mongoDB will create it
DBCollection collection = db.getCollection("users");
// create a document to store attributes
BasicDBObject document = new BasicDBObject();
document.get("_id");
// save it into collection named "myCollection"
System.out.println( collection.find());


DBCursor cur = collection.find();
while(cur.hasNext()) {
    System.out.println(cur.next());
}
// search query


} catch (MongoException e) {

e.printStackTrace();

}

}

}


User.java


public class User {
private String id;
private String password;

public User(){};
public User(String id, String password) {
super();
this.id = id;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}


/**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
public String toString() {
return "User [id=" + id + ", firstname=" + password"]";
}
}




pom.xml


Just change the groupID by your package name



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>SpringExample</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>SpringMongoExample</name>
<url>http://maven.apache.org</url>

<repositories>
<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>http://maven.springframework.org/milestone</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>

<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.5.2</version>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.0.0.M2</version>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

</project>





Thursday 8 November 2012

A new site for tutorial on Java ...



Today I was browsing the http://java-champions.java.net/

I was going through one of the Java champion Lars Vogel is the founder and CEO of vogella GmbH


He has got very good tutorials for Java Based, Android based


http://www.vogella.com/tutorials.html

Hope this helps someone ...

Wednesday 31 October 2012

Free Online MongoDB(NOSQL) Training ...

Any one want to do a free course in MongoDB


Sign up then


https://education.10gen.com/dashboard

Thursday 18 October 2012

HSBC.co.uk... down ...

Frankly it shocks me to see one of UK largest main website being down for nearly 12 hours.

I was not able to access HSBC personal banking website from yesterday(18/10/2012) evening 5 PM.



Attaching the screen shot





Frankly the banks should pay consultants (like me :p) to have a proper solution .... Instead of loosing money employing dummers and duffers .... ;)

Tuesday 2 October 2012

Mac Time Machine Backup


In case you Mac TimeMachine Takes long ... After Mountain Lion the backup takes long ... I think there is a bug in spotlight.

How to solve it ?

Technically stop the spotlight using the command ...

Open up a terminal

> sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

Now the Backup will be faster.

Once done enable the spotlight back

> sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

My backup took very long ... got this solution and finished my back up.

Hope this helps ...

Sunday 23 September 2012

My First Post on Technology

Hi

Everyone, this is going to be my main blog for technology update, problems i face and solutions which is used by me to solve the technology problems i face.

Hope you all Join Muy Blog, Like share in Google plus, FaceBook & Twitter ...

Come lets have some fun (in technology)  ...