Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

java program to print values of oracle database table

Posted on 2012-08-23
6
Medium Priority
?
811 Views
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(xyz.properties in package abc)which is being referenced using a java constants file(say def.java). 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 {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } 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( rs.next() ) {
            System.out.println(rs.getString("USER_KEY_1")+" , "+rs.getString("USER_KEY_10"));
        }
        rs.close();
        stmt.close();
        conn.close();
    }
}
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 38326799
Not 100% sure what you're asking for, but if your properties file is in package 'ghi' and is called 'db.properties' then you can load it with

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

Open in new window

0
 
LVL 21

Assisted Solution

by:Amitkumar P
Amitkumar P earned 1000 total points
ID: 38328522
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

0
 
LVL 21

Expert Comment

by:Amitkumar P
ID: 38328527
Property file should contain following text..

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@xxxxxx83u:1543:COOLDEV
jdbc.username=cooluser
jdbc.password=coolpwd

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 21

Expert Comment

by:Amitkumar P
ID: 38328530
Property file, i mean "db.properties" as per the snippet provided by CEHJ.
0
 
LVL 7

Author Comment

by:gudii9
ID: 38329417
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38329431
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/db.properties");

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

581 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