Solved

how to make sure that my connection to database works?

Posted on 2007-04-09
1
236 Views
Last Modified: 2013-12-18
how to make sure that the connection string to my orical data base is correct.
this is my conection code in java

 // Database Connection object
    Connection conn = null;
 
    // Set database URL details.
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    try {
     
      // Instantiate the driver class.
      Class.forName("oracle.jdbc.OracleDriver");
     
      // Get the database connection.
      conn = DriverManager.getConnection(url,"system","pass");
    }catch (Exception ex) {
        System.out.println("Exception while getting database connection: "
                           +ex.toString() );

can any one help me to write a select statment to make sure that it goes to my database???

I have this table created:
SQL> CREATE TABLE product_information(PRODUCT_ID           NUMBER(6)      NOT NULL,                                      PRODUCT_NAME         VARCHAR2(50)   NOT NULL,                                      PRODUCT_DESCRIPTION  VARCHAR2(2000) NOT NULL,                                       MIN_PRICE            NUMBER(8,2),                                      LIST_PRICE           NUMBER(8,2));SQL> INSERT INTO product_information VALUES(2400, 'DIMM - 512 MB',         '512 MB DIMM memory. Improved memory upgrade granularity', 380, 448);SQL> INSERT INTO product_information VALUES(2401, 'DIMM - 64MB',        'Memory DIMM: RAM, 64MB (100-MHz Unregistered ECC SDRAM)', 202, 247);SQL> INSERT INTO product_information VALUES(2402, 'EDO - 32MB',        'Memory EDO SIM: RAM, 32 MB (100-MHz Unregistered ECC SDRAM)', 149, 179);
0
Comment
Question by:fm134
1 Comment
 
LVL 10

Accepted Solution

by:
Jaax earned 500 total points
ID: 18874745
You could code like this after securing the connection
conn = DriverManager.getConnection(url,"system","pass");
PreparedStatement prep = conn.prepareStatement("select * from product_information");
ResultSet rs = prep.executeQuery();
//Iteratore over the resultset


prep = conn.prepareStatement("select sysdate from dual");
rs = prep.executeQuery();
while(rs.next()){
      System.out.println("Date : "+rs.getDate("sysdate"));

}

OR querying to dual to check DB connection

PreparedStatement prep = conn.prepareStatement("select sysdate from dual");
ResultSet rs = prep.executeQuery();
while(rs.next()){
      System.out.println("Date : "+rs.getDate("sysdate"));

}



0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
The viewer will learn how to implement Singleton Design Pattern in Java.

746 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

13 Experts available now in Live!

Get 1:1 Help Now