본문 바로가기
Java

Preparestatement CANNOT order by

by nomore7 2014. 5. 26.



JDBC, MYSQL getStrng Blob type, order by BUG.


SQL : select user_name, aes_decrypt(user_password,'SECRET_KEY') as user_password from user order by user_name;


java.sql.preparestatement 를 사용할 때, getString으로 리턴 할 경우 위와 같은 쿼리 결과를 제대로 반환하지 않는 문제에 직면했다.


query상에 문제가 없고, order by를 사용하지 않거나, preparestatement대신 statement로 대신했을 경우 또는 Blob형을 사용하지 않을 경우는 문제가 없었다.


왜 그럴가 생각하고, 집에와서 간단히 jdbc를 다운받고, 간단한 코드로 테스트 해보았는데, 잘 동작했다.


같은 java테스트 코드로 테스트해보았는데도 잘 동작했다.


알고보니 사용한 jdbc 버전의 문제였다.


http://bugs.mysql.com/bug.php?id=19912


새로 받은 라이브러리에서는 문제가 없었다.


ex) 

1.최신에서의 리턴 


user_name : test01 user_password : master user_name : tester1 user_password : test1 user_name : tester2 user_password : test1 user_name : test user_password : test1


2.old version JDBC


user_name : test01

user_password : [B@7018f3c0

user_name : tester1

user_password : [B@762afbbe

user_name : tester2

user_password : [B@7ff5b38d

user_name : test

user_password : [B@5bdf2f9c



old version 라이브러리를 변경할 수 없다면, 해결 방법으로는


1. getString으로 결과를 반환하지 않고 getBlob으로 결과를 가져와 변환하던지, 

2. stmt를 사용하던지,

3. sql에서 CAST를 통해 string형으로 변환할 수 있다.



테스트 환경은 아래와 같다.


JDK

1.6 , 1.7

테스트한 버전의 JDBC

mysql-connector-java-3.1.7-bin.jar

mysql-connector-java-5.1.30-bin.jar




-----------------------------------------------------





JDBC, MYSQL getStrng Blob type, order by BUG.


SQL : select user_name, aes_decrypt(user_password,'SECRET_KEY') as user_password from user order by user_name;


In case java.sql.preparestatement,getString, SQL contains order by, Blob Type. RusultSet doesn`t return result.


if you doesn`t use 1. order by, OR 2. preparestatement, OR 3. BLOB Type. It`s OK.

To test this problem, I tried again in my home.


In my home, With SAME Code, It WORKED..


The problem was version of JDBC.


http://bugs.mysql.com/bug.php?id=19912


recent version of JDBC, download immediately was OK.



ex) 

1 recent JDBC result. 


user_name : test01 user_password : master user_name : tester1 user_password : test1 user_name : tester2 user_password : test1 user_name : test user_password : test1


2.old version JDBC result


user_name : test01

user_password : [B@7018f3c0

user_name : tester1

user_password : [B@762afbbe

user_name : tester2

user_password : [B@7ff5b38d

user_name : test

user_password : [B@5bdf2f9c



If you cannot change old version of JDBC. You could solve this problem.


1. Use getBlob, and then parse it to String. 

2. Use statement,

3. Modify SQL with CAST.



Test Environment.


JDK

1.6 , 1.7

Version test JDBC

mysql-connector-java-3.1.7-bin.jar

mysql-connector-java-5.1.30-bin.jar