Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PreparedStatement, VARCHAR"(4000) and umlauts

484912Mar 12 2007 — edited Jun 29 2007
Can someone explain this to me, please?

I have a varchar2 column of size 4000. Just big enough so I don't have to LOB it.I can insert a String of size 4000 no problem. But, if my String contains special characters, such as umlauts, I get an error that the String size is too big for the column. Surprises me, as my column type is char rather than byte. However, if I don't use prepared Statements the same String will insert OK.

Further, if I reduce the column size to 1, I can insert 'ä' using prepared statements.

Simple test code is below. Can someone explain to me what is going on? Thanks.



create table test_table (
name varchar2(4000 char);
)

public static void main(String[] args) {
final String url = "jdbc:oracle:thin:@host:port:DB";
final String username = "me";
final String password = "password";

String veryBigString = "";
for (int i = 0; i < 4000; i++) veryBigString = veryBigString + "a";
System.out.println("vbs length: " + veryBigString.length());

Connection connection = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(url, username, password);
Statement s = connection.createStatement();

int inserted = s.executeUpdate("insert into test_table values ('" + veryBigString + "')");
System.out.println("1: inserted " + inserted);

inserted = s.executeUpdate("insert into test_table values ('" + ("ä" + veryBigString.substring(1)) + "')");
System.out.println("2: inserted " + inserted);

PreparedStatement ps = connection.prepareStatement("insert into test_table values (?)");

ps.setString(1, veryBigString);
inserted = ps.executeUpdate();
System.out.println("3: inserted " + inserted);

ps.setString(1, "ä" + veryBigString.substring(1));
inserted = ps.executeUpdate();
System.out.println("4: inserted " + inserted);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) try {
connection.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 27 2007
Added on Mar 12 2007
12 comments
6,931 views