Solved

how to make sure that my connection to database works?

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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 practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 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