Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Barathwaj.

Asked: January 06, 2009 - 9:59 am UTC

Last updated: March 09, 2018 - 2:31 am UTC

Version: 10.2.0.3

Viewed 10K+ times! This question is

You Asked

Hi,
I have five columns as below in my DB
col1 VARCHAR2(4000 CHAR)
col2 VARCHAR2(4000 CHAR)
col3 VARCHAR2(4000 CHAR)
col4 VARCHAR2(4000 CHAR)
col5 VARCHAR2(4000 CHAR)

When i try to save the data with max length, i get the following error.
SQL state [72000]; error code [1461]; ORA-01461: can bind a LONG value only for insert into a LONG column ; nested exception is java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column

I am using Hibernate to save the data from the application. And here is the complete message i get.
Couldnot save. Due to Hibernate flushing: could not update: [com.xyz.Data#24]; uncategorized SQLException for SQL [/* update com.xyz.DP */ update APPLICATION.TY_P set VERSION=?, CYCLE_ID=?, PL_ID=?, T_LEVEL_ID=?, TERRITORY=?, DIVISION_ID=?, DIVISION=?, LEAD=?, T_LEAD=?, STRENGTHS=?, WEAKNESSES=?, OPPORTUNITIES=?, THREATS=?, STRATEGY=?, AEVENTS=?, T_EVENTS=?, AAND_LEARNS=?, TAND_LEARNS=?, AP_EVENTS=?, TP_EVENTS=?, A_PROGRAMS=?, T_PROGRAMS=?, P_COST=?, P_BUDGET=?, LLEARNS_COST=?, L_LEARNS_BUDGET=?, PS_COST=?, PS_BUDGET=?, MROGRAMS_COST=?, MROGRAMS_BUDGET=?, STATUS=?, LAST_UPDATED_BY=?, LAST_UPDATE_DATE=? where T_ID=? and VERSION=?]; SQL state [72000]; error code [1461]; ORA-01461: can bind a LONG value only for insert into a LONG column ; nested exception is java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column
It doesnt work

The strange thing is. column col5 can is accepting 4000chars and its getting saved. But if i try to save 4000 chars from Col1 through Col4 it doesnt work.

I am not sure what is the limitation on the table definition. Cant i have more than one Varchar2(4000 CHAR).
Thanks
Barath

and Tom said...

this has nothing to do with Oracle.

this has something to do with the hibernate generated code - there are no problems updating 5 varchar2(4000) columns from Java.


You can use this program to verify that it works - and this is a hibernate binding issue (that you would need to get into touch with the owners of hibernate about...)

Note: if you modify the code below to try to update with 4001 characters, it'll fail with " ORA-01461: can bind a LONG value only for insert into a LONG column" - so you might just want to check your code (eg: put a debug line of code in there to dump the lengths of the strings before the update) to ensure you are only doing 4000 or less.

import java.sql.*;
public class instest
{
   static public void main(String args[]) throws Exception
   {
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      Connection
         conn = DriverManager.getConnection
                ("jdbc:oracle:thin:@dellpe:1521:ora10gr2",
         "scott","tiger");
      conn.setAutoCommit( false );

      Statement stmt = conn.createStatement();
      stmt.execute(
      "create table t " +
      "(x number, "+
      " c1 varchar2(4000),"+
      " c2 varchar2(4000),"+
      " c3 varchar2(4000),"+
      " c4 varchar2(4000),"+
      " c5 varchar2(4000) )" );


      stmt.execute( "insert into t (x) values (1)" );


      PreparedStatement pstmt =
          conn.prepareStatement
          ("update t set c1 = ?, c2 = ?, c3 = ?, c4 = ?, c5 = ? where x = 1" );

      String data1 = "x";
      for( int i = 1; i < 4000; data1 = data1+"x", i++ );
      String data2 = data1;
      String data3 = data1;
      String data4 = data1;
      String data5 = data1;

      pstmt.setString( 1, data1 );
      pstmt.setString( 2, data1 );
      pstmt.setString( 3, data1 );
      pstmt.setString( 4, data1 );
      pstmt.setString( 5, data1 );

      PreparedStatement commit =
          conn.prepareStatement
          ("commit work write immediate wait");

      pstmt.executeUpdate();
      commit.executeUpdate();
      conn.close();
   }
}

Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

It doesnt Work. VARCHAR2(4000 CHAR)", version 10.2.0.3

Barathwaj Ramamoorthy, January 07, 2009 - 2:42 pm UTC

Hi Tom,
thanks for the reply. I am sorry about the duplicate. I did that coz i was not sure and couldnt find my post.

I tried the same code that you gave to me. I didnt work. It
throws java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column

Here is the catch. I tried using plain jdbc statement instead of a prepared statement which works. But when i try with PreparedStatement it didnt work for me. Since i use 10g i tried with the new driver (oracle.jdbc.OracleDriver) instead of (oracle.jdbc.driver.OracleDriver). Both of them didnt work.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class instest
{
   static public void main(String args[]) throws Exception
   {
   
      DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
      Connection
         conn = DriverManager.getConnection
                ("jdbc:oracle:thin:@nest:1521:dt",
         "scott","tiger");
      conn.setAutoCommit( false );

      final String UPDATE_SQL = "UPDATE TP " +
  "SET " +
  "STRENGTHS=?," +
  "WEAKNESSES=?, " + 
  "OPPORTUNITIES=?, " + 
  "THREATS=? " +
  "WHERE TP_ID= ?";
      PreparedStatement pstmt =
          conn.prepareStatement
          (UPDATE_SQL);

      String data1 = "x";
      for( int i = 1; i < 4000; data1 = data1+"x", i++ );
      String data2 = data1;
      String data3 = data1;
      String data4 = data1;

      pstmt.setString( 1, data1 );
      pstmt.setString( 2, data2 );
      pstmt.setString( 3, data3 );
      pstmt.setString( 4, data4 );
      pstmt.setLong(5, 24);

      PreparedStatement commit =
          conn.prepareStatement
          ("commit work write immediate wait");

      pstmt.executeUpdate();
      commit.executeUpdate();
      conn.close();
   }
}


I am not sure about your database settings.
The character set i have is UTF-8.
Hibernate is also using UTF-8 by default for character set encoding. I tried with the preparedStatementCallBack in Hibernate too. Didnt work. Would it be a oracleDriver issue? I was using the oc4jdbc came with weblogic 8.1.6 as well used the drivers upto 10.2.0.3. No difference.

I would appreciate any help on this. Thanks for your time.

Thanks
Barath

Tom Kyte
January 07, 2009 - 3:05 pm UTC

I'll have to refer you to support - you have the perfect "small" test case now. I used the stock jdbc drivers with 10.2.0.4

$ echo $CLASSPATH
/home/ora10gr2/jdbc/lib/classes12.zip:.


it could well be the utf8

It doesnt Work. VARCHAR2(4000 CHAR)", version 10.2.0.3

Barathwaj Ramamoorthy, January 07, 2009 - 4:55 pm UTC

Thanks Tom, I am using JDK 1.4 version.
So i am not intended to use Classes12.zip / classes12.jar files. I am using oc4jdbc12.jar file.

Im surprised if there will be difference between them. Curiously i looked for the classes in both these jar's and i could not figure out many differences though.

http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html - I got the jars from this location.

Thanks
Barath

Tom Kyte
January 07, 2009 - 5:23 pm UTC

did you try the ones I used, they ship with the database, you already have them.

Byte limitation?

APH, January 07, 2009 - 5:45 pm UTC

If I recall, regardless of whether you have the column as 4000 bytes or 4000 chars, the limit of the database is 4000 bytes. That is, if you are using a multi-byte character set you may not actually be able to fit 4000 characters into a varchar2(4000 char) field.

See the reference at http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/datatype.htm#sthref3780

The VARCHAR2 datatype stores variable-length character strings. When you create a table with a VARCHAR2 column, you specify a maximum string length (in bytes or characters) between 1 and 4000 bytes for the VARCHAR2 column.


Tom Kyte
January 08, 2009 - 8:10 am UTC

that is correct, however - we are binding a 4000 byte string here - 'x' takes a single byte.

Feedback on not answering the exact quesiton

Alik Elzin, July 16, 2015 - 7:08 am UTC

The question was asked about `varchar(4000 char)` but the answer was about `varchar(4000)`, which and implicit `varchar(4000 byte)`.
One is `char` and the other is `byte`. A `char` may be bigger than a `byte`.

Resolution?

Mike Konikoff, March 08, 2018 - 10:13 pm UTC

If a support ticket was filed, could the OP or someone link it here? Was there ever any resolution? Inquiring minds and all...
Connor McDonald
March 09, 2018 - 2:31 am UTC

MOS note 461670.1 seems to be relevant here. Patches were released for the database.