views:

180

answers:

3

Here's the class

package db;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Oshadha Gunawardena
 */
public class DBFacade {

    private static Connection c;

    public static void connect() throws Exception {
        if (c == null) {
            Properties prop = new Properties();
            FileInputStream fis = new FileInputStream("props.xml");
            prop.loadFromXML(fis);

            String dbUrl = prop.getProperty("dburl");
            String dbDriver = prop.getProperty("dbdriver");
            String dbUser = prop.getProperty("username");
            String dbPass = prop.getProperty("password");

            Class.forName(dbDriver).newInstance();

            c = DriverManager.getConnection(dbUrl, dbUser, dbPass);
        }
    }

    public static ResultSet fetch(String sql) throws Exception {
        connect();
        synchronized (c) {
            return c.createStatement().executeQuery(sql);
        }
    }

    public static void save(String sql) throws Exception {
        connect();
        synchronized (c) {
            c.createStatement().executeUpdate(sql);
        }
    }
}

I'm using this class as my database facade class,so my entire project is a web application I'm calling this fetch and save methods using a servlet, but when I try to run this It throws an exception (java.io.FileNotFoundException). All the paths are set correctly props.xml file is in my project home directory also it works when I try to print the data to the out put.

String dbUrl = prop.getProperty("dburl")
System.out.println(dbUrl);

Problem only occurs when I try to deploy and run the project. Note: I'm using NetBeans 6.1 as my primary IDE.

Thanks

+4  A: 

If you are loading from war file , you should be using

getClass().getResourceAsStream("props.xml");
J-16 SDiZ
Can you explain bit more
MMRUser
This method is not working
MMRUser
The file should be in the same package as your class.
boutta
+2  A: 

As J-16 SDiZ says, your file is probably ending up in the war file - or if it's not, then your working directory probably isn't what you think it is.

You say that props.xml is in the "project home directory" - where is it after deployment? Is it in a war file (in which case you'll need to use getResourceAsStream(), although I suspect that Class.getClassLoader().getResourceAsStream("props.xml") is more likely to work:

InputStream input = null;  
try
{
    input YourClassName.class.getClassLoader.getResourceAsStream("props.xml");
    if (input == null)
    {
         // Throw an appropriate exception here to show you can't find your file
    }
    prop.loadFromXML(input);
}
finally
{
    if (input != null)
    {
        input.close();
    }
}

If it really is a file, you'll need to find some way of working out the directory it's in programmatically, then use:

File file = new File(directory, "props.xml");
FileInputStream fis = new FileInputStream(file);
// And close it in a finally block as above
Jon Skeet
This gives me an nullpoin Exception
MMRUser
Where? What's null? If it's input, then you haven't follow my comment about throwing an appropriate exception. If it's prop, then you haven't created your Properties object first.
Jon Skeet
It worked, problem occurs because I didn't add the properties file in the classpath. Thanks for the help
MMRUser
A: 

If you used the getResourceAsStream method, then your property file must be on the classpath. See the documentation for your servlet container to see what it sets the classpath to.

However, since we are talking about JDBC connections, this isn't the right approach to use. The preferred mechanism for obtaining connections is using a DataSource.

Instructions for configuring a DataSource in Tomcat can be found here

Using a DataSource object is the preferred alternative to using the DriverManager for establishing a connection to a data source. They are similar to the extent that the DriverManager class and DataSource interface both have methods for creating a connection, methods for getting and setting a timeout limit for making a connection, and methods for getting and setting a stream for logging.

Their differences are more significant than their similarities, however. Unlike the DriverManager, a DataSource object has properties that identify and describe the data source it represents. Also, a DataSource object works with a JavaTM Naming and Directory InterfaceTM (JNDI) naming service and is created, deployed, and managed separately from the applications that use it. A driver vendor will provide a class that is a basic implementation of the DataSource interface as part of its JDBC 2.0 or 3.0 driver product. What a system administrator does to register a DataSource object with a JNDI naming service and what an application does to get a connection to a data source using a DataSource object registered with a JNDI naming service are described later in this chapter.

Being registered with a JNDI naming service gives a DataSource object two major advantages over the DriverManager. First, an application does not need to hardcode driver information, as it does with the DriverManager. A programmer can choose a logical name for the data source and register the logical name with a JNDI naming service. The application uses the logical name, and the JNDI naming service will supply the DataSource object associated with the logical name. The DataSource object can then be used to create a connection to the data source it represents.

The second major advantage is that the DataSource facility allows developers to implement a DataSource class to take advantage of features like connection pooling and distributed transactions. Connection pooling can increase performance dramatically by reusing connections rather than creating a new physical connection each time a connection is requested. The ability to use distributed transactions enables an application to do the heavy duty database work of large enterprises.

toolkit