java program to print values of oracle database table

Posted on 2012-08-23
Last Modified: 2012-08-27
I am using following program to select and print values of oracle database table values into console. This simple program working fine and fetching and printing values in console. I have connection details like URL, username and password in a property file( in package abc)which is being referenced using a java constants file(say I wonder how to modify my java program to refer the database connection properties in a property file in different package.
import java.sql.*;
import java.util.Calendar;

public class Ri {
    public static void main(String[] args) throws SQLException {

        try {
        } catch ( Exception e ) {
            throw new SQLException("Oracle JDBC is not available",e);

        String url = "jdbc:oracle:thin:@xxxxxx83u:1543:COOLDEV";
        Connection conn = DriverManager.getConnection(url,"cooluser","coolpwd");

        String sql = "SELECT COL_1,COL_15 FROM cooltable where USER_KEY_10='kkkkk' and USER_KEY_1='6666666666'";
        PreparedStatement stmt = conn.prepareStatement(sql);

        ResultSet rs = stmt.executeQuery();
        while( ) {
            System.out.println(rs.getString("USER_KEY_1")+" , "+rs.getString("USER_KEY_10"));
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
Question by:gudii9
    LVL 86

    Accepted Solution

    Not 100% sure what you're asking for, but if your properties file is in package 'ghi' and is called '' then you can load it with

    Properties p = new Properties();
    InputStream in = Ri.class.getResourceAsStream("/ghi/");

    Open in new window

    LVL 20

    Assisted Solution

    by:Amitkumar Panchal
    Agree with CEHJ..  adding further..

    After loading the properties from property file..... you can use the values as given in the below code.

    String driver = p.getProperty("jdbc.driver");
    if (driver != null) {
           Class.forName(driver) ;
    String url = p.getProperty("jdbc.url");
    String username = p.getProperty("jdbc.username");
    String password = p.getProperty("jdbc.password");
    Connection con = DriverManager.getConnection(url, username, password);

    Open in new window

    LVL 20

    Expert Comment

    by:Amitkumar Panchal
    Property file should contain following text..


    Open in new window

    LVL 20

    Expert Comment

    by:Amitkumar Panchal
    Property file, i mean "" as per the snippet provided by CEHJ.
    LVL 7

    Author Comment

    proeprty file is under folder called config within same project. Do I need to include that into classpath. (right click on project---build path--etc). I am not able to get reference to property file(which has datasource connection details and sqls) as well as java constant file which connects those SQLs inside property files. Please advise
    LVL 86

    Expert Comment

    Do I need to include that into classpath.
    You shouldn't need to, since it should already be a folder under your package root

    InputStream in = Ri.class.getResourceAsStream("/config/");

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now