Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1434316
  • 博文数量: 556
  • 博客积分: 12626
  • 博客等级: 上将
  • 技术积分: 5799
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-11 15:56
个人简介

从事IT基础架构多年,发现自己原来更合适去当老师……喜欢关注新鲜事物,不仅限于IT领域。

文章分类

全部博文(556)

文章存档

2019年(6)

2018年(15)

2017年(17)

2016年(11)

2015年(2)

2014年(2)

2013年(36)

2012年(54)

2011年(100)

2010年(41)

2009年(72)

2008年(14)

2007年(82)

2006年(104)

分类: Oracle

2007-08-01 13:25:16

In this Document
  
  
  
  
  
  
  


Applies to:

ALL PRODUCT ACCESS - Version:
Information in this document applies to any platform.

Purpose

Determine information about your JDBC/JDK environment that is especially useful for Language Support Analysts. Includes: database version, JDBC driver version, JDK version, Path, Classpath and JDBC URL.

Software Requirements/Prerequisites

The sample program in this article is provided for educational purposes only and is NOT supported by Oracle Support Services. It has been tested internally, however, and works as documented. We do not guarantee that it will work for you, so be sure to test it in your environment before relying on it.

Configuring the Sample Code

* This application is meant for the command line. Refer to:
"Extract jdbc information from database connection in a web server" for a web serverNote 265686.1 "Extract jdbc information from database connection in a web server" for a web server format (jsp).

* This application assumes the JDK is installed and that the classpath contains the appropriate libraries (ie OracleDriver).

Running the Sample Code

Compile: javac TestJdbc.java
Run: java TestJdbc yourFullJdbcURL username password
ie: java TestJdbc jdbc:oracle:thin:@myhost:1521:mySid scott tiger

Caution

This sample code is provided for educational purposes only and not supported by Oracle Support Services. It has been tested internally, however, and works as documented. We do not guarantee that it will work for you, so be sure to test it in your environment before relying on it.

Proofread this sample code before using it! Due to the differences in the way text editors, e-mail packages and operating systems handle text formatting (spaces, tabs and carriage returns), this sample code may not be in an executable state when you first receive it. Check over the sample code to ensure that errors of this type are corrected.

Sample Code

import java.sql.*;

public class TestJdbc {

public static void main (String[] args) {
  if (args.length < 3) {
    System.out.println("Usage: java TestJdbc jdbcUrl user password");
    System.exit(0);
  }

  String url = args[0];
  String user = args[1];
  String password = args[2];


  try {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Connection conn = DriverManager.getConnection(url, user, password);

    DatabaseMetaData meta = conn.getMetaData ();

    // gets driver info:
    System.out.println("\n=============\nDatabase Product Name is ... " +
      meta.getDatabaseProductName());
    System.out.println("\nDatabase Product Version is " +
      meta.getDatabaseProductVersion());
    System.out.println("\n=============\nJDBC Driver Name is ........ " + 
      meta.getDriverName());
    System.out.println("\nJDBC Driver Version is ..... " + meta.getDriverVersion());
    System.out.println("\nJDBC URL " + meta.getURL());
    System.out.println("\n=============");

    //get environment info:
    java.util.Properties props = System.getProperties();
    java.util.Enumeration propNames = props.propertyNames();

    StringBuffer path = new StringBuffer("\t" + props.getProperty("java.library.path"));
    int idx = 0;

    while (idx < path.length()) {
      idx = path.toString().indexOf(";", idx);

      if (idx == -1)
        break;

      path.replace(idx, ++idx, "\n\t");

      idx++;
   }

   StringBuffer classpath = new StringBuffer("\t" + props.getProperty("java.class.path"));
   int idx2 = 0;

   while (idx2 < classpath.length()) {
     idx2 = classpath.toString().indexOf(";", idx2);

     if (idx2 == -1)
       break;
   
     classpath.replace(idx2, ++idx2, "\n\t");

     idx2++;
   }

  System.out.println("java runtime Version: \t" +
     props.getProperty("java.runtime.version") + "\n");
  System.out.println("\n=============");
  System.out.println("java vm Version: \t" + props.getProperty("java.vm.version") + "\n");
  System.out.println("\n=============");
  System.out.println("java vm name: \t" + props.getProperty("java.vm.name") + "\n");
  System.out.println("\n=============");
  System.out.println("java library path: \n" + path + "\n");
  System.out.println("\n=============");
  System.out.println("java classpath: " + classpath + "\n");


} catch (Exception e) {e.printStackTrace(); }
}//end of main
}//end of TestJdbc

Sample Code Output

If you forget the arguments you will see:

Usage: java TestJdbc jdbcUrl user password

Correct execution result:
=============
Database Product Name is ... Oracle

Database Product Version is Oracle9i Enterprise Edition Release 9.2.0.4.0 - Pro
duction
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production

=============
JDBC Driver Name is ........ Oracle JDBC driver

JDBC Driver Version is ..... 9.2.0.4.0

Jdbc URL java:oracle:thin:@myhost:1521:mysid

=============
java runtime Version: 1.3.1_09-b03


=============
java vm Version: 1.3.1_09-b03


=============
java vm name: Java HotSpot(TM) Client VM


=============
java library path:
c:\java\jdk1.3\bin
.
C:\WINNT\system32
C:\WINNT
C:\oracle\ora92\bin
C:\ora9ias\bin
C:\ora9ias\jlib
C:\oracle\ora817\bin
C:\oracle\ora817\Apache\Perl\5.00503\bin\mswin32-x86
C:\Program Files\Oracle\jre\1.1.7\bin
c:\java\jdk1.3\bin
C:\Program Files\Oracle\jre\1.3.1\bin
C:\Program Files\Oracle\jre\1.1.8\bin
C:\Program Files\Reflection
C:\WINNT\system32
C:\WINNT
C:\WINNT\System32\Wbem
C:\Program Files\Symantec\pcAnywhere\
C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT
C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin
C:\Program Files\Microsoft Visual Studio\Common\Tools
C:\Program Files\Microsoft Visual Studio\VC98\bin


=============
java classpath: .
c:\oracle\ora92\jdbc\lib\classes12.jar
c:\oracle\ora92\lib\xmlparserv2.jar
c:\oracle\ora92\rdbms\jlib\xdb.jar


今天由于需要,测试了一把,因为对java是门外汉,所以折腾了半天。遇到了几个问题,首先是代码中的public class TestJdbc要和java文件的TestJdbc名字一致并区分大小写,一定要设置CLASSPATH,不然这两个地方都会报错,CLASSPATH要指定具体的jar文件,光设置路径好像不行,这里我指定的是ojdbc14_g.jar。最后要注意的就是用java TestJdbc jdbc:oracle:thin:@myhost:1521:mySid scott tiger方式连接的时候报错:
java.sql.SQLException: Io 异常: NL Exception was generated
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:158)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:382)
        at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:333)
        at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:371)
        at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:148)
        at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
        at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:572)
        at java.sql.DriverManager.getConnection(DriverManager.java:512)
        at java.sql.DriverManager.getConnection(DriverManager.java:171)
        at TestJdbc.main(TestJdbc.java:18)
实际上将URL中@后面的连接串部分改为(DESCRIPTION = (ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))   )(CONNECT_DATA = (SERVICE_NAME = sid)  ) )就可以了,至于为什么我不知道,java专门的论坛里应该有说明,呵呵。
完整的命令就是:
java TestJdbc jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))   )(CONNECT_DATA = (SERVICE_NAME = zhyz1)  ) ) scott tiger
 
另外,在使用的ojdbc14_g.jar这个包里有个MANIFEST.MF的文件,里面应该是这个jar的一些信息,可以看下。
 
上面文章的metalink号是265685.1
阅读(2304) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~