Java JDBC Oracle Connection getDBVersion(String dbSourceName, String server, int port, String dbName, String userid, String pwd)

Here you can find the source of getDBVersion(String dbSourceName, String server, int port, String dbName, String userid, String pwd)

Description

get DB Version

License

Apache License

Declaration

public static String getDBVersion(String dbSourceName, String server,
            int port, String dbName, String userid, String pwd) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.FileInputStream;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Properties;

public class Main {
    private static final SimpleDateFormat timestampFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH.mm.ss.SSS");
    private static String URL_PROP_FILE = "url.properties";
    private static String DRIVER_PROP_FILE = "driver.properties";
    private static Properties propURL = null;
    private static Properties propDrivers = null;
    public static String linesep = System.getProperty("line.separator");
    public static String osType = System.getProperty("os.name")
            .toUpperCase().startsWith("Z/OS") ? "z/OS" : System
            .getProperty("os.name").toUpperCase().startsWith("WIN") ? "WIN"
            : "OTHER";
    public static String Message = "";

    public static String getDBVersion(String dbSourceName, String server,
            int port, String dbName, String userid, String pwd) {
        StringBuffer sb = new StringBuffer();
        Connection mainConn = null;
        String driverName = getDriverName(dbSourceName);
        String sql = null;// w ww. ja v a 2s .c  o m

        if (dbSourceName.equals("oracle")) {
            sql = "SELECT * FROM V$VERSION";
        } else if (dbSourceName.equals("db2")) {
            sql = "SELECT service_level||'(FP'||fixpack_num||')' FROM TABLE (sysproc.env_get_inst_info()) as x";
        } else if ((dbSourceName.equals("sybase"))
                || (dbSourceName.equals("mssql"))) {
            sql = "SELECT @@VERSION";
        } else if ((dbSourceName.equals("mysql"))
                || (dbSourceName.equals("postgres"))) {
            sql = "SELECT VERSION()";
        } else if (dbSourceName.equals("zdb2")) {
            sql = "";
        }

        if ((sql == null) || (sql.length() == 0)) {
            return "Unknown Version for " + dbSourceName;
        }
        try {
            try {
                Class.forName(driverName).newInstance();
            } catch (Exception e) {
                e.printStackTrace();
                Message = "Driver could not be loaded. See console's output.";
                return null;
            }
            String url = getURL(dbSourceName, server, port, dbName);
            if (dbSourceName.equalsIgnoreCase("domino")) {
                mainConn = DriverManager.getConnection(url);
            } else {
                mainConn = DriverManager.getConnection(url, userid, pwd);
                mainConn.setAutoCommit(true);
            }
            try {
                PreparedStatement stat = mainConn.prepareStatement(sql);
                ResultSet Reader = stat.executeQuery();
                while (Reader.next()) {
                    sb.append(Reader.getString(1) + linesep);
                }
                if (Reader != null)
                    Reader.close();
                if (stat != null)
                    stat.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            mainConn.close();
        } catch (SQLException e) {
            log("Error for : " + driverName + " for " + dbSourceName
                    + " Error Message :" + e.getMessage());
            System.exit(-1);
        }
        return sb.toString();
    }

    public static String getDriverName(String dbSourceName) {
        try {
            if (propDrivers == null) {
                propDrivers = new Properties();
                InputStream istream = ClassLoader
                        .getSystemResourceAsStream(DRIVER_PROP_FILE);
                if (istream == null) {
                    FileInputStream finStream = new FileInputStream(
                            DRIVER_PROP_FILE);
                    propDrivers.load(finStream);
                    finStream.close();
                } else {
                    propDrivers.load(istream);
                    istream.close();
                }
                log("Configuration file loaded: '" + DRIVER_PROP_FILE + "'");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String driverName = propDrivers.getProperty(dbSourceName
                .toLowerCase());
        return driverName;
    }

    public static String getURL(String dbSourceName, String server,
            int port, String dbName) {
        String url = "";
        try {
            if (propURL == null) {
                propURL = new Properties();
                InputStream istream = ClassLoader
                        .getSystemResourceAsStream(URL_PROP_FILE);
                if (istream == null) {
                    FileInputStream finStream = new FileInputStream(
                            URL_PROP_FILE);
                    propURL.load(finStream);
                    finStream.close();
                } else {
                    propURL.load(istream);
                    istream.close();
                }
                log("Configuration file loaded: '" + URL_PROP_FILE + "'"
                        + linesep);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (dbSourceName.equalsIgnoreCase("oracle")) {
            url = (String) propURL.get(dbSourceName) + server + ":" + port
                    + ":" + dbName;
        } else if (dbSourceName.equalsIgnoreCase("mssql")) {
            url = (String) propURL.get(dbSourceName) + server + ":" + port
                    + ";database=" + dbName;
        } else if ((dbSourceName.equalsIgnoreCase("access"))
                || (dbSourceName.equalsIgnoreCase("hxtt"))
                || (dbSourceName.equalsIgnoreCase("domino"))) {
            url = (String) propURL.get(dbSourceName) + server;
        } else if (dbSourceName.equalsIgnoreCase("mysql")) {
            url = (String) propURL.get(dbSourceName) + server + ":" + port
                    + "/" + dbName + "?zeroDateTimeBehavior=round";
        } else if (dbSourceName.equalsIgnoreCase("zdb2")) {
            url = (String) propURL.get(dbSourceName)
                    + server
                    + ":"
                    + port
                    + "/"
                    + dbName
                    + ":retrieveMessagesFromServerOnGetMessage=true;emulateParameterMetaDataForZCalls=1;";
        } else if (dbSourceName.equalsIgnoreCase("idb2")) {
            url = (String) propURL.get(dbSourceName) + server + ":" + port
                    + "/" + dbName + ";date format=iso";
        } else if (dbSourceName.equalsIgnoreCase("sybase")) {
            url = (String) propURL.get(dbSourceName) + server + ":" + port
                    + "/" + dbName;
        } else {
            url = (String) propURL.get(dbSourceName) + server + ":" + port
                    + "/" + dbName;
        }
        return url;
    }

    public static void log(String msg) {
        if (osType.equals("z/OS")) {
            System.out.println(timestampFormat.format(new Date()) + ":"
                    + msg);
        } else
            System.out.println("[" + timestampFormat.format(new Date())
                    + "] " + msg);
    }
}

Related

  1. getConnection()
  2. getConnection(String connectString)
  3. getConnection(String dbType, String dbIp, String dbPort, String dbName, String userName, String password)
  4. getConnection(String ip, String serviceName, String uid, String pwd)
  5. getDatabaseConnection()
  6. getJDBCConn(String hostname, String port, String sid, String username, String pwd)
  7. getJDBCConnection()
  8. getOracle10GConnection(String connIP, String port, String dbName, String username, String password)
  9. getOracleConn()