Java. SQL. sqlexception reported in ibatis: Invalid column type exception

Source: Internet
Author: User
1. <! -- Insert records to the database in the content table --> 2. < Insert ID = "Insertcontent" Parameterclass = "Com. unmi. Content" > 3. Insert into person (ID, name, serviceid) values (# ID #, # name #, # serviceid #) 4. </ Insert > // The three child segments can be null)

1. Sqlmapclient sqlmap = sqlmapconfig. getsqlmapinstance (); // As coded above 2. Content content = New Content ( 1 ); 3. Content. setname ( "Unmi" ); 4. // Content. setserviceid ("123456"); // note that this row is commented out. The serviceid attribute is null. 5. Sqlmap. insert ( "Content" , Content ); Some exception information: --- The error occurred in COM/unmi/content. xml.
--- The error occurred while applying a parameter map.
--- Check the insertperson-inlineparametermap.
--- Check the parameter mapping for the 'serviceid' property.
--- Cause: Java. SQL. sqlexception: Invalid column type
Caused by: Java. SQL. sqlexception: Invalid column type
At com.ibatis.sqlmap.engine.mapping.statement.generalstatement.exe cuteupdate (generalstatement. Java: 91)
At com. ibatis. sqlmap. Engine. impl. sqlmapexecutordelegate. insert (sqlmapexecutordelegate. Java: 442)
At com. ibatis. sqlmap. Engine. impl. sqlmapsessionimpl. insert (sqlmapsessionimpl. Java: 81)
At com. ibatis. sqlmap. Engine. impl. sqlmapclientimpl. insert (sqlmapclientimpl. Java: 58)
At com. unmi. Client. Main (client. Java: 33)
Caused by: Java. SQL. sqlexception: Invalid column type // Solution: You only need to remove the comment before content. setserviceid ("123456"); so that the three fields of content are not null, you can successfully insert records to the database. In fact, the content table does not have any constraints, but ibatis intercepts them here. In fact, this is an extra move. What should I do if I make a field null? You only need to modify the preset SQL statement in the ing file to tell ibatis what value should be used to replace the null value in this field, to connect to Oracle, even if a field is required to input a null value, the null value must be inserted. This is annoying.

1. <! -- Insert a record corresponding to person to the database -->
2. <insert id = "content" parameterclass = "com. unmi. Content">
3. insert into person (ID, name, passwd) values (# ID #,# name #,# serviceid: varchar: NULL #)
4. </Insert>Supported types are listed in Java. SQL. types. At the beginning, I mistakenly wrote the above varchar to varchar2. If you want to replace the input ID with 0 when it is null, you can write it as # ID: INTEGER: 0 #.

The reason is that ibatis determines which version of setxxx () method should be called for this prefill redstatement Setting Parameter based on the type of the current value of the parameter (rather than the class attribute, setstring (). If it is an int, setint () is called ). If the parameter value is null, ibatis does not know which setxxx () method to call. Therefore, the setnull () method is called in a general way under the Oracle driver, however, Oracle uses setnull () for string fields. This is because the type of the current field must be specified in the configuration, for example, # serviceid: varchar: NULL #. When ibatis encounters a condition where the parameter is null, it is difficult to determine, but the varchar option here is displayed, it also knows that the setstring () method should be used to assign values to the presponredstatement. As for null, it is only the default value when the specified parameter is null. Obviously, it can be written as # serviceid: varchar #. The following null is redundant. I don't know if you have noticed that the insert mark above configures the parameterclass attribute. If parametermap is configured, how can we solve the problem of null field values? This requires that we must specify the corresponding jdbctype attribute for parameters that may contain null values in the parametermap flag. If the nullvalue attribute is not specified, the null value is recorded in this table. View Source

Print?
01. <parametermap id = "insert-person-paramap" class = "com. unmi. Content">
02. <parameter property = "ID"/>
03. <parameter property = "name"/>
04. <parameter property = "serviceid" jdbctype = "varchar"/>
05. </parametermap>
06. <! -- Insert a record corresponding to person to the database -->
07. <insert id = "insertperson" parametermap = "insert-person-paramap">
08. insert into content (ID, name, serviceid) values (?,?,?)
09. </Insert>

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.