0

The program two.java is compiling but no output is being produced ,no exception occuring .

(Executing in cmd)

//db.properties

driverclass = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@loacalhost:1521:xe
user = system
password = kapil

//ConnectionProvider.java

class ConnectionProvider
{
    static Properties prop;

    static
    {
        prop = new Properties();
        String path = File.separator + "db.properties";
        InputStream in = prop.getClass().getResourceAsStream(path);
        try
        {
            prop.load(in);
        }
        catch(Exception e)
        {
        }
    }

    public static Connection getConnection() throws Exception
    {

        Class.forName(prop.getProperty("driverclass"));
        Connection con = DriverManager.getConnection(
                prop.getProperty("url"),
                prop.getProperty("user"),
                prop.getProperty("password"));

        return con;
    }
}

// two.java
class Two
{
    public static void main(String args[])
    {
        try
        {
            Connection con = ConnectionProvider.getConnection();
            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("Select * from Emp ");
            while(rset.next())
            {
                System.out.println(rset.getInt(1) + "\t"
                        + rset.getString(2) + "\t"
                        + rset.getString(3) + "\t"
                        + rset.getInt(4));
            }
            con.close();
        }
        catch(Exception e){}
    }
}
2
  • 1
    Exception is not occurred because you are catching it but not printing it. In other words, exception might occur but you are not printing it. e.printStackTrace(); Commented Jul 18, 2014 at 18:14
  • You misspelled localhost. Commented Jul 20, 2014 at 7:09

3 Answers 3

1

First thing dont consume the exception by doing this catch(Exception e){} Its not a good practice always print the stacktrace like the catch(Exception e){ e.printStacktrace();} now the problem in ur code is the url change it to-

url = jdbc:oracle:thin:@localhost:1521:xe

there is a typo in url's localhost.

Edit :As you are executing the class through cmd I expect that the classes and the db.properties are in same folder try something like this

try {
          prop.load(new FileInputStream("db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }

so the full ConnectionProvider class looks something like this

class ConnectionProvider
{
    static Properties prop;
    static
    {
        prop = new Properties();
        try {
            prop.load(new FileInputStream("db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws Exception
    {

        Class.forName(prop.getProperty("driverclass"));
        Connection con = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("user"), prop.getProperty("password"));

        return con;
    }
}

Now while executing the class dont forget to include the ojdbc6.jar in your classpath.You can get it from here.

Sign up to request clarification or add additional context in comments.

2 Comments

still nothing is happening.
Its not prop.getClass() it should be ConnectionProvider.getClass().getClassLoader.getResourseAsStream(path); keep ur properties file in src/main/resources
0

Exception is not occurred because you are catching it but not printing it. In other words, exception might occur but you are not printing it.

add this to your catch block e.printStackTrace();

Also this code:
String path = File.separator + "db.properties";
doesn't seem correct. First print out the path to make sure you are pointing at correct file. You can use .getCanonicalPath() to get absolute path.

Do something like this:

            String filePath = new File("./yourfile.properties").getCanonicalPath();
            FileInputStream fis = new FileInputStream(filePath);
            props.load(fis);

Comments

0

You can use BalusC's great dao layer tutorial. There is an properties file loader that will fit your needs. Summary of the code is below.

This is how the file dao.properties will look like (change jdbc.url and jdbc.driver for Oracle Db):

javabase.jdbc.url = jdbc:mysql://localhost:3306/javabase
javabase.jdbc.driver = com.mysql.jdbc.Driver
javabase.jdbc.username = java
javabase.jdbc.password = d$7hF_r!9Y

Properties file loader (as noted you may change it depending on your needs, Note: it depends on how often you think that this file changes in your environment, if it changes only once per year, then it is really not worth that to load it from disk everytime, but if it changes for example every day, then it might be worth to add a static method which reloads the properties file and execute it by some (scheduled) background job.)

package com.example.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * This class immediately loads the DAO properties file 'dao.properties' once in memory and provides
 * a constructor which takes the specific key which is to be used as property key prefix of the DAO
 * properties file. There is a property getter which only returns the property prefixed with
 * 'specificKey.' and provides the option to indicate whether the property is mandatory or not.
 *
 * @author BalusC
 * @link http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html
 */
public class DAOProperties {

    // Constants ----------------------------------------------------------------------------------

    private static final String PROPERTIES_FILE = "dao.properties";
    private static final Properties PROPERTIES = new Properties();

    static {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        InputStream propertiesFile = classLoader.getResourceAsStream(PROPERTIES_FILE);

        if (propertiesFile == null) {
            throw new DAOConfigurationException(
                "Properties file '" + PROPERTIES_FILE + "' is missing in classpath.");
        }

        try {
            PROPERTIES.load(propertiesFile);
        } catch (IOException e) {
            throw new DAOConfigurationException(
                "Cannot load properties file '" + PROPERTIES_FILE + "'.", e);
        }
    }

    // Vars ---------------------------------------------------------------------------------------

    private String specificKey;

    // Constructors -------------------------------------------------------------------------------

    /**
     * Construct a DAOProperties instance for the given specific key which is to be used as property
     * key prefix of the DAO properties file.
     * @param specificKey The specific key which is to be used as property key prefix.
     * @throws DAOConfigurationException During class initialization if the DAO properties file is
     * missing in the classpath or cannot be loaded.
     */
    public DAOProperties(String specificKey) throws DAOConfigurationException {
        this.specificKey = specificKey;
    }

    // Actions ------------------------------------------------------------------------------------

    /**
     * Returns the DAOProperties instance specific property value associated with the given key with
     * the option to indicate whether the property is mandatory or not.
     * @param key The key to be associated with a DAOProperties instance specific value.
     * @param mandatory Sets whether the returned property value should not be null nor empty.
     * @return The DAOProperties instance specific property value associated with the given key.
     * @throws DAOConfigurationException If the returned property value is null or empty while
     * it is mandatory.
     */
    public String getProperty(String key, boolean mandatory) throws DAOConfigurationException {
        String fullKey = specificKey + "." + key;
        String property = PROPERTIES.getProperty(fullKey);

        if (property == null || property.trim().length() == 0) {
            if (mandatory) {
                throw new DAOConfigurationException("Required property '" + fullKey + "'"
                    + " is missing in properties file '" + PROPERTIES_FILE + "'.");
            } else {
                // Make empty value null. Empty Strings are evil.
                property = null;
            }
        }

        return property;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.