Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java_server thread: Re: [Microsoft][ODBC Driver Manager] Data source name not found - jkd1.3/jdbc3.0/NT/oracle8i


Message #1 by "Sanjeev Kumar" <sanjeevkumar1000@h...> on Wed, 02 May 2001 10:37:28 -0000
Dear Frant,
      Pls select the Microsoft odbc driver for oracle and create the Dsn
Suppose the dsn is shr

import java.io.*;
import java.sql.*;
public class shr
{
  public static void main(String args[])
  {
    String a = new String();
    ResultSet rs = null;
    try
    {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection con = 
DriverManager.getConnection("jdbc:odbc:shr","scott","tiger");
      Statement stat = con.createStatement();
      rs = stat.executeQuery("Select * from emp" );
        while(rs.next())
        {
           a = rs.getString(1);
           System.out.println(a);

        }
    }

    catch(Exception e)
    {
      System.out.println("An exception has occurred" +e);
    }
  }

}


thanks
sanjeev
>From: "frant turner" <seasundown@a...>
>Reply-To: "Java Server" <pro_java_server@p...>
>To: "Java Server" <pro_java_server@p...>
>Subject: [pro_java_server] [Microsoft][ODBC Driver Manager] Data source 
>name not found - jkd1.3/jdbc3.0/NT/oracle8i
>Date: Mon, 30 Apr 2001 06:47:10
>
>My ODBC installation is OK but jdbc doesn't work.
>
>Should I scrap it with the Sun driver & use
>the Oracle light driver instead?
>
>Firstly, does my JDBC:ODBC url string appear
>accurate?  The connection string at the tail
>end of jdbc:odbc connection url string is identicle
>to the one created and used to connect succesfully
>with odbctest tool for Oracle8i.
>
>Does my classpath look functional?
>
>ODBC connectivity with Oracle8i odbctest tool
>indicates  ODBC is installed ok.  But jdbc:odbc
>bridge does not appear to be working.  What
>follows is the: error, java test file, &
>instructions used in case there in a glaring
>error in that process.
>
>Any help - Please for this or other driver
>instructions ( which work) to get this or
>similar test code functional.
>
>TIA!
>----------------------------------------------------
>error from java code...
>---------------------------------------------------
>
>class found
>now try connection con...
>
>*** SQLException caught ***
>
>SQLState: IM002
>Message:  [Microsoft][ODBC Driver Manager] Data source name not found and
>no default driver specified   Vendor:   0
>
>-------------------------------------------------
>test code in error...
>-------------------------------------------------
>
>
>//-----------------------------------------------
>//
>// Module:  simpleselect.java
>//
>// Description: Test program for ODBC API interface.  This java application
>//      will connection to a JDBC driver, issue a select statement
>//      and display all result columns and rows
>//
>// Product: JDBC to ODBC Bridge
>//
>// Author:  Karl Moss
>//
>// Date:    February, 1996
>//
>// Copyright:   1990-1996 INTERSOLV, Inc.
>//      This software contains confidential and proprietary
>//      information of INTERSOLV, Inc.
>//-------------------------------------------------------------------------
>---
>
>import java.net.URL;
>import java.sql.*;
>
>class simpleselect {
>
>     public static void main (String args[]) {
>
>         //String url   = "jdbc:odbc:my-dsn";
>         // .mdb only refers to msoft Access DSNs
>         // String url   = "jdbc:odbc://cse-ferg41.unl.edu/JavaData.mdb";
>
>String url = "jdbc:odbc://testhost.testcompany.org/OracleODBCDriverDSN";
>         String query = "SELECT count(*) FROM scott.emp";
>//        String query = "SELECT count(*) FROM Cases";
>
>         try {
>
>             // Load the jdbc-odbc bridge driver
>
>             Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
>
>             // Attempt to connect to a driver.  Each one
>             // of the registered drivers will be loaded until
>             // one is found that can process this URL
>
>             System.err.println("class found");
>
>             System.err.println("now try connection con... ");
>
>             // url, "my-user", "my-passwd");
>   Connection con = DriverManager.getConnection ( url, "system", 
>"manager");
>
>             System.err.println("after connection");
>
>             // If we were unable to connect, an exception
>             // would have been thrown.  So, if we get here,
>             // we are successfully connected to the URL
>
>             // Check for, and display and warnings generated
>             // by the connect.
>
>             checkForWarning (con.getWarnings ());
>
>             // Get the DatabaseMetaData object and display
>             // some information about the connection
>
>             DatabaseMetaData dma = con.getMetaData ();
>
>             System.out.println("\nConnected to " + dma.getURL());
>             System.out.println("Driver       " +
>                 dma.getDriverName());
>             System.out.println("Version      " +
>                 dma.getDriverVersion());
>             System.out.println("");
>
>             // Create a Statement object so we can submit
>             // SQL statements to the driver
>
>             Statement stmt = con.createStatement ();
>
>             // Submit a query, creating a ResultSet object
>
>             ResultSet rs = stmt.executeQuery (query);
>
>             // Display all columns and rows from the result set
>
>             dispResultSet (rs);
>
>             // Close the result set
>
>             rs.close();
>
>             // Close the statement
>
>             stmt.close();
>
>             // Close the connection
>
>             con.close();
>         }
>         catch (SQLException ex) {
>
>             // A SQLException was generated.  Catch it and
>             // display the error information.  Note that there
>             // could be multiple error objects chained
>             // together
>
>             System.out.println ("\n*** SQLException caught ***\n");
>
>             while (ex != null) {
>                 System.out.println ("SQLState: " +
>                     ex.getSQLState ());
>                 System.out.println ("Message:  " +
>                     ex.getMessage ());
>                 System.out.println ("Vendor:   " +
>                     ex.getErrorCode ());
>                 ex = ex.getNextException ();
>                 System.out.println ("");
>             }
>         }
>         catch (java.lang.Exception ex) {
>
>             // Got some other type of exception.  Dump it.
>
>             ex.printStackTrace ();
>         }
>     }
>
>     //-------------------------------------------------------------------
>     // checkForWarning
>     // Checks for and displays warnings.  Returns true if a warning
>     // existed
>     //-------------------------------------------------------------------
>
>     private static boolean checkForWarning (SQLWarning warn)
>             throws SQLException
>     {
>         boolean rc = false;
>
>         // If a SQLWarning object was given, display the
>         // warning messages.  Note that there could be
>         // multiple warnings chained together
>
>         if (warn != null) {
>             System.out.println ("\n *** Warning ***\n");
>             rc = true;
>             while (warn != null) {
>                 System.out.println ("SQLState: " +
>                     warn.getSQLState ());
>                 System.out.println ("Message:  " +
>                     warn.getMessage ());
>                 System.out.println ("Vendor:   " +
>                     warn.getErrorCode ());
>                 System.out.println ("");
>                 warn = warn.getNextWarning ();
>             }
>         }
>         return rc;
>     }
>
>     //-------------------------------------------------------------------
>     // dispResultSet
>     // Displays all columns and rows in the given result set
>     //-------------------------------------------------------------------
>
>     private static void dispResultSet (ResultSet rs)
>         throws SQLException
>     {
>         int i;
>
>         // Get the ResultSetMetaData.  This will be used for
>         // the column headings
>
>         ResultSetMetaData rsmd = rs.getMetaData ();
>
>         // Get the number of columns in the result set
>
>         int numCols = rsmd.getColumnCount ();
>
>         // Display column headings
>
>         for (i=1; i<=numCols; i++) {
>             if (i > 1) System.out.print(",");
>             System.out.print(rsmd.getColumnLabel(i));
>         }
>         System.out.println("");
>
>         // Display data, fetching until end of the result set
>
>         while (rs.next ()) {
>
>             // Loop through each column, getting the
>             // column data and displaying
>
>             for (i=1; i<=numCols; i++) {
>                 if (i > 1) System.out.print(",");
>                 System.out.print(rs.getString(i));
>             }
>             System.out.println("");
>
>             // Fetch the next result set row
>         }
>     }
>------------------------------------------------------
>I downloaded latest 3.0 jdbc package from sun
>and also use jdk1.3.
>
>My classpath  is:
>
>;d:\sybase-install\ASEP\3pclass.zip;d:\sybase-
>install\ASEP\monclass.zip;.;c:\mysql\jdbc\twz1
>\noopt;c:\SDKs\Java\Sun\javacheck;c:\jdk1.3;c:\jdk1.3\src.jar;c:\jdk1.3
>\lib;c:\jdk1.3\lib\dt.jar;c:\jdk1.3\lib\tools.jar;c:\jdk1.3
>\jre\lib;D:\ORANT\orb\classes\yoj.jar;D:\ORANT\orb\classes\share.zip;d:\Ora
>3\orb\classes\yoj.jar;d:\Ora3\orb\classes\share.zip;d:\jdbc-3_0\jdbc-3_0-
>fpd2-classes.zip;c:\gsl.sql.jar
>
>--------------------------------------------------------
>page (minus fluff) & url from whence I located driver
>& instructions which now seem not to work...
>--------------------------------------------------------
>
>How to Use Java and JDBC To Access a Database
>This article focuses on querying an MS Access database, but it contains
>information useful developers wishing to connect to any database.
>
>By Kurt Baumgarten (kurt@c...)
>
>Latest Modification: 1-12-01
>date links last checked : 1-12-01.
>.
>.
>.
>This document describes how to use Java and JDBC(Java Database
>Connectivity) to access a database. It assumes Java is installed on your
>machine.
>.
>.
>
>STEPS REQUIRED TO USE JDBC :
>1. Install JDBC, and the JDBC-ODBC bridge. (They can be obtained at
>http://splash.javasoft.com/jdbc.)
>Note: if you are using any version of the JDK past 1.1, both JDBC and the
>JDBC-ODBC bridge are included.
>
>2. Make the changes to your Java CLASSPATH variable specified in the
>installation instructions for JDBC and JDBC-ODBC.  (Again, no changes are
>needed  to the CLASSPATH if you're using any JDK past 1.1.)
>
>Example path for those using JDK 1.1 and up:
>
>     .;C:\jdk1.1.8\lib\classes.zip;
>
>Example path for Windows NT, using JDK 1.0.2 : (all one line)
>
>     .;%SystemRoot%\java\lib\classes.zip;%SystemRoot%
>\java\Jdbc\jdbc\classes\;
>     %SystemRoot%\java\Jdbc\jdbc-odbc\classes\;
>Note: On Windows NT/2000, you can reset the CLASSPATH variable by opening
>the control panel, selecting "System", and then clicking the the
>Environment tab. You can't do this in windows '95/'98.
>
>Also note: omitting  "." at the start of your path is a common mistake.
>.
>.
>.
>3. Install the Microsoft Desktop Database drivers on your web server.
>These may not be included in a normal Windows NT/2000 or 95/98
>installation. Thus, you might need to do a custom installation. The
>drivers should be under some section of the installation dealing with
>ODBC. Also install anything that relates to enabling ODBC (if it isn't
>already installed).
>
>4. Create your database. (Using, say, MS Access.) Place a copy of it on
>the web server.
>
>5. Register the database as an ODBC database. To do this, go into the
>Control Panel, open up "ODBC", click "Add", click "Microsoft Access
>Driver" (if you're using an Access database), then type in the name of
>your database, and click "Select" to browse for your database. That's all
>you should need to do.
>
>User vs. System DSN's:
>Note that if you want your database to be available regardless of who is
>logged onto your machine, you must register the database as a "System
>DSN".  Click the appropriate tab on the ODBC control panel to add a System
>DSN.  If your machine has only one user,  a "User DSN" is fine, provided
>you remain logged on as that user.
>
>6. Compile and run the sample program. The sample is
>called "SimpleSelect.java" and it is in the JDBC-ODBC directory.   It is
>also available at this site.
>
>To run the sample, start by editing the file by changing the name of the
>database to your database.     The code you need to change looks like
>this: String url = "jdbc:odbc:MyDatabase.mdb";
>
>The "jdbc:odbc:" part indicates that you intend to use the JDBC-ODBC
>bridge to open the database called "MyDatabase.mdb".  Change the name of
>the database to the database you plan to use.  The name of the database
>should exactly match the name you gave to the database when you registered
>it in step 5.
>.
>.
>Once you've amended the sample program, save it, and compile it with the
>command
>
>         javac SimpleSelect.java
>Run it using
>
>         java SimpleSelect
>
>
>The sample program has a routine called "dispResultSet" which simply
>displays, on your terminal, all the rows in your database that satisfy the
>query.  You should see the result of your query print out on the DOS
>console.  You're done!
>
>Error messages and Their Possible Causes:
>
>Data Source Not Found
>If you get an error similar to "datasource not found", this means that
>either your database is improperly registered(or not registered) through
>ODBC, or the name you gave your database in the code  is not exactly the
>same as the name you gave it when you registered it.  Another possible
>source of this error:  attempting to connect to a database on another
>machine.   You can't do this directly through JDBC-ODBC, unless your
>database driver understands HTTP URLs.  (Access doesn't.)  See below.
>Can't find Class:
>If the classes are not ones you've created, then some necessary classes
>are not in your classpath.  You need to locate those classes and modify
>your classpath, as described above.     If the classes are ones you've
>created, the likely reason is that you did not put the current directory
>in your classpath, or you are attempting to run your application from
>somewhere other than the current directory.   (Add "." to your classpath
>to add the current directory.)


  Return to Index