Skip to content

Instantly share code, notes, and snippets.

@jovemfelix
Last active November 18, 2021 15:49
Show Gist options
  • Save jovemfelix/9948d1365eea29dd69203fec2675927a to your computer and use it in GitHub Desktop.
Save jovemfelix/9948d1365eea29dd69203fec2675927a to your computer and use it in GitHub Desktop.
Example of how to get the database version using java, in the case below as an example using SQL Server

Example of how to get the database version using java, in the case below as an example using SQL Server

Source Code

Change: username , password and driver, jdbc-driver

import java.sql.*;
import java.util.Vector;

public class Main {
    static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerXADataSource";
    static final String DB_URL = "jdbc:sqlserver://my-host;DatabaseName=my-database;sendStringParametersAsUnicode=false;";

    static final String USER = "my-user"; // Fake of course.
    static final String PASS = "my-password"; // This too!

    static final String QUERY = "SELECT @@VERSION";

    public static void main(String[] args) {
        run(QUERY);
    }

    public static void run(String sql) {
        Statement stmt = null;
        ResultSet rs = null;
        Connection conn = null;
        Vector<String> columnNames = new Vector<String>();

        try {
            Class.forName(JDBC_DRIVER);

            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs != null) {
                ResultSetMetaData columns = rs.getMetaData();
                int i = 0;
                while (i < columns.getColumnCount()) {
                    i++;
                    System.out.print(columns.getColumnName(i) + "\t");
                    columnNames.add(columns.getColumnName(i));
                }
                System.out.print("\n");

                while (rs.next()) {
                    for (i = 0; i < columnNames.size(); i++) {
                        System.out.print(rs.getString(columnNames.get(i)) + "\t");

                    }
                    System.out.print("\n");
                }

            }
        } catch (Exception e) {
            System.out.println("Exception: " + e.toString());
        }

        finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception mysqlEx) {
                System.out.println(mysqlEx.toString());
            }
        }
    }
}

Build

javac Main.java

Package

Driver used mssql-jdbc-9.4.0.jre11.jar

# copy the drivers to folder
mkdir dist
cp /driver-folder/mssql-jdbc-9.4.0.jre11.jar dist/

# copy executable to folder
cp Main.class dist/

Run

cd dist
java -cp ".:./mssql-jdbc-9.4.0.jre11.jar" Main1

Sample output:

Microsoft SQL Azure (RTM) - 12.0.2000.8 Sep 18 2021 19:01:34 Copyright (C) 2019 Microsoft Corporation

JDBC for MySQL

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://my-host:3306/my-database?useSSL=false";

JDBC for Oracle

Download drivers here: https://www.oracle.com/database/technologies/appdev/jdbc-downloads.html

//static final String JDBC_DRIVER = "oracle.jdbc.xa.client.OracleXADataSource";
static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
static final String DB_URL = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my-host)(PORT=1529))(FAILOVER=on)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=my-service-name)(FAILOVER_MODE=(TYPE=SELECT)(METHOD=BASIC))))";

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment