Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of Java type is "[B"?

I am trying to get MD5 encrypted pass from MySQL DB via Java code (Hibernate). But I cant 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 (or whatever Java type I try cast to).

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) 
like image 236
Xorty Avatar asked Jan 05 '11 17:01

Xorty


People also ask

What is B in Java?

In the Java descriptor syntax, a [ at the beginning means an array. There's a one letter code for each primitive type - B = Byte, C = Char, S = Short, Z = Boolean, I = Int, J = Long, F = Float, and D = Double.

What is a Java type?

The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types (§4.2) are the boolean type and the numeric types. The numeric types are the integral types byte , short , int , long , and char , and the floating-point types float and double .

What is bigger than string in Java?

Integer. MAX_VALUE which is 2147483647 or (2^31 - 1).

How do you check if a variable is a double in Java?

The best way is to read the code where you declared what it was. If you are trying to parse a string as a double, then Double. parseDouble will throw an exception (which you can catch) if the string can't be parsed. radius is a double 100% of the time.


2 Answers

That my friend is an array of bytes. In JNI, [B is used to describe an array ([) of bytes (B). An array of ints is [I etc.

You can get a bit more information on field descriptors here:
JNI Types and Data Structures (Table 3-2 should be what you are looking for).

like image 192
rfeak Avatar answered Sep 28 '22 23:09

rfeak


It's the class name of byte[].class. Try this:

System.out.println(byte[].class.getName()); 

Output (you guessed it):

[B

And if you want to access the readable name, use Class.getCanonicalName():

System.out.println(byte[].class.getCanonicalName()); 

Output:

byte[]

like image 37
Sean Patrick Floyd Avatar answered Sep 29 '22 00:09

Sean Patrick Floyd