001: /*
002: Copyright (C) 2002-2007 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package testsuite.simple.jdbc4;
026:
027: import java.io.Reader;
028: import java.io.StringReader;
029: import java.sql.Clob;
030: import java.sql.Connection;
031: import java.sql.NClob;
032: import java.sql.PreparedStatement;
033: import java.sql.ResultSet;
034: import java.sql.SQLException;
035: import java.sql.Statement;
036: import java.util.Properties;
037:
038: import testsuite.BaseTestCase;
039:
040: public class StatementsTest extends BaseTestCase {
041:
042: public StatementsTest(String name) {
043: super (name);
044:
045: }
046:
047: /**
048: * Tests for ResultSet.getNCharacterStream()
049: *
050: * @throws Exception
051: */
052: public void testGetNCharacterSteram() throws Exception {
053: createTable("testGetNCharacterStream",
054: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
055: this .stmt
056: .executeUpdate("INSERT INTO testGetNCharacterStream (c1, c2) VALUES (_utf8 'aaa', _utf8 'bbb')");
057: this .rs = this .stmt
058: .executeQuery("SELECT c1, c2 FROM testGetNCharacterStream");
059: this .rs.next();
060: char[] c1 = new char[3];
061: this .rs.getNCharacterStream(1).read(c1);
062: assertEquals("aaa", new String(c1));
063: char[] c2 = new char[3];
064: this .rs.getNCharacterStream("c2").read(c2);
065: assertEquals("bbb", new String(c2));
066: this .rs.close();
067: }
068:
069: /**
070: * Tests for ResultSet.getNClob()
071: *
072: * @throws Exception
073: */
074: public void testGetNClob() throws Exception {
075: createTable("testGetNClob",
076: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
077: this .stmt
078: .executeUpdate("INSERT INTO testGetNClob (c1, c2) VALUES (_utf8 'aaa', _utf8 'bbb')");
079: this .rs = this .stmt
080: .executeQuery("SELECT c1, c2 FROM testGetNClob");
081: this .rs.next();
082: char[] c1 = new char[3];
083: this .rs.getNClob(1).getCharacterStream().read(c1);
084: assertEquals("aaa", new String(c1));
085: char[] c2 = new char[3];
086: this .rs.getNClob("c2").getCharacterStream().read(c2);
087: assertEquals("bbb", new String(c2));
088: this .rs.close();
089:
090: // for isBinaryEncoded = true, using PreparedStatement
091: createTable("testGetNClob",
092: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
093: this .stmt
094: .executeUpdate("INSERT INTO testGetNClob (c1, c2) VALUES (_utf8 'aaa', _utf8 'bbb')");
095: this .pstmt = this .conn
096: .prepareStatement("SELECT c1, c2 FROM testGetNClob");
097: this .rs = this .pstmt.executeQuery();
098: this .rs.next();
099: c1 = new char[3];
100: this .rs.getNClob(1).getCharacterStream().read(c1);
101: assertEquals("aaa", new String(c1));
102: c2 = new char[3];
103: this .rs.getNClob("c2").getCharacterStream().read(c2);
104: assertEquals("bbb", new String(c2));
105: this .rs.close();
106: }
107:
108: /**
109: * Tests for ResultSet.getNString()
110: *
111: * @throws Exception
112: */
113: public void testGetNString() throws Exception {
114: createTable("testGetNString",
115: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
116: this .stmt
117: .executeUpdate("INSERT INTO testGetNString (c1, c2) VALUES (_utf8 'aaa', _utf8 'bbb')");
118: this .rs = this .stmt
119: .executeQuery("SELECT c1, c2 FROM testGetNString");
120: this .rs.next();
121: assertEquals("aaa", this .rs.getNString(1));
122: assertEquals("bbb", this .rs.getNString("c2"));
123: this .rs.close();
124: }
125:
126: /**
127: * Tests for PreparedStatement.setNCharacterSteam()
128: *
129: * @throws Exception
130: */
131: public void testSetNCharacterStream() throws Exception {
132: // suppose sql_mode don't include "NO_BACKSLASH_ESCAPES"
133:
134: createTable("testSetNCharacterStream",
135: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), "
136: + "c3 NATIONAL CHARACTER(10))");
137: Properties props1 = new Properties();
138: props1.put("useServerPrepStmts", "false"); // use client-side prepared statement
139: props1.put("useUnicode", "true");
140: props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
141: Connection conn1 = getConnectionWithProps(props1);
142: com.mysql.jdbc.PreparedStatement pstmt1 = (com.mysql.jdbc.PreparedStatement) conn1
143: .prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2, c3) VALUES (?, ?, ?)");
144: pstmt1.setNCharacterStream(1, null, 0);
145: pstmt1.setNCharacterStream(2, new StringReader("aaa"), 3);
146: pstmt1.setNCharacterStream(3, new StringReader("\'aaa\'"), 5);
147: pstmt1.execute();
148: ResultSet rs1 = this .stmt
149: .executeQuery("SELECT c1, c2, c3 FROM testSetNCharacterStream");
150: rs1.next();
151: assertEquals(null, rs1.getString(1));
152: assertEquals("aaa", rs1.getString(2));
153: assertEquals("\'aaa\'", rs1.getString(3));
154: rs1.close();
155: pstmt1.close();
156: conn1.close();
157:
158: createTable("testSetNCharacterStream",
159: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), "
160: + "c3 NATIONAL CHARACTER(10))");
161: Properties props2 = new Properties();
162: props2.put("useServerPrepStmts", "false"); // use client-side prepared statement
163: props2.put("useUnicode", "true");
164: props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
165: Connection conn2 = getConnectionWithProps(props2);
166: com.mysql.jdbc.PreparedStatement pstmt2 = (com.mysql.jdbc.PreparedStatement) conn2
167: .prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2, c3) VALUES (?, ?, ?)");
168: pstmt2.setNCharacterStream(1, null, 0);
169: pstmt2.setNCharacterStream(2, new StringReader("aaa"), 3);
170: pstmt2.setNCharacterStream(3, new StringReader("\'aaa\'"), 5);
171: pstmt2.execute();
172: ResultSet rs2 = this .stmt
173: .executeQuery("SELECT c1, c2, c3 FROM testSetNCharacterStream");
174: rs2.next();
175: assertEquals(null, rs2.getString(1));
176: assertEquals("aaa", rs2.getString(2));
177: assertEquals("\'aaa\'", rs2.getString(3));
178: rs2.close();
179: pstmt2.close();
180: conn2.close();
181: }
182:
183: /**
184: * Tests for ServerPreparedStatement.setNCharacterSteam()
185: *
186: * @throws Exception
187: */
188: public void testSetNCharacterStreamServer() throws Exception {
189: createTable("testSetNCharacterStreamServer",
190: "(c1 NATIONAL CHARACTER(10))");
191: Properties props1 = new Properties();
192: props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
193: props1.put("useUnicode", "true");
194: props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
195: Connection conn1 = getConnectionWithProps(props1);
196: PreparedStatement pstmt1 = conn1
197: .prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
198: try {
199: pstmt1.setNCharacterStream(1, new StringReader("aaa"), 3);
200: fail();
201: } catch (SQLException e) {
202: // ok
203: assertEquals(
204: "Can not call setNCharacterStream() when connection character set isn't UTF-8",
205: e.getMessage());
206: }
207: pstmt1.close();
208: conn1.close();
209:
210: createTable("testSetNCharacterStreamServer",
211: "(c1 LONGTEXT charset utf8)");
212: Properties props2 = new Properties();
213: props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
214: props2.put("useUnicode", "true");
215: props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
216: Connection conn2 = getConnectionWithProps(props2);
217: PreparedStatement pstmt2 = conn2
218: .prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
219: pstmt2.setNCharacterStream(1, new StringReader(new String(
220: new char[81921])), 81921); // 10 Full Long Data Packet's chars + 1 char
221: pstmt2.execute();
222: ResultSet rs2 = this .stmt
223: .executeQuery("SELECT c1 FROM testSetNCharacterStreamServer");
224: rs2.next();
225: assertEquals(new String(new char[81921]), rs2.getString(1));
226: rs2.close();
227: pstmt2.close();
228: conn2.close();
229: }
230:
231: /**
232: * Tests for PreparedStatement.setNClob()
233: *
234: * @throws Exception
235: */
236: public void testSetNClob() throws Exception {
237: // suppose sql_mode don't include "NO_BACKSLASH_ESCAPES"
238:
239: createTable("testSetNClob",
240: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), "
241: + "c3 NATIONAL CHARACTER(10))");
242: Properties props1 = new Properties();
243: props1.put("useServerPrepStmts", "false"); // use client-side prepared statement
244: props1.put("useUnicode", "true");
245: props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
246: Connection conn1 = getConnectionWithProps(props1);
247: PreparedStatement pstmt1 = conn1
248: .prepareStatement("INSERT INTO testSetNClob (c1, c2, c3) VALUES (?, ?, ?)");
249: pstmt1.setNClob(1, (NClob) null);
250: NClob nclob2 = conn1.createNClob();
251: nclob2.setString(1, "aaa");
252: pstmt1.setNClob(2, nclob2); // for setNClob(int, NClob)
253: Reader reader3 = new StringReader("\'aaa\'");
254: pstmt1.setNClob(3, reader3, 5); // for setNClob(int, Reader, long)
255: pstmt1.execute();
256: ResultSet rs1 = this .stmt
257: .executeQuery("SELECT c1, c2, c3 FROM testSetNClob");
258: rs1.next();
259: assertEquals(null, rs1.getString(1));
260: assertEquals("aaa", rs1.getString(2));
261: assertEquals("\'aaa\'", rs1.getString(3));
262: rs1.close();
263: pstmt1.close();
264: conn1.close();
265:
266: createTable("testSetNClob",
267: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), "
268: + "c3 NATIONAL CHARACTER(10))");
269: Properties props2 = new Properties();
270: props2.put("useServerPrepStmts", "false"); // use client-side prepared statement
271: props2.put("useUnicode", "true");
272: props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
273: Connection conn2 = getConnectionWithProps(props2);
274: PreparedStatement pstmt2 = conn2
275: .prepareStatement("INSERT INTO testSetNClob (c1, c2, c3) VALUES (?, ?, ?)");
276: pstmt2.setNClob(1, (NClob) null);
277: nclob2 = conn2.createNClob();
278: nclob2.setString(1, "aaa");
279: pstmt2.setNClob(2, nclob2); // for setNClob(int, NClob)
280: reader3 = new StringReader("\'aaa\'");
281: pstmt2.setNClob(3, reader3, 5); // for setNClob(int, Reader, long)
282: pstmt2.execute();
283: ResultSet rs2 = this .stmt
284: .executeQuery("SELECT c1, c2, c3 FROM testSetNClob");
285: rs2.next();
286: assertEquals(null, rs2.getString(1));
287: assertEquals("aaa", rs2.getString(2));
288: assertEquals("\'aaa\'", rs2.getString(3));
289: rs2.close();
290: pstmt2.close();
291: conn2.close();
292: }
293:
294: /**
295: * Tests for ServerPreparedStatement.setNClob()
296: *
297: * @throws Exception
298: */
299: public void testSetNClobServer() throws Exception {
300: createTable("testSetNClobServer",
301: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
302: Properties props1 = new Properties();
303: props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
304: props1.put("useUnicode", "true");
305: props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
306: Connection conn1 = getConnectionWithProps(props1);
307: PreparedStatement pstmt1 = conn1
308: .prepareStatement("INSERT INTO testSetNClobServer (c1, c2) VALUES (?, ?)");
309: NClob nclob1 = conn1.createNClob();
310: nclob1.setString(1, "aaa");
311: Reader reader2 = new StringReader("aaa");
312: try {
313: pstmt1.setNClob(1, nclob1);
314: fail();
315: } catch (SQLException e) {
316: // ok
317: assertEquals(
318: "Can not call setNClob() when connection character set isn't UTF-8",
319: e.getMessage());
320: }
321: try {
322: pstmt1.setNClob(2, reader2, 3);
323: fail();
324: } catch (SQLException e) {
325: // ok
326: assertEquals(
327: "Can not call setNClob() when connection character set isn't UTF-8",
328: e.getMessage());
329: }
330: pstmt1.close();
331: conn1.close();
332:
333: createTable("testSetNClobServer",
334: "(c1 NATIONAL CHARACTER(10), c2 LONGTEXT charset utf8)");
335: Properties props2 = new Properties();
336: props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
337: props2.put("useUnicode", "true");
338: props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
339: Connection conn2 = getConnectionWithProps(props2);
340: PreparedStatement pstmt2 = conn2
341: .prepareStatement("INSERT INTO testSetNClobServer (c1, c2) VALUES (?, ?)");
342: nclob1 = conn2.createNClob();
343: nclob1.setString(1, "aaa");
344: pstmt2.setNClob(1, nclob1);
345: pstmt2.setNClob(2,
346: new StringReader(new String(new char[81921])), 81921); // 10 Full Long Data Packet's chars + 1 char
347: pstmt2.execute();
348: ResultSet rs2 = this .stmt
349: .executeQuery("SELECT c1, c2 FROM testSetNClobServer");
350: rs2.next();
351: assertEquals("aaa", rs2.getString(1));
352: assertEquals(new String(new char[81921]), rs2.getString(2));
353: rs2.close();
354: pstmt2.close();
355: conn2.close();
356: }
357:
358: /**
359: * Tests for PreparedStatement.setNString()
360: *
361: * @throws Exception
362: */
363: public void testSetNString() throws Exception {
364: // suppose sql_mode don't include "NO_BACKSLASH_ESCAPES"
365:
366: createTable(
367: "testSetNString",
368: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), "
369: + "c3 NATIONAL CHARACTER(10)) DEFAULT CHARACTER SET cp932");
370: Properties props1 = new Properties();
371: props1.put("useServerPrepStmts", "false"); // use client-side prepared statement
372: props1.put("useUnicode", "true");
373: props1.put("characterEncoding", "MS932"); // ensure charset isn't utf8 here
374: Connection conn1 = getConnectionWithProps(props1);
375: PreparedStatement pstmt1 = conn1
376: .prepareStatement("INSERT INTO testSetNString (c1, c2, c3) VALUES (?, ?, ?)");
377: pstmt1.setNString(1, null);
378: pstmt1.setNString(2, "aaa");
379: pstmt1.setNString(3, "\'aaa\'");
380: pstmt1.execute();
381: ResultSet rs1 = this .stmt
382: .executeQuery("SELECT c1, c2, c3 FROM testSetNString");
383: rs1.next();
384: assertEquals(null, rs1.getString(1));
385: assertEquals("aaa", rs1.getString(2));
386: assertEquals("\'aaa\'", rs1.getString(3));
387: rs1.close();
388: pstmt1.close();
389: conn1.close();
390:
391: createTable(
392: "testSetNString",
393: "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), "
394: + "c3 NATIONAL CHARACTER(10)) DEFAULT CHARACTER SET cp932");
395: Properties props2 = new Properties();
396: props2.put("useServerPrepStmts", "false"); // use client-side prepared statement
397: props2.put("useUnicode", "true");
398: props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
399: Connection conn2 = getConnectionWithProps(props2);
400: PreparedStatement pstmt2 = conn2
401: .prepareStatement("INSERT INTO testSetNString (c1, c2, c3) VALUES (?, ?, ?)");
402: pstmt2.setNString(1, null);
403: pstmt2.setNString(2, "aaa");
404: pstmt2.setNString(3, "\'aaa\'");
405: pstmt2.execute();
406: ResultSet rs2 = this .stmt
407: .executeQuery("SELECT c1, c2, c3 FROM testSetNString");
408: rs2.next();
409: assertEquals(null, rs2.getString(1));
410: assertEquals("aaa", rs2.getString(2));
411: assertEquals("\'aaa\'", rs2.getString(3));
412: rs2.close();
413: pstmt2.close();
414: conn2.close();
415: }
416:
417: /**
418: * Tests for ServerPreparedStatement.setNString()
419: *
420: * @throws Exception
421: */
422: public void testSetNStringServer() throws Exception {
423: createTable("testSetNStringServer",
424: "(c1 NATIONAL CHARACTER(10))");
425: Properties props1 = new Properties();
426: props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
427: props1.put("useUnicode", "true");
428: props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
429: Connection conn1 = getConnectionWithProps(props1);
430: PreparedStatement pstmt1 = conn1
431: .prepareStatement("INSERT INTO testSetNStringServer (c1) VALUES (?)");
432: try {
433: pstmt1.setNString(1, "aaa");
434: fail();
435: } catch (SQLException e) {
436: // ok
437: assertEquals(
438: "Can not call setNString() when connection character set isn't UTF-8",
439: e.getMessage());
440: }
441: pstmt1.close();
442: conn1.close();
443:
444: createTable("testSetNStringServer",
445: "(c1 NATIONAL CHARACTER(10))");
446: Properties props2 = new Properties();
447: props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
448: props2.put("useUnicode", "true");
449: props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
450: Connection conn2 = getConnectionWithProps(props2);
451: PreparedStatement pstmt2 = conn2
452: .prepareStatement("INSERT INTO testSetNStringServer (c1) VALUES (?)");
453: pstmt2.setNString(1, "\'aaa\'");
454: pstmt2.execute();
455: ResultSet rs2 = this .stmt
456: .executeQuery("SELECT c1 FROM testSetNStringServer");
457: rs2.next();
458: assertEquals("\'aaa\'", rs2.getString(1));
459: rs2.close();
460: pstmt2.close();
461: conn2.close();
462: }
463:
464: /**
465: * Tests for ResultSet.updateNCharacterStream()
466: *
467: * @throws Exception
468: */
469: public void testUpdateNCharacterStream() throws Exception {
470: createTable(
471: "testUpdateNCharacterStream",
472: "(c1 CHAR(10) PRIMARY KEY, c2 NATIONAL CHARACTER(10)) default character set sjis");
473: Properties props1 = new Properties();
474: props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
475: props1.put("characterEncoding", "UTF-8"); // ensure charset isn't utf8 here
476: Connection conn1 = getConnectionWithProps(props1);
477: PreparedStatement pstmt1 = conn1
478: .prepareStatement("INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
479: pstmt1.setString(1, "1");
480: pstmt1.setNCharacterStream(2, new StringReader("aaa"), 3);
481: pstmt1.execute();
482: Statement stmt1 = conn1
483: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
484: ResultSet.CONCUR_UPDATABLE);
485: ResultSet rs1 = stmt1
486: .executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
487: rs1.next();
488: rs1.updateNCharacterStream("c2", new StringReader("bbb"), 3);
489: rs1.updateRow();
490: rs1.moveToInsertRow();
491: rs1.updateString("c1", "2");
492: rs1.updateNCharacterStream("c2", new StringReader("ccc"), 3);
493: rs1.insertRow();
494: ResultSet rs2 = stmt1
495: .executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
496: rs2.next();
497: assertEquals("1", rs2.getString("c1"));
498: assertEquals("bbb", rs2.getNString("c2"));
499: rs2.next();
500: assertEquals("2", rs2.getString("c1"));
501: assertEquals("ccc", rs2.getNString("c2"));
502: pstmt1.close();
503: stmt1.close();
504: conn1.close();
505:
506: createTable("testUpdateNCharacterStream",
507: "(c1 CHAR(10) PRIMARY KEY, c2 CHAR(10)) default character set sjis"); // sjis field
508: Properties props2 = new Properties();
509: props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
510: props2.put("characterEncoding", "SJIS"); // ensure charset isn't utf8 here
511: Connection conn2 = getConnectionWithProps(props2);
512: PreparedStatement pstmt2 = conn2
513: .prepareStatement("INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
514: pstmt2.setString(1, "1");
515: pstmt2.setString(2, "aaa");
516: pstmt2.execute();
517: Statement stmt2 = conn2
518: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
519: ResultSet.CONCUR_UPDATABLE);
520: ResultSet rs3 = stmt2
521: .executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
522: rs3.next();
523: try {
524: rs3
525: .updateNCharacterStream("c2", new StringReader(
526: "bbb"), 3); // field's charset isn't utf8
527: fail();
528: } catch (SQLException ex) {
529: assertEquals(
530: "Can not call updateNCharacterStream() when field's character set isn't UTF-8",
531: ex.getMessage());
532: }
533: rs3.close();
534: pstmt2.close();
535: stmt2.close();
536: conn2.close();
537: }
538:
539: /**
540: * Tests for ResultSet.updateNClob()
541: *
542: * @throws Exception
543: */
544: public void testUpdateNClob() throws Exception {
545: createTable(
546: "testUpdateNChlob",
547: "(c1 CHAR(10) PRIMARY KEY, c2 NATIONAL CHARACTER(10)) default character set sjis");
548: Properties props1 = new Properties();
549: props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
550: props1.put("characterEncoding", "UTF-8"); // ensure charset isn't utf8 here
551: Connection conn1 = getConnectionWithProps(props1);
552: PreparedStatement pstmt1 = conn1
553: .prepareStatement("INSERT INTO testUpdateNChlob (c1, c2) VALUES (?, ?)");
554: pstmt1.setString(1, "1");
555: NClob nClob1 = conn1.createNClob();
556: nClob1.setString(1, "aaa");
557: pstmt1.setNClob(2, nClob1);
558: pstmt1.execute();
559: Statement stmt1 = conn1
560: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
561: ResultSet.CONCUR_UPDATABLE);
562: ResultSet rs1 = stmt1
563: .executeQuery("SELECT c1, c2 FROM testUpdateNChlob");
564: rs1.next();
565: NClob nClob2 = conn1.createNClob();
566: nClob2.setString(1, "bbb");
567: rs1.updateNClob("c2", nClob2);
568: rs1.updateRow();
569: rs1.moveToInsertRow();
570: rs1.updateString("c1", "2");
571: NClob nClob3 = conn1.createNClob();
572: nClob3.setString(1, "ccc");
573: rs1.updateNClob("c2", nClob3);
574: rs1.insertRow();
575: ResultSet rs2 = stmt1
576: .executeQuery("SELECT c1, c2 FROM testUpdateNChlob");
577: rs2.next();
578: assertEquals("1", rs2.getString("c1"));
579: assertEquals("bbb", rs2.getNString("c2"));
580: rs2.next();
581: assertEquals("2", rs2.getString("c1"));
582: assertEquals("ccc", rs2.getNString("c2"));
583: pstmt1.close();
584: stmt1.close();
585: conn1.close();
586:
587: createTable("testUpdateNChlob",
588: "(c1 CHAR(10) PRIMARY KEY, c2 CHAR(10)) default character set sjis"); // sjis field
589: Properties props2 = new Properties();
590: props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
591: props2.put("characterEncoding", "SJIS"); // ensure charset isn't utf8 here
592: Connection conn2 = getConnectionWithProps(props2);
593: PreparedStatement pstmt2 = conn2
594: .prepareStatement("INSERT INTO testUpdateNChlob (c1, c2) VALUES (?, ?)");
595: pstmt2.setString(1, "1");
596: pstmt2.setString(2, "aaa");
597: pstmt2.execute();
598: Statement stmt2 = conn2
599: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
600: ResultSet.CONCUR_UPDATABLE);
601: ResultSet rs3 = stmt2
602: .executeQuery("SELECT c1, c2 FROM testUpdateNChlob");
603: rs3.next();
604: NClob nClob4 = conn2.createNClob();
605: nClob4.setString(1, "bbb");
606: try {
607: rs3.updateNClob("c2", nClob4); // field's charset isn't utf8
608: fail();
609: } catch (SQLException ex) {
610: assertEquals(
611: "Can not call updateNClob() when field's character set isn't UTF-8",
612: ex.getMessage());
613: }
614: rs3.close();
615: pstmt2.close();
616: stmt2.close();
617: conn2.close();
618: }
619:
620: /**
621: * Tests for ResultSet.updateNString()
622: *
623: * @throws Exception
624: */
625: public void testUpdateNString() throws Exception {
626: createTable(
627: "testUpdateNString",
628: "(c1 CHAR(10) PRIMARY KEY, c2 NATIONAL CHARACTER(10)) default character set sjis");
629: Properties props1 = new Properties();
630: props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
631: props1.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
632: Connection conn1 = getConnectionWithProps(props1);
633: PreparedStatement pstmt1 = conn1
634: .prepareStatement("INSERT INTO testUpdateNString (c1, c2) VALUES (?, ?)");
635: pstmt1.setString(1, "1");
636: pstmt1.setNString(2, "aaa");
637: pstmt1.execute();
638: Statement stmt1 = conn1
639: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
640: ResultSet.CONCUR_UPDATABLE);
641: ResultSet rs1 = stmt1
642: .executeQuery("SELECT c1, c2 FROM testUpdateNString");
643: rs1.next();
644: rs1.updateNString("c2", "bbb");
645: rs1.updateRow();
646: rs1.moveToInsertRow();
647: rs1.updateString("c1", "2");
648: rs1.updateNString("c2", "ccc");
649: rs1.insertRow();
650: ResultSet rs2 = stmt1
651: .executeQuery("SELECT c1, c2 FROM testUpdateNString");
652: rs2.next();
653: assertEquals("1", rs2.getString("c1"));
654: assertEquals("bbb", rs2.getNString("c2"));
655: rs2.next();
656: assertEquals("2", rs2.getString("c1"));
657: assertEquals("ccc", rs2.getNString("c2"));
658: pstmt1.close();
659: stmt1.close();
660: conn1.close();
661:
662: createTable("testUpdateNString",
663: "(c1 CHAR(10) PRIMARY KEY, c2 CHAR(10)) default character set sjis"); // sjis field
664: Properties props2 = new Properties();
665: props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
666: props2.put("characterEncoding", "SJIS"); // ensure charset isn't utf8 here
667: Connection conn2 = getConnectionWithProps(props2);
668: PreparedStatement pstmt2 = conn2
669: .prepareStatement("INSERT INTO testUpdateNString (c1, c2) VALUES (?, ?)");
670: pstmt2.setString(1, "1");
671: pstmt2.setString(2, "aaa");
672: pstmt2.execute();
673: Statement stmt2 = conn2
674: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
675: ResultSet.CONCUR_UPDATABLE);
676: ResultSet rs3 = stmt2
677: .executeQuery("SELECT c1, c2 FROM testUpdateNString");
678: rs3.next();
679: try {
680: rs3.updateNString("c2", "bbb"); // field's charset isn't utf8
681: fail();
682: } catch (SQLException ex) {
683: assertEquals(
684: "Can not call updateNString() when field's character set isn't UTF-8",
685: ex.getMessage());
686: }
687: rs3.close();
688: pstmt2.close();
689: stmt2.close();
690: conn2.close();
691: }
692: }
|