An issue that maps a field of number type in Oracle to a specific type in Java

Source: Internet
Author: User

I set a user ID field in Oracle to the number type, and using JDBC to complete the ORM, I thought it could be automatically converted to integer, because my Pojo class ID is an integer. But the fact is, when I was testing, I found all the user ID is all null, but also strange in the database ID is a value, why not?

The reason for this is that the number type of Oracle is mapped to a type of java.math.BigDecimal (immutable, arbitrary-precision, signed decimal number) in a Java type, not an Integer I think of as simple, and an error is reported:

That is, the BigDecimal field cannot be set into an integer type property.

I found that, in fact, if you use native JDBC to encapsulate the data, it is no problem to use Rs.getint into our integer field directly on this ID field:

     PublicList<user> FindList2 ()throwsException {Connection Connection=NULL; PreparedStatement PS=NULL; ResultSet RS=NULL; List<User> list=NULL; Try{Connection=Super. getconnection (); String SQL= "Select eu_user_id from Easybuy_user"; PS=connection.preparestatement (SQL); RS=Ps.executequery (); List=NewArraylist<user>();  while(Rs.next ()) {User User=NewUser (); USER.SETEU_USER_ID (Rs.getint ("EU_USER_ID"));            List.add (user); }        } Catch(Exception e) {e.printstacktrace (); }finally{            Super. CloseAll (Connection, PS, RS); }        returnlist; }

So why am I having this error?

The reason is that in order to facilitate the writing of JDBC, I use a simple encapsulation of the BASEDAOIMPL, its query using the reflection to do, it seems that also must use reflection, because each Pojo class attribute type is inconsistent:

Ublic list<object> ExecuteQuery2 (String sql, object[] objparam, class<?> className)throwsException {List<Object> list =NewArraylist<object>(); Connection Conn= This. getconnection (); PreparedStatement PS=conn.preparestatement (SQL); ResultSet RS=NULL; if(Objparam! =NULL) {             for(inti = 0; i < objparam.length; i++) {Ps.setobject ((i+ 1), Objparam[i]); }} RS=Ps.executequery (); //class<?> clz = Class.forName (className);Class<?> clz=ClassName; Field[] FS=Clz.getdeclaredfields ();  while(Rs.next ()) {Object objinstrance=clz.newinstance ();  for(Field f:fs) {Try{String FieldName=F.getname (); Object Fieldvalue=Rs.getobject (fieldName); //System.out.println (fieldname+ ":" +fieldvalue+ "\ T" +f.gettype () + "\ T" +fieldvalue.getclass ());F.setaccessible (true);                F.set (Objinstrance, fieldvalue); } Catch(Exception ex) {ex.printstacktrace ();        }} list.add (Objinstrance); }         This. CloseAll (Conn, PS, RS); returnlist; }

BigDecimal type is not processed, we are not likely to use the BigDecimal type in Pojo, so we can not map the BigDecimal field into an integer, the report above the exception, slightly modified, add a judgment:

 Publiclist<object> ExecuteQuery2 (String sql, object[] objparam, class<?> className)throwsException {List<Object> list =NewArraylist<object>(); Connection Conn= This. getconnection (); PreparedStatement PS=conn.preparestatement (SQL); ResultSet RS=NULL; if(Objparam! =NULL) {             for(inti = 0; i < objparam.length; i++) {Ps.setobject ((i+ 1), Objparam[i]); }} RS=Ps.executequery (); //class<?> clz = Class.forName (className);Class<?> clz=ClassName; Field[] FS=Clz.getdeclaredfields ();  while(Rs.next ()) {Object objinstrance=clz.newinstance ();  for(Field f:fs) {Try{String FieldName=F.getname (); Object Fieldvalue=Rs.getobject (fieldName); if("Java.math.BigDecimal". Equals (Fieldvalue.getclass (). GetName ())) {     //if the Java.math.BigDecimal type attribute is converted to integer, the parseint of the integer is usedFieldvalue=Integer.parseint (fieldvalue.tostring ()); }                    //System.out.println (fieldname+ ":" +fieldvalue+ "\ T" +f.gettype () + "\ T" +fieldvalue.getclass ());F.setaccessible (true);                F.set (Objinstrance, fieldvalue); } Catch(Exception ex) {ex.printstacktrace ();        }} list.add (Objinstrance); }         This. CloseAll (Conn, PS, RS); returnlist; }

This is only a compromise, because our java.math.BigDecimal type can only be converted to integer, if the ID type in Pojo is long, and not. So there is a certain limitation, not too flexible. Should be based on the specific type of id attribute in our Pojo class to convert, I do not know how the specific code should be written.

Perhaps we should use the Queryrunner class in Commons-dbutils.jar as a basedao to use? I used a Txqueryrunner class in (Jsp+servlet+jdbc's use review) to simply encapsulate the queryrunner.

In general, we are not very likely to encounter this anomaly, because the strong framework has helped us to complete these trivial problems, but the timely understanding of the underlying things, there are some benefits

An issue that maps a field of number type in Oracle to a specific type in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.