Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I am trying to get MD5 generated pass from MySQL DB via Java code (Hibernate). But I can get neither String nor any reasonable Java type.

The only thing I am getting is this unhelpful message:

java.lang.ClassCastException: [B cannot be cast to com.mysql.jdbc.Blob

Here is my method:

public void testCrypto() {

        session.beginTransaction();

        // creates native SQL query

        // uses native MySQL's MD5 crypto

        final Blob pass = (Blob) session.createSQLQuery("SELECT MD5('somePass')")

            .list().get(0);

        session.getTransaction().commit();

}

Here is full stack trace:

java.lang.ClassCastException: [B cannot be cast to com.mysql.jdbc.Blob

    at domain.DatabaseTest.testCrypto(DatabaseTest.java:57)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at junit.framework.TestCase.runTest(TestCase.java:168)

    at junit.framework.TestCase.runBare(TestCase.java:134)

    at junit.framework.TestResult$1.protect(TestResult.java:110)

    at junit.framework.TestResult.runProtected(TestResult.java:128)

    at junit.framework.TestResult.run(TestResult.java:113)

    at junit.framework.TestCase.run(TestCase.java:124)

    at junit.framework.TestSuite.runTest(TestSuite.java:232)

    at junit.framework.TestSuite.run(TestSuite.java:227)

    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)

    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)

    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 Answer

0 votes
by (13.1k points)

That is an array of bytes. In JNI, [B is used to describe an array of bytes. An array of integers is [I, etc.

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
asked Mar 29, 2021 in Java by dev_sk2311 (45k points)
0 votes
1 answer
asked Mar 23, 2021 in Java by dev_sk2311 (45k points)
0 votes
1 answer
asked Mar 29, 2021 in Java by dev_sk2311 (45k points)
0 votes
1 answer
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...