使用PreparedStatement的setString(i, s)时出现:
可以参考帖子:http://community.csdn.net/Expert/topic/3936/3936672.xml?temp=.2879145
java.sql.SQLException: 数据大小超出此类型的最大值: 3000
后面那个值大小不定, 感觉与s大小有关
表结构
create table test(
name char(32),
addr varchar(3000) //varchar2也一样
)
解决办法: 采用setCharacterStream
import java.sql.*;
import java.io.*;
import java.util.*;
/**
* oracle测试
* @author kingfish
* @version 1.0
*/
public class TestOra {
public static void testORACLE() {
String url = "jdbc:oracle:thin:@localhost:1521:oradb";
String username = "system";
String password = "manager";
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
}
catch (Exception e) {
e.printStackTrace();
return;
}
char[] carray = new char[1000];
Arrays.fill(carray, '我');
String s = new String(carray);
try {
PreparedStatement pst = conn.prepareStatement(
"insert into test(name,addr) values(?,?)");
pst.setString(1, "kingfish");
pst.setCharacterStream(2,
new InputStreamReader(new ByteArrayInputStream(s.
getBytes())), s.length());
//pst.setString(2,s); //用此句则异常
pst.execute();
Statement st = conn.createStatement();
ResultSet r = st.executeQuery("SELECT * from test");
while (r.next()) {
s = r.getString(2);
System.out.println("len=" + s.length());
System.out.println("value=" + s);
}
r.close();
st.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 测试
* @param args String[]
*/
public static void main(String[] args) {
testORACLE();
}
}
说明: 引起此问题的原因估计和OraclePreparedStatement中的setString的实现有关.
kingfish
2005.4.14