1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.ResultSet ; 28 import java.sql.Statement ; 29 import java.sql.SQLException ; 30 import java.sql.Types ; 31 32 import org.apache.derby.tools.ij; 33 import org.apache.derby.tools.JDBCDisplayUtil; 34 import java.io.*; 35 import java.sql.PreparedStatement ; 36 37 40 41 public class CharUTF8 { 42 43 public static PreparedStatement psSet; 44 public static PreparedStatement psGet; 45 46 public static void main(String [] args) { 47 48 System.out.println("Test CharUTF8 starting"); 49 50 StringBuffer buff = new StringBuffer (); 51 52 try { 53 ij.getPropertyArg(args); 56 Connection conn = ij.startJBMS(); 57 58 Statement st2 = conn.createStatement(); 59 st2.execute("CREATE TABLE TEST(id int not null primary key, body varchar(60))"); 60 psSet = conn.prepareStatement("insert into test values(?,?)"); 61 psGet = conn.prepareStatement("select body from test where id=?"); 62 63 int off = 0; 64 for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) { 65 66 buff.append((char) i); 67 68 if ((buff.length() == 60) || (i == Character.MAX_VALUE)) { 69 70 String text = buff.toString(); 71 System.out.println("Testing with last char value " + i + " length=" + text.length()); 72 73 setBody(i, text); 75 76 String res = getBody(i); 78 if (!res.equals(text)) { 79 System.out.println("FAIL -- string fetched is incorrect, length is " 80 + buff.length() + ", expecting string: " + text 81 + ", instead got the following: " + res); 82 break; 83 } 84 85 buff.setLength(0); 86 } 87 } 88 89 setBody(-1, ""); 91 if (!getBody(-1).equals("")) { 92 System.out.println("FAIL: empty string returned as " + getBody(-1)); 93 } 94 95 96 conn.close(); 97 98 } catch (SQLException e) { 99 dumpSQLExceptions(e); 100 } catch (Throwable e) { 101 System.out.println("FAIL -- unexpected exception:" + e.toString()); 102 } 103 104 System.out.println("Test CharUTF8 finished"); 105 106 } 107 108 private static void setBody(int key, String body) { 109 110 try { 111 psSet.setInt(1, key); 112 psSet.setString(2, body); 113 psSet.executeUpdate(); 114 115 } catch (SQLException ex) { 116 ex.printStackTrace(); 117 118 System.out.println("FAIL -- unexpected exception"); 119 System.exit(-1); 120 } 121 } 122 123 private static String getBody(int key) { 124 125 String result="NO RESULT"; 126 127 try { 128 psGet.setInt(1, key); 129 ResultSet rs = psGet.executeQuery(); 130 131 if (rs.next()) 132 result = rs.getString(1); 133 134 } catch (SQLException ex) { 135 ex.printStackTrace(); 136 } 137 138 return result; 139 } 140 141 static private void dumpSQLExceptions (SQLException se) { 142 System.out.println("FAIL -- unexpected exception: " + se.toString()); 143 while (se != null) { 144 System.out.print("SQLSTATE("+se.getSQLState()+"):"); 145 se = se.getNextException(); 146 } 147 } 148 149 153 public static String getMaxDefinedCharAsString() { 154 155 return "\uFA2D"; 156 } 157 158 } 159 | Popular Tags |