Link to home
Start Free TrialLog in
Avatar of chalie001
chalie001

asked on

error when using method calling db connection

hi am trying to connect to my db in java am having this error
User generated imagemy db is mongo
this is the code
public DBObject findByUsername(String username){
		 // DBCollection dbCollection = mongo.getDB("system").getCollection("users");
		  DBCollection dbCollection =  ConnectToDB.getDB("system").getCollection("users");
		  DBObject criteria = new BasicDBObject("username", username);
		  return dbCollection.findOne(criteria); 
		}

Open in new window


my connection class is
public class ConnectToDB {
      
	private DB db = null;
	private Mongo mongoClient = null;
	private Properties properties = new Properties();
        private InputStream input = null;
        
	public ConnectToDB() {
	
		getDB();
		
	}
	
	
	private boolean connectToDB() throws Exception{
		input = new FileInputStream("dsnApp.properties");
                properties.load(input);
                
		  this.mongoClient = new Mongo(properties.getProperty("mongoDBipAddress"), Integer.valueOf(properties.getProperty("mongoDBport")));
                  
		  this.db = mongoClient.getDB("dsn");
		  		  
		  if(db != null)
		  {
			  return true;
		  }
		     
		 return false;
	}
	
	
	public DB getDB() {
		
		try {
			if(connectToDB())
			{
				return db;
			}
			else
			{
				connectToDB();
				return db;
			}
		} catch (Exception e) {
			
			System.out.println("Something went wrong while trying to connect to the database..");
			e.printStackTrace();
			return null;
		}
	}
  
}

Open in new window

Avatar of sachiek
sachiek
Flag of Singapore image

I guess there is no DB named "System".

Local is the default DB available.
ASKER CERTIFIED SOLUTION
Avatar of sachiek
sachiek
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chalie001
chalie001

ASKER

i don't what to user had coded value MongoClient mongo = new MongoClient("localhost", 27017); i what to use property file which am geting from  connectToDB