vivianQQ
asked on
Hon can I do with the big data?
Hi,
When I inserted some datas into a column which is varchar2 I always got error ora-01704 String literal too long. Must the length of the data is bigger than 4000.
I want to change the column type to LOB,but I don't know how to write the data into it and how to read
them out! I use Oracle jdbc.
Any reply will be appreciated.
Vivian Qi
When I inserted some datas into a column which is varchar2 I always got error ora-01704 String literal too long. Must the length of the data is bigger than 4000.
I want to change the column type to LOB,but I don't know how to write the data into it and how to read
them out! I use Oracle jdbc.
Any reply will be appreciated.
Vivian Qi
ASKER
But how can I transfer the String that I got from the client into the CLOB.I have try java.io.writer.
java.io.Writer writer
// read data into a character array
char[] data = {'0','1','2','3','4','5',' 6','7','8' ,'9'};
// write the array of character data to a CLOB
writer = ((CLOB)my_clob).getCharact erOutputSt ream();
writer.write(data);
writer.flush();
writer.close();
This is the example that given by oralce 8 document.I got the error :"Error #: 300 : method write(oracle.sql.CLOB) not found in class java.io.Writer at line 195, column 16"
when I compile the code.
what's wrong?
java.io.Writer writer
// read data into a character array
char[] data = {'0','1','2','3','4','5','
// write the array of character data to a CLOB
writer = ((CLOB)my_clob).getCharact
writer.write(data);
writer.flush();
writer.close();
This is the example that given by oralce 8 document.I got the error :"Error #: 300 : method write(oracle.sql.CLOB) not found in class java.io.Writer at line 195, column 16"
when I compile the code.
what's wrong?
hi
which Writer class u are using
and where r u trying to write?
is it in the file?
well if u are getting the string then u can try following
String client ="0,1,2,3,4,5,6,7,8,9";
ByteArrayInputStream barray = new ByteArrayInputStream(clien t.getBytes ());
then use this stream and passit to preparedstatement.setInput Stream()to set it in DB
Cheers
RJ
which Writer class u are using
and where r u trying to write?
is it in the file?
well if u are getting the string then u can try following
String client ="0,1,2,3,4,5,6,7,8,9";
ByteArrayInputStream barray = new ByteArrayInputStream(clien
then use this stream and passit to preparedstatement.setInput
Cheers
RJ
ASKER
I want to know how can I declare a clob and write a string into it before insert it into the table?
I built the code in this way:
CharArrayInputStream barray = new CharArrayInputStream(intro .getBytes( ));
String sql="insert into singers values('"+singer_ID+"','"+ singer_Cat egory+"',' "+cn_Name+ "','"+en_N ame+"','"+ gender+"', '?')";
PreparedStatement pstmt = conn.prepareStatement(sql) ;
pstmt.setInputStream(1,bar ray);
pstmt.execute();
I got the compiler errors:
"sseiBean.java": Error #: 300 : class CharArrayInputStream not found in class ssei.sseiBean at line 198, column 9
"sseiBean.java": Error #: 300 : class CharArrayInputStream not found in class ssei.sseiBean at line 198, column 43
"sseiBean.java": Error #: 300 : method setInputStream(int, <any>) not found in interface java.sql.PreparedStatement at line 201, column 15
Thank U!
I am not familiar with stream and clob.
I built the code in this way:
CharArrayInputStream barray = new CharArrayInputStream(intro
String sql="insert into singers values('"+singer_ID+"','"+
PreparedStatement pstmt = conn.prepareStatement(sql)
pstmt.setInputStream(1,bar
pstmt.execute();
I got the compiler errors:
"sseiBean.java": Error #: 300 : class CharArrayInputStream not found in class ssei.sseiBean at line 198, column 9
"sseiBean.java": Error #: 300 : class CharArrayInputStream not found in class ssei.sseiBean at line 198, column 43
"sseiBean.java": Error #: 300 : method setInputStream(int, <any>) not found in interface java.sql.PreparedStatement
Thank U!
I am not familiar with stream and clob.
ASKER
I want to know how can I declare a clob and write a string into it before insert it into the table?
I built the code in this way:
CharArrayInputStream barray = new CharArrayInputStream(intro .getBytes( ));
String sql="insert into singers values('"+singer_ID+"','"+ singer_Cat egory+"',' "+cn_Name+ "','"+en_N ame+"','"+ gender+"', '?')";
PreparedStatement pstmt = conn.prepareStatement(sql) ;
pstmt.setInputStream(1,bar ray);
pstmt.execute();
I got the compiler errors:
"sseiBean.java": Error #: 300 : class CharArrayInputStream not found in class ssei.sseiBean at line 198, column 9
"sseiBean.java": Error #: 300 : class CharArrayInputStream not found in class ssei.sseiBean at line 198, column 43
"sseiBean.java": Error #: 300 : method setInputStream(int, <any>) not found in interface java.sql.PreparedStatement at line 201, column 15
Thank U!
I am not familiar with stream and clob.
I built the code in this way:
CharArrayInputStream barray = new CharArrayInputStream(intro
String sql="insert into singers values('"+singer_ID+"','"+
PreparedStatement pstmt = conn.prepareStatement(sql)
pstmt.setInputStream(1,bar
pstmt.execute();
I got the compiler errors:
"sseiBean.java": Error #: 300 : class CharArrayInputStream not found in class ssei.sseiBean at line 198, column 9
"sseiBean.java": Error #: 300 : class CharArrayInputStream not found in class ssei.sseiBean at line 198, column 43
"sseiBean.java": Error #: 300 : method setInputStream(int, <any>) not found in interface java.sql.PreparedStatement
Thank U!
I am not familiar with stream and clob.
I believe you can treat the CLOB column as though it is a varchar. Suppose that you have two columns, and one of them is a CLOB to store HTML code, then you can have the following:
Statement statement = connection.createStatement ();
statement.executeUpdate("i nsert into mytable (id, html) values('00','" + html + "')"); // where html is a CLOB data
statement.close();
connection.close();
Statement statement = connection.createStatement
statement.executeUpdate("i
statement.close();
connection.close();
ASKER
But how can I change the String variable to a CLOB variable?
You don't have to covert it to a CLOB variable. Just insert you String variable into the database as though the column is of type varchar.
// Open the html file
FileInputStream fis = new FileInputStream("somefile. html");
BufferedReader reader = new BufferedReader( new InputStreamReader(fis));
// Read each line of the file and append to a StringBuffer
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while ( (line = reader.readLine()) != null) {
stringBuffer.append(line);
}
// Convert to a string
String content = stringBuffer.toString();
// Insert the string into the database
Statement statement = connection.createStatement ();
// html is the CLOB column, but content is String
statement.executeUpdate("i nsert into mytable (id, html) values('00','" + content + "')");
statement.close();
connection.close();
I am not sure about Oracle, but it works perfectly in DB2.
// Open the html file
FileInputStream fis = new FileInputStream("somefile.
BufferedReader reader = new BufferedReader( new InputStreamReader(fis));
// Read each line of the file and append to a StringBuffer
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while ( (line = reader.readLine()) != null) {
stringBuffer.append(line);
}
// Convert to a string
String content = stringBuffer.toString();
// Insert the string into the database
Statement statement = connection.createStatement
// html is the CLOB column, but content is String
statement.executeUpdate("i
statement.close();
connection.close();
I am not sure about Oracle, but it works perfectly in DB2.
ASKER
ORA-01704 string literal too long
Cause: The string literal is longer than 4000 characters.
Action: Use a string literal of at most 4000 characters. Longer values may only be entered using bind variables.
I still got the error like above when I enter the String that longer than 4000 characters even used the clob.
What should I do with long String?How can I insert it into the table?
I have really been confused.
ThX
Cause: The string literal is longer than 4000 characters.
Action: Use a string literal of at most 4000 characters. Longer values may only be entered using bind variables.
I still got the error like above when I enter the String that longer than 4000 characters even used the clob.
What should I do with long String?How can I insert it into the table?
I have really been confused.
ThX
hi there
use following
String clientString = "balabalabalabaaba";
char[] chararray = clientString.toCharArray() ;
String sql="insert into table name values(?)where id=?";
try
{
CharArrayReader charReader = new CharArrayReader(chararray) ;
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setCharacterStream(1 ,charReade r);//pls refer the documentation
if(pstmt.executeUpdate()!= 1)
{
System.out.println("Error in the Code...");
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
cheers
RJ
use following
String clientString = "balabalabalabaaba";
char[] chararray = clientString.toCharArray()
String sql="insert into table name values(?)where id=?";
try
{
CharArrayReader charReader = new CharArrayReader(chararray)
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setCharacterStream(1
if(pstmt.executeUpdate()!=
{
System.out.println("Error in the Code...");
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
cheers
RJ
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have solved the problem!
Thank all of U ,Black especially!
Vivian Qi
Thank all of U ,Black especially!
Vivian Qi
ASKER
I found the "for update" in the select statement string is very important.But nobody told before U .Thank U very much!
Vivian Qi
Vivian Qi
then use the prepared statement to access these datat types
e.g
String sql="insert into table name values(?)where id=?";
try
{
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setBlob(1,blobobject
//or u can set the clob or u can directly read data using stream and set it to BLOB field in Databasein that case use the setBinaryStream method of the prepared statement
}
catch(Exception ee)
{
ee.printStackTrace();
}
cheers
RJ