Solved

how to make sure that my connection to database works?

Posted on 2007-04-09
1
239 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to connect SQL Server from my Oracle database? 11 73
java stored proc example 9 24
servlet example 11 19
Distinct values from all columns in a table?? PL SQL 4 26
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 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

18 Experts available now in Live!

Get 1:1 Help Now