Get MySQL version with Java
NickName:Peter Penzov Ask DateTime:2015-12-12T23:27:36

Get MySQL version with Java

I tested this code to get MySQL version:

public void testMySQLVersion() throws Exception
    {
        System.out.println("\nTesting SQL query for MySQL version\n");

        // Load the JDBC driver
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

        // Connect to the database
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@112.213.131.40:3306:webdb", "admin", "eHqtaXuc6h1w");

        PreparedStatement pstmt
            = conn.prepareStatement("SHOW VARIABLES LIKE 'version'");

        ResultSet rset = pstmt.executeQuery();

        while (rset.next())
        {
            System.out.println("MySQL version " + rset.getString("Value"));
        }

        pstmt.close();
        conn.close();
    }

But I get this result when I run the code:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.634 sec <<< FAILURE! - in org.mysql.engine.osgi.impl.MySQLImplTest
testMySQLVersion(org.mysql.engine.osgi.impl.MySQLImplTest)  Time elapsed: 0.633 sec  <<< ERROR!
java.lang.StringIndexOutOfBoundsException: String index out of range: 12300
    at java.lang.String.checkBounds(String.java:385)
    at java.lang.String.<init>(String.java:324)
    at oracle.net.ns.Packet.extractData(Packet.java:447)
    at oracle.net.ns.RefusePacket.<init>(RefusePacket.java:70)
    at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:239)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:264)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1452)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at org.mysql.engine.osgi.impl.MySQLImplTest.testMySQLVersion(MySQLImplTest.java:66)

Can you give me some advice how to solve this issue?

Copyright Notice:Content Author:「Peter Penzov」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34241534/get-mysql-version-with-java

More about “Get MySQL version with Java” related questions

Get MySQL version with Java

I tested this code to get MySQL version: public void testMySQLVersion() throws Exception { System.out.println("\nTesting SQL query for MySQL version\n"); // Load the JDBC driv...

Show Detail

Mysql version for Slick

I updated my Play app with Scala 2.12 and Play 2.6.1, also I've changed Slick to v3.2 . Before (Scala 2.11.11 and Play 2.5.14) everything was working fine with Mysql driver version 5.1.23, which is...

Show Detail

Migrating to OpenJDK 11, which version of MySql connector is compatible for java 11?

I am trying to migrate my application from Java 8 to OpenJDK 11. I used MySql connector version 5 and its working fine. Is it required to upgrade MySql connector JAR to version 8.0? I read about it...

Show Detail

What mysql java driver version are you supposed to use with the latest mysql server

What mysql java driver version are you supposed to use with the latest mysql 5.5.7.RC server. (Env: Hibernate 3.3.x)

Show Detail

How to get the MySQL version

have the following string. [root@fedoravm001 ~]# mysql -V mysql Ver 14.14 Distrib 5.5.38, for Linux (x86_64) using readline 5.1 From the above I can able to parse and get the required version n...

Show Detail

Docker - MySQL and Java container connectivity error

I am trying to do a simple task of creating a microservice with JAVA and MySQL. I am using docker-compose on Windows 10 with Docker Desktop. Client: Docker Engine - Community Version: 1...

Show Detail

MySQL version error

I have following error: **Average of Job_Render is 138.29047 and its Minimum value is : 2.00061 and Maximum value is :7499.75 java.sql.SQLException: You have an error in your SQL syntax; check the

Show Detail

Upgrading mysql connector version to 8.0 from 5.1.42

Will this upgrade work with com.mysql.jdbc.Driver ? Because with change in driver i.e com.mysql.cj.jdbc.Driver throws could not start container error. ERROR [main] 0].tryStart(264) | Could not start

Show Detail

Java MySQL SyntaxErrorException

everyone! I'm trying to create a database using java. Here's the code public void createDatabase(String user, String password) { String query = "CREATE DATABASE IF NOT EXISTS Contacts;use Cont...

Show Detail

How to get the Java version in PowerShell

I'm trying to get the Java version in PowerShell. The version string is printed to stderr, so I'm trying to redirect it to stdout and assign it to a string variable. I get the following strange er...

Show Detail