Solved

JDBC connection problem ...

Posted on 1998-04-28
6
299 Views
Last Modified: 2010-04-16
I am trying to connect to an Oracle database located on a hp box and my development is
 primarily on an NT box. I am using the function getconnection to access the database in the following fashion.
conn = DriverManager.getConnection ("jdbc:oracle:dnldthin:@myhost:3202:isdev11", "userid", "password");
Is there something that I am missing????



 
0
Comment
Question by:aswinmajj
  • 3
  • 2
6 Comments
 

Expert Comment

by:mka
ID: 1220109
Hi,

A lot depends upon how your Oracle is configured. Hoping everything is fine there and you have the Oracle driver classes, look at the sample below. Compare what you have in your program as against my sample.

/*
 * This sample applet just selects 'Hello World' and the date from the database
 */

// Import the JDBC classes
import java.sql.*;

// Import the java classes used in applets
import java.awt.*;
import java.io.*;
import java.util.*;

public class JdbcApplet extends java.applet.Applet
{
  // The driver to load
  static final String driver_class = "oracle.jdbc.driver.OracleDriver";

  // The connect string
  static final String connect_string =
                  "jdbc:oracle:thin:scott/tiger@dlsun511:1721:dbms733";

  // This is the kind of string you woudl use if going through the
  // Oracle 8 connection manager which lets you run the database on a
  // different host than the Web Server.  See the on-line documentation
  // for more information.
  // static final String connect_string = "jdbc:oracle:thin:scott/tiger@(description=(address_list=(address=(protocol=tcp)(host=dlsun511)(port=1610))(address=(protocol=tcp)(host=pkrishna-pc2)(port=1521)))(source_route=yes)(connect_data=(sid=orcl)))";

  // The query we will execute
  static final String query = "select 'Hello JDBC: ' || sysdate from dual";
 

  // The button to push for executing the query
  Button execute_button;

  // The place where to dump the query result
  TextArea output;

  // The connection to the database
  Connection conn;

  // Create the User Interface
  public void init ()
  {
    this.setLayout (new BorderLayout ());
    Panel p = new Panel ();
    p.setLayout (new FlowLayout (FlowLayout.LEFT));
    execute_button = new Button ("Hello JDBC");
    p.add (execute_button);
    this.add ("North", p);
    output = new TextArea (10, 60);
    this.add ("Center", output);
  }

  // Do the work
  public boolean action (Event ev, Object arg)
  {
    if (ev.target == execute_button)
    {
      try
      {
      // Clear the output area
      output.setText (null);

      // See if we need to open the connection to the database
      if (conn == null)
      {
        // Load the JDBC driver
        output.appendText ("Loading JDBC driver " + driver_class + "\n");
        Class.forName (driver_class);

        // Connect to the databse
        output.appendText ("Connecting to " + connect_string + "\n");
        conn = DriverManager.getConnection (connect_string);
        output.appendText ("Connected\n");
      }

      // Create a statement
      Statement stmt = conn.createStatement ();

      // Execute the query
      output.appendText ("Executing query " + query + "\n");
      ResultSet rset = stmt.executeQuery (query);

      // Dump the result
      while (rset.next ())
        output.appendText (rset.getString (1) + "\n");

      // We're done
      output.appendText ("done.\n");
      }
      catch (Exception e)
      {
      // Oops
      output.appendText (e.getMessage () + "\n");
      }
      return true;
    }
    else
      return false;
  }
}

The program is 'over-systematic', but it is written like that for a purpose. I couldn't write one to suit you, though this should be enough.

Thanks.
Kaleem.
0
 

Author Comment

by:aswinmajj
ID: 1220110
Can u tell me how u'r oracle should be configured??? From what I understand I can access the oracle database from the unix command prompt. Also do I need the webserver to run on the hp machine??

Thanks
0
 

Expert Comment

by:mka
ID: 1220111
Hi,

You'll need the Oracle webserver only in case you are to execute your program as an applet on the browser -- about which you have made no mention in your question. I do not know anything about your configuration of Oracle - like your installation is a Oracle Server or you have Oracle Server running as a host machine on the network etc etc. When you can access it from the command prompt -- the only thing that could be troubling your connection from a Java program could very well be the driver classes for Oracle. Double check your code for the presence of  a statements like the below:
// The driver to load
   static final String driver_class = "oracle.jdbc.driver.OracleDriver";

and:
  // Load the JDBC driver
   output.appendText ("Loading JDBC driver " + driver_class + "\n");
   Class.forName (driver_class);  // This loads the driver.

Also check whether you really posses oracle.jdbc.driver.OracleDriver in one of you .jar or .zip's in your classpath. Without any of these, an Oracle database connection through Java cannot through.

Since you mentioned Win NT, you can use WinZip to see the contents of .zip and .jar files -- and look in .zip files in the directory jdbc or something like that; and file names like classes111.zip, etc.

If you don't have the driver classes; you'll have to procure them.

Thanks.
Kaleem.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:evijay
ID: 1220112
Hi,

Checkup whether the tnslsnr is running and tnsnames.ora is having all the correct details. Tell me one thing. Are you able to access oracle from windows client using tns ? If you are not then your jdbc driver will fail too. First checkup whether you are able to access oracle thru tns.

The following things can be done
In the unix machine check up whether a process by name
tnslsnr is running or not using ps command. If it is not running run it. Before doing this, configure tnsnames.ora to have proper value (in solaris it is in /var/opt/oracle).

try the following command
sqlplus userid/password@isdev11   (isdev11 is your tns name)
if it succeeds then your unix part is ok

Next step is go to nt part. you must have oracle client installed in the system. use tnsneteasy config to configure the tns names. suppose your tns alias is comdev then try giving
sqlplus userid/password@comdev. if connection succeeds then everything is fine in both nt and unix. Next try your program. If it still doesnt work send me a comment.



0
 

Author Comment

by:aswinmajj
ID: 1220113
Let me put down the scenario again in front of u guys:
1) I can perform sqlplus from the command prompt in unix
2) Downloaded the oracle driver
3) The code looks like the following
import java.sql.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class JdbcApplet extends java.applet.Applet{
  static final String driver_class = "oracle.jdbc.dnlddriver.OracleDriver";  
  string static final
 public void init ()
  {
    this.setLayout (new BorderLayout ());
 
   Panel p = new Panel ();
 
  p.setLayout (new FlowLayout (FlowLayout.LEFT));
 
   execute_button = new Button ("Hello JDBC");
 
  p.add (execute_button);
   
 this.add ("North", p);
 
  output = new TextArea (10, 60);
   
 this.add ("Center", output);
  }
public boolean action (Event ev, Object arg)
  {
   
 if (ev.target == execute_button)
 
  {
      try
      {
      // Clear the output area
      output.setText (null);

      
// See if we need to open the connection to the database
      
if (conn == null)
      {
        // Load the JDBC driver
                  output.appendText ("Beyond class" + driver_class + "\n");
        output.appendText ("Loading JDBC driver " + driver_class + "\n");
        Class.forName(driver_class);
      
  output.appendText ("Beyond class" + driver_class + "\n");
      
  // Connect to the database
      
 output.appendText ("Connecting to " + connect_string + "\n");
      
  conn = DriverManager.getConnection("jdbc:oracle:dnldthin:@myhost:isdev11", "userid", "password");
      
  output.appendText ("Connected\n");
      }


4) I don't have a httpd server running on the Unix box ...

THanks

-- aswin

0
 

Accepted Solution

by:
mka earned 50 total points
ID: 1220114
Hi,

Now you are speaking stuff. Without the code about we'd be trying all combinations of replies here. Thanks for giving your code.

The problem is with the line:
conn = DriverManager.getConnection("jdbc:oracle:dnldthin:@myhost:isdev11", "userid", "password");

What is "isdev11"? Since you are working on HP, it appears you are using UNIX 'dev' term to denote the UNIX terminal. (Tell me if I am wrong.)

Check whether "isdev11" is a valid database name in Oracle. Your understanding of JDBC appears to be that connection is made with the terminal or client or network machine. No, instead the connection is made with a database instance in the Oracle database. A database connection then allows you to access all the tables and views in the database.

Besides that, your code seems to be okay to me.

If you are executing from appletviewer, then you should get the database connection immediately. However, if you are trying to execute the applet through a browser, you'll be in pain till you get a database server like Oracle server.

Thanks.
Kaleem.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

758 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

19 Experts available now in Live!

Get 1:1 Help Now