chalie001
asked on
error when using method calling db connection
hi am trying to connect to my db in java am having this error
my db is mongo
this is the code
my connection class is
public class ConnectToDB {
my 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);
}
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;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Local is the default DB available.