Detecting remote database types via JDBC (source code)

Source: Internet
Author: User
Tags db2 ibm db2

The work often tells a database server about JDBC connection information, not the database version, so that development is not afraid to use the latest features of the database. I wrote a small program to detect the remote database server type:

Import java.sql.Connection; Import Java.sql.DatabaseMetaData; Import Java.sql.DriverManager; Import java.sql.SQLException; public class Dbversiondetector {public static void main (string[] args) throws Instantiationexception, Illegalaccessexcep tion, classnotfoundexception, SQLException {//class.forname ("Oracle.jdbc.driver.OracleDriver"). newinstance (); Class.forName ("Com.ibm.db2.jcc.DB2Driver"). newinstance (); Connection conn = Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:test", "oral", "oral"); Connection conn = drivermanager.getconnection ("Jdbc:db2://localhost:50000/sample", "Db2admin", "db2admin"); DatabaseMetaData meta = conn.getmetadata (); System.out.println ("url:/t" + Meta.geturl ()); System.out.println ("username:/t" + meta.getusername ()); System.out.println ("databaseproductname:/t" + meta.getdatabaseproductname ()); System.out.println ("databasemajorversion:/t" + meta.getdatabasemajorversion ()); System.out.println ("databaseminorversion:/t" + Meta.getdatabaseminorvErsion ()); System.out.println ("databaseproductversion:/t" + meta.getdatabaseproductversion ()); System.out.println ("drivername:/t" + meta.getdrivername ()); System.out.println ("drivermajorversion:/t" + meta.getdrivermajorversion ()); System.out.println ("driverminorversion:/t" + meta.getdriverminorversion ()); System.out.println ("driverversion:/t" + meta.getdriverversion ()); System.out.println ("jdbcmajorversion:/t" + meta.getjdbcmajorversion ()); System.out.println ("jdbcminorversion:/t" + meta.getjdbcminorversion ()); Conn.close (); } }

The results of the operation are as follows (example):

Url:jdbc:db2://localhost:50000/sample username:db2admin databaseproductname:db2/nt Databasemajorversion:9 Databaseminorversion:7 databaseproductversion:sql09070 DRIVERNAME:IBM DB2 JDBC Universal Driver Architecture DriverMaj Orversion:3 driverminorversion:57 driverversion:3.57.82 Jdbcmajorversion:3 jdbcminorversion:0

You can see from here that this is a DB2 v9.7 for Windows Server that supports JDBC3.0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.