PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/EQEmuJSM/mysql-connector-java-5.1.13/src/testsuite/simple/jdbc4/StatementsTest.java

http://cubbers-eqemu-utils.googlecode.com/
Java | 626 lines | 501 code | 30 blank | 95 comment | 0 complexity | e00f8213f83318627090f54270ff8f16 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. /*
  2. Copyright 2002-2007 MySQL AB, 2008 Sun Microsystems
  3. All rights reserved. Use is subject to license terms.
  4. The MySQL Connector/J is licensed under the terms of the GPL,
  5. like most MySQL Connectors. There are special exceptions to the
  6. terms and conditions of the GPL as it is applied to this software,
  7. see the FLOSS License Exception available on mysql.com.
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; version 2 of the
  11. License.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. 02110-1301 USA
  20. */
  21. package testsuite.simple.jdbc4;
  22. import java.io.Reader;
  23. import java.io.StringReader;
  24. import java.sql.Clob;
  25. import java.sql.Connection;
  26. import java.sql.NClob;
  27. import java.sql.PreparedStatement;
  28. import java.sql.ResultSet;
  29. import java.sql.SQLException;
  30. import java.sql.Statement;
  31. import java.util.Properties;
  32. import testsuite.BaseTestCase;
  33. public class StatementsTest extends BaseTestCase {
  34. public StatementsTest(String name) {
  35. super(name);
  36. }
  37. /**
  38. * Tests for ResultSet.getNCharacterStream()
  39. *
  40. * @throws Exception
  41. */
  42. public void testGetNCharacterSteram() throws Exception {
  43. createTable("testGetNCharacterStream", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
  44. this.stmt.executeUpdate("INSERT INTO testGetNCharacterStream (c1, c2) VALUES (_utf8 'aaa', _utf8 'bbb')");
  45. this.rs = this.stmt.executeQuery("SELECT c1, c2 FROM testGetNCharacterStream");
  46. this.rs.next();
  47. char[] c1 = new char[3];
  48. this.rs.getNCharacterStream(1).read(c1);
  49. assertEquals("aaa", new String(c1));
  50. char[] c2 = new char[3];
  51. this.rs.getNCharacterStream("c2").read(c2);
  52. assertEquals("bbb", new String(c2));
  53. this.rs.close();
  54. }
  55. /**
  56. * Tests for ResultSet.getNClob()
  57. *
  58. * @throws Exception
  59. */
  60. public void testGetNClob() throws Exception {
  61. createTable("testGetNClob", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
  62. this.stmt.executeUpdate("INSERT INTO testGetNClob (c1, c2) VALUES (_utf8 'aaa', _utf8 'bbb')");
  63. this.rs = this.stmt.executeQuery("SELECT c1, c2 FROM testGetNClob");
  64. this.rs.next();
  65. char[] c1 = new char[3];
  66. this.rs.getNClob(1).getCharacterStream().read(c1);
  67. assertEquals("aaa", new String(c1));
  68. char[] c2 = new char[3];
  69. this.rs.getNClob("c2").getCharacterStream().read(c2);
  70. assertEquals("bbb", new String(c2));
  71. this.rs.close();
  72. // for isBinaryEncoded = true, using PreparedStatement
  73. createTable("testGetNClob", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
  74. this.stmt.executeUpdate("INSERT INTO testGetNClob (c1, c2) VALUES (_utf8 'aaa', _utf8 'bbb')");
  75. this.pstmt = this.conn.prepareStatement("SELECT c1, c2 FROM testGetNClob");
  76. this.rs = this.pstmt.executeQuery();
  77. this.rs.next();
  78. c1 = new char[3];
  79. this.rs.getNClob(1).getCharacterStream().read(c1);
  80. assertEquals("aaa", new String(c1));
  81. c2 = new char[3];
  82. this.rs.getNClob("c2").getCharacterStream().read(c2);
  83. assertEquals("bbb", new String(c2));
  84. this.rs.close();
  85. }
  86. /**
  87. * Tests for ResultSet.getNString()
  88. *
  89. * @throws Exception
  90. */
  91. public void testGetNString() throws Exception {
  92. createTable("testGetNString", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
  93. this.stmt.executeUpdate("INSERT INTO testGetNString (c1, c2) VALUES (_utf8 'aaa', _utf8 'bbb')");
  94. this.rs = this.stmt.executeQuery("SELECT c1, c2 FROM testGetNString");
  95. this.rs.next();
  96. assertEquals("aaa", this.rs.getNString(1));
  97. assertEquals("bbb", this.rs.getNString("c2"));
  98. this.rs.close();
  99. }
  100. /**
  101. * Tests for PreparedStatement.setNCharacterSteam()
  102. *
  103. * @throws Exception
  104. */
  105. public void testSetNCharacterStream() throws Exception {
  106. // suppose sql_mode don't include "NO_BACKSLASH_ESCAPES"
  107. createTable("testSetNCharacterStream", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " +
  108. "c3 NATIONAL CHARACTER(10))");
  109. Properties props1 = new Properties();
  110. props1.put("useServerPrepStmts", "false"); // use client-side prepared statement
  111. props1.put("useUnicode", "true");
  112. props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
  113. Connection conn1 = getConnectionWithProps(props1);
  114. com.mysql.jdbc.PreparedStatement pstmt1 = (com.mysql.jdbc.PreparedStatement)
  115. conn1.prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2, c3) VALUES (?, ?, ?)");
  116. pstmt1.setNCharacterStream(1, null, 0);
  117. pstmt1.setNCharacterStream(2, new StringReader("aaa"), 3);
  118. pstmt1.setNCharacterStream(3, new StringReader("\'aaa\'"), 5);
  119. pstmt1.execute();
  120. ResultSet rs1 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNCharacterStream");
  121. rs1.next();
  122. assertEquals(null, rs1.getString(1));
  123. assertEquals("aaa", rs1.getString(2));
  124. assertEquals("\'aaa\'", rs1.getString(3));
  125. rs1.close();
  126. pstmt1.close();
  127. conn1.close();
  128. createTable("testSetNCharacterStream", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " +
  129. "c3 NATIONAL CHARACTER(10))");
  130. Properties props2 = new Properties();
  131. props2.put("useServerPrepStmts", "false"); // use client-side prepared statement
  132. props2.put("useUnicode", "true");
  133. props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
  134. Connection conn2 = getConnectionWithProps(props2);
  135. com.mysql.jdbc.PreparedStatement pstmt2 = (com.mysql.jdbc.PreparedStatement)
  136. conn2.prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2, c3) VALUES (?, ?, ?)");
  137. pstmt2.setNCharacterStream(1, null, 0);
  138. pstmt2.setNCharacterStream(2, new StringReader("aaa"), 3);
  139. pstmt2.setNCharacterStream(3, new StringReader("\'aaa\'"), 5);
  140. pstmt2.execute();
  141. ResultSet rs2 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNCharacterStream");
  142. rs2.next();
  143. assertEquals(null, rs2.getString(1));
  144. assertEquals("aaa", rs2.getString(2));
  145. assertEquals("\'aaa\'", rs2.getString(3));
  146. rs2.close();
  147. pstmt2.close();
  148. conn2.close();
  149. }
  150. /**
  151. * Tests for ServerPreparedStatement.setNCharacterSteam()
  152. *
  153. * @throws Exception
  154. */
  155. public void testSetNCharacterStreamServer() throws Exception {
  156. createTable("testSetNCharacterStreamServer", "(c1 NATIONAL CHARACTER(10))");
  157. Properties props1 = new Properties();
  158. props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
  159. props1.put("useUnicode", "true");
  160. props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
  161. Connection conn1 = getConnectionWithProps(props1);
  162. PreparedStatement pstmt1 = conn1.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
  163. try {
  164. pstmt1.setNCharacterStream(1, new StringReader("aaa"), 3);
  165. fail();
  166. } catch (SQLException e) {
  167. // ok
  168. assertEquals("Can not call setNCharacterStream() when connection character set isn't UTF-8",
  169. e.getMessage());
  170. }
  171. pstmt1.close();
  172. conn1.close();
  173. createTable("testSetNCharacterStreamServer", "(c1 LONGTEXT charset utf8)");
  174. Properties props2 = new Properties();
  175. props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
  176. props2.put("useUnicode", "true");
  177. props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
  178. Connection conn2 = getConnectionWithProps(props2);
  179. PreparedStatement pstmt2 =
  180. conn2.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
  181. pstmt2.setNCharacterStream(1, new StringReader(
  182. new String(new char[81921])), 81921); // 10 Full Long Data Packet's chars + 1 char
  183. pstmt2.execute();
  184. ResultSet rs2 = this.stmt.executeQuery("SELECT c1 FROM testSetNCharacterStreamServer");
  185. rs2.next();
  186. assertEquals(new String(new char[81921]), rs2.getString(1));
  187. rs2.close();
  188. pstmt2.close();
  189. conn2.close();
  190. }
  191. /**
  192. * Tests for PreparedStatement.setNClob()
  193. *
  194. * @throws Exception
  195. */
  196. public void testSetNClob() throws Exception {
  197. // suppose sql_mode don't include "NO_BACKSLASH_ESCAPES"
  198. createTable("testSetNClob", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " +
  199. "c3 NATIONAL CHARACTER(10))");
  200. Properties props1 = new Properties();
  201. props1.put("useServerPrepStmts", "false"); // use client-side prepared statement
  202. props1.put("useUnicode", "true");
  203. props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
  204. Connection conn1 = getConnectionWithProps(props1);
  205. PreparedStatement pstmt1 =
  206. conn1.prepareStatement("INSERT INTO testSetNClob (c1, c2, c3) VALUES (?, ?, ?)");
  207. pstmt1.setNClob(1, (NClob)null);
  208. NClob nclob2 = conn1.createNClob();
  209. nclob2.setString(1, "aaa");
  210. pstmt1.setNClob(2, nclob2); // for setNClob(int, NClob)
  211. Reader reader3 = new StringReader("\'aaa\'");
  212. pstmt1.setNClob(3, reader3, 5); // for setNClob(int, Reader, long)
  213. pstmt1.execute();
  214. ResultSet rs1 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNClob");
  215. rs1.next();
  216. assertEquals(null, rs1.getString(1));
  217. assertEquals("aaa", rs1.getString(2));
  218. assertEquals("\'aaa\'", rs1.getString(3));
  219. rs1.close();
  220. pstmt1.close();
  221. conn1.close();
  222. createTable("testSetNClob", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " +
  223. "c3 NATIONAL CHARACTER(10))");
  224. Properties props2 = new Properties();
  225. props2.put("useServerPrepStmts", "false"); // use client-side prepared statement
  226. props2.put("useUnicode", "true");
  227. props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
  228. Connection conn2 = getConnectionWithProps(props2);
  229. PreparedStatement pstmt2 =
  230. conn2.prepareStatement("INSERT INTO testSetNClob (c1, c2, c3) VALUES (?, ?, ?)");
  231. pstmt2.setNClob(1, (NClob)null);
  232. nclob2 = conn2.createNClob();
  233. nclob2.setString(1, "aaa");
  234. pstmt2.setNClob(2, nclob2); // for setNClob(int, NClob)
  235. reader3 = new StringReader("\'aaa\'");
  236. pstmt2.setNClob(3, reader3, 5); // for setNClob(int, Reader, long)
  237. pstmt2.execute();
  238. ResultSet rs2 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNClob");
  239. rs2.next();
  240. assertEquals(null, rs2.getString(1));
  241. assertEquals("aaa", rs2.getString(2));
  242. assertEquals("\'aaa\'", rs2.getString(3));
  243. rs2.close();
  244. pstmt2.close();
  245. conn2.close();
  246. }
  247. /**
  248. * Tests for ServerPreparedStatement.setNClob()
  249. *
  250. * @throws Exception
  251. */
  252. public void testSetNClobServer() throws Exception {
  253. createTable("testSetNClobServer", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
  254. Properties props1 = new Properties();
  255. props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
  256. props1.put("useUnicode", "true");
  257. props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
  258. Connection conn1 = getConnectionWithProps(props1);
  259. PreparedStatement pstmt1 =
  260. conn1.prepareStatement("INSERT INTO testSetNClobServer (c1, c2) VALUES (?, ?)");
  261. NClob nclob1 = conn1.createNClob();
  262. nclob1.setString(1, "aaa");
  263. Reader reader2 = new StringReader("aaa");
  264. try {
  265. pstmt1.setNClob(1, nclob1);
  266. fail();
  267. } catch (SQLException e) {
  268. // ok
  269. assertEquals("Can not call setNClob() when connection character set isn't UTF-8",
  270. e.getMessage());
  271. }
  272. try {
  273. pstmt1.setNClob(2, reader2, 3);
  274. fail();
  275. } catch (SQLException e) {
  276. // ok
  277. assertEquals("Can not call setNClob() when connection character set isn't UTF-8",
  278. e.getMessage());
  279. }
  280. pstmt1.close();
  281. conn1.close();
  282. createTable("testSetNClobServer", "(c1 NATIONAL CHARACTER(10), c2 LONGTEXT charset utf8)");
  283. Properties props2 = new Properties();
  284. props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
  285. props2.put("useUnicode", "true");
  286. props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
  287. Connection conn2 = getConnectionWithProps(props2);
  288. PreparedStatement pstmt2 =
  289. conn2.prepareStatement("INSERT INTO testSetNClobServer (c1, c2) VALUES (?, ?)");
  290. nclob1 = conn2.createNClob();
  291. nclob1.setString(1, "aaa");
  292. pstmt2.setNClob(1, nclob1);
  293. pstmt2.setNClob(2, new StringReader(
  294. new String(new char[81921])), 81921); // 10 Full Long Data Packet's chars + 1 char
  295. pstmt2.execute();
  296. ResultSet rs2 = this.stmt.executeQuery("SELECT c1, c2 FROM testSetNClobServer");
  297. rs2.next();
  298. assertEquals("aaa", rs2.getString(1));
  299. assertEquals(new String(new char[81921]), rs2.getString(2));
  300. rs2.close();
  301. pstmt2.close();
  302. conn2.close();
  303. }
  304. /**
  305. * Tests for PreparedStatement.setNString()
  306. *
  307. * @throws Exception
  308. */
  309. public void testSetNString() throws Exception {
  310. // suppose sql_mode don't include "NO_BACKSLASH_ESCAPES"
  311. createTable("testSetNString", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " +
  312. "c3 NATIONAL CHARACTER(10)) DEFAULT CHARACTER SET cp932");
  313. Properties props1 = new Properties();
  314. props1.put("useServerPrepStmts", "false"); // use client-side prepared statement
  315. props1.put("useUnicode", "true");
  316. props1.put("characterEncoding", "MS932"); // ensure charset isn't utf8 here
  317. Connection conn1 = getConnectionWithProps(props1);
  318. PreparedStatement pstmt1 =
  319. conn1.prepareStatement("INSERT INTO testSetNString (c1, c2, c3) VALUES (?, ?, ?)");
  320. pstmt1.setNString(1, null);
  321. pstmt1.setNString(2, "aaa");
  322. pstmt1.setNString(3, "\'aaa\'");
  323. pstmt1.execute();
  324. ResultSet rs1 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNString");
  325. rs1.next();
  326. assertEquals(null, rs1.getString(1));
  327. assertEquals("aaa", rs1.getString(2));
  328. assertEquals("\'aaa\'", rs1.getString(3));
  329. rs1.close();
  330. pstmt1.close();
  331. conn1.close();
  332. createTable("testSetNString", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " +
  333. "c3 NATIONAL CHARACTER(10)) DEFAULT CHARACTER SET cp932");
  334. Properties props2 = new Properties();
  335. props2.put("useServerPrepStmts", "false"); // use client-side prepared statement
  336. props2.put("useUnicode", "true");
  337. props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
  338. Connection conn2 = getConnectionWithProps(props2);
  339. PreparedStatement pstmt2 =
  340. conn2.prepareStatement("INSERT INTO testSetNString (c1, c2, c3) VALUES (?, ?, ?)");
  341. pstmt2.setNString(1, null);
  342. pstmt2.setNString(2, "aaa");
  343. pstmt2.setNString(3, "\'aaa\'");
  344. pstmt2.execute();
  345. ResultSet rs2 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNString");
  346. rs2.next();
  347. assertEquals(null, rs2.getString(1));
  348. assertEquals("aaa", rs2.getString(2));
  349. assertEquals("\'aaa\'", rs2.getString(3));
  350. rs2.close();
  351. pstmt2.close();
  352. conn2.close();
  353. }
  354. /**
  355. * Tests for ServerPreparedStatement.setNString()
  356. *
  357. * @throws Exception
  358. */
  359. public void testSetNStringServer() throws Exception {
  360. createTable("testSetNStringServer", "(c1 NATIONAL CHARACTER(10))");
  361. Properties props1 = new Properties();
  362. props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
  363. props1.put("useUnicode", "true");
  364. props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
  365. Connection conn1 = getConnectionWithProps(props1);
  366. PreparedStatement pstmt1 =
  367. conn1.prepareStatement("INSERT INTO testSetNStringServer (c1) VALUES (?)");
  368. try {
  369. pstmt1.setNString(1, "aaa");
  370. fail();
  371. } catch (SQLException e) {
  372. // ok
  373. assertEquals("Can not call setNString() when connection character set isn't UTF-8",
  374. e.getMessage());
  375. }
  376. pstmt1.close();
  377. conn1.close();
  378. createTable("testSetNStringServer", "(c1 NATIONAL CHARACTER(10))");
  379. Properties props2 = new Properties();
  380. props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
  381. props2.put("useUnicode", "true");
  382. props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
  383. Connection conn2 = getConnectionWithProps(props2);
  384. PreparedStatement pstmt2 =
  385. conn2.prepareStatement("INSERT INTO testSetNStringServer (c1) VALUES (?)");
  386. pstmt2.setNString(1, "\'aaa\'");
  387. pstmt2.execute();
  388. ResultSet rs2 = this.stmt.executeQuery("SELECT c1 FROM testSetNStringServer");
  389. rs2.next();
  390. assertEquals("\'aaa\'", rs2.getString(1));
  391. rs2.close();
  392. pstmt2.close();
  393. conn2.close();
  394. }
  395. /**
  396. * Tests for ResultSet.updateNCharacterStream()
  397. *
  398. * @throws Exception
  399. */
  400. public void testUpdateNCharacterStream() throws Exception {
  401. createTable("testUpdateNCharacterStream",
  402. "(c1 CHAR(10) PRIMARY KEY, c2 NATIONAL CHARACTER(10)) default character set sjis");
  403. Properties props1 = new Properties();
  404. props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
  405. props1.put("characterEncoding", "UTF-8"); // ensure charset isn't utf8 here
  406. Connection conn1 = getConnectionWithProps(props1);
  407. PreparedStatement pstmt1 = conn1.prepareStatement(
  408. "INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
  409. pstmt1.setString(1, "1");
  410. pstmt1.setNCharacterStream(2, new StringReader("aaa"), 3);
  411. pstmt1.execute();
  412. Statement stmt1 = conn1.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
  413. ResultSet rs1 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
  414. rs1.next();
  415. rs1.updateNCharacterStream("c2", new StringReader("bbb"), 3);
  416. rs1.updateRow();
  417. rs1.moveToInsertRow();
  418. rs1.updateString("c1", "2");
  419. rs1.updateNCharacterStream("c2", new StringReader("ccc"), 3);
  420. rs1.insertRow();
  421. ResultSet rs2 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
  422. rs2.next();
  423. assertEquals("1", rs2.getString("c1"));
  424. assertEquals("bbb", rs2.getNString("c2"));
  425. rs2.next();
  426. assertEquals("2", rs2.getString("c1"));
  427. assertEquals("ccc", rs2.getNString("c2"));
  428. pstmt1.close();
  429. stmt1.close();
  430. conn1.close();
  431. createTable("testUpdateNCharacterStream",
  432. "(c1 CHAR(10) PRIMARY KEY, c2 CHAR(10)) default character set sjis"); // sjis field
  433. Properties props2 = new Properties();
  434. props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
  435. props2.put("characterEncoding", "SJIS"); // ensure charset isn't utf8 here
  436. Connection conn2 = getConnectionWithProps(props2);
  437. PreparedStatement pstmt2 = conn2.prepareStatement(
  438. "INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
  439. pstmt2.setString(1, "1");
  440. pstmt2.setString(2, "aaa");
  441. pstmt2.execute();
  442. Statement stmt2 = conn2.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
  443. ResultSet rs3 = stmt2.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
  444. rs3.next();
  445. try {
  446. rs3.updateNCharacterStream("c2", new StringReader("bbb"), 3); // field's charset isn't utf8
  447. fail();
  448. } catch (SQLException ex) {
  449. assertEquals("Can not call updateNCharacterStream() when field's character set isn't UTF-8",
  450. ex.getMessage());
  451. }
  452. rs3.close();
  453. pstmt2.close();
  454. stmt2.close();
  455. conn2.close();
  456. }
  457. /**
  458. * Tests for ResultSet.updateNClob()
  459. *
  460. * @throws Exception
  461. */
  462. public void testUpdateNClob() throws Exception {
  463. createTable("testUpdateNChlob",
  464. "(c1 CHAR(10) PRIMARY KEY, c2 NATIONAL CHARACTER(10)) default character set sjis");
  465. Properties props1 = new Properties();
  466. props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
  467. props1.put("characterEncoding", "UTF-8"); // ensure charset isn't utf8 here
  468. Connection conn1 = getConnectionWithProps(props1);
  469. PreparedStatement pstmt1 = conn1.prepareStatement(
  470. "INSERT INTO testUpdateNChlob (c1, c2) VALUES (?, ?)");
  471. pstmt1.setString(1, "1");
  472. NClob nClob1 = conn1.createNClob();
  473. nClob1.setString(1, "aaa");
  474. pstmt1.setNClob(2, nClob1);
  475. pstmt1.execute();
  476. Statement stmt1 = conn1.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
  477. ResultSet rs1 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNChlob");
  478. rs1.next();
  479. NClob nClob2 = conn1.createNClob();
  480. nClob2.setString(1, "bbb");
  481. rs1.updateNClob("c2", nClob2);
  482. rs1.updateRow();
  483. rs1.moveToInsertRow();
  484. rs1.updateString("c1", "2");
  485. NClob nClob3 = conn1.createNClob();
  486. nClob3.setString(1, "ccc");
  487. rs1.updateNClob("c2", nClob3);
  488. rs1.insertRow();
  489. ResultSet rs2 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNChlob");
  490. rs2.next();
  491. assertEquals("1", rs2.getString("c1"));
  492. assertEquals("bbb", rs2.getNString("c2"));
  493. rs2.next();
  494. assertEquals("2", rs2.getString("c1"));
  495. assertEquals("ccc", rs2.getNString("c2"));
  496. pstmt1.close();
  497. stmt1.close();
  498. conn1.close();
  499. createTable("testUpdateNChlob",
  500. "(c1 CHAR(10) PRIMARY KEY, c2 CHAR(10)) default character set sjis"); // sjis field
  501. Properties props2 = new Properties();
  502. props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
  503. props2.put("characterEncoding", "SJIS"); // ensure charset isn't utf8 here
  504. Connection conn2 = getConnectionWithProps(props2);
  505. PreparedStatement pstmt2 = conn2.prepareStatement(
  506. "INSERT INTO testUpdateNChlob (c1, c2) VALUES (?, ?)");
  507. pstmt2.setString(1, "1");
  508. pstmt2.setString(2, "aaa");
  509. pstmt2.execute();
  510. Statement stmt2 = conn2.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
  511. ResultSet rs3 = stmt2.executeQuery("SELECT c1, c2 FROM testUpdateNChlob");
  512. rs3.next();
  513. NClob nClob4 = conn2.createNClob();
  514. nClob4.setString(1, "bbb");
  515. try {
  516. rs3.updateNClob("c2", nClob4); // field's charset isn't utf8
  517. fail();
  518. } catch (SQLException ex) {
  519. assertEquals("Can not call updateNClob() when field's character set isn't UTF-8",
  520. ex.getMessage());
  521. }
  522. rs3.close();
  523. pstmt2.close();
  524. stmt2.close();
  525. conn2.close();
  526. }
  527. /**
  528. * Tests for ResultSet.updateNString()
  529. *
  530. * @throws Exception
  531. */
  532. public void testUpdateNString() throws Exception {
  533. createTable("testUpdateNString",
  534. "(c1 CHAR(10) PRIMARY KEY, c2 NATIONAL CHARACTER(10)) default character set sjis");
  535. Properties props1 = new Properties();
  536. props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
  537. props1.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
  538. Connection conn1 = getConnectionWithProps(props1);
  539. PreparedStatement pstmt1 = conn1.prepareStatement(
  540. "INSERT INTO testUpdateNString (c1, c2) VALUES (?, ?)");
  541. pstmt1.setString(1, "1");
  542. pstmt1.setNString(2, "aaa");
  543. pstmt1.execute();
  544. Statement stmt1 = conn1.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
  545. ResultSet rs1 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNString");
  546. rs1.next();
  547. rs1.updateNString("c2", "bbb");
  548. rs1.updateRow();
  549. rs1.moveToInsertRow();
  550. rs1.updateString("c1", "2");
  551. rs1.updateNString("c2", "ccc");
  552. rs1.insertRow();
  553. ResultSet rs2 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNString");
  554. rs2.next();
  555. assertEquals("1", rs2.getString("c1"));
  556. assertEquals("bbb", rs2.getNString("c2"));
  557. rs2.next();
  558. assertEquals("2", rs2.getString("c1"));
  559. assertEquals("ccc", rs2.getNString("c2"));
  560. pstmt1.close();
  561. stmt1.close();
  562. conn1.close();
  563. createTable("testUpdateNString",
  564. "(c1 CHAR(10) PRIMARY KEY, c2 CHAR(10)) default character set sjis"); // sjis field
  565. Properties props2 = new Properties();
  566. props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
  567. props2.put("characterEncoding", "SJIS"); // ensure charset isn't utf8 here
  568. Connection conn2 = getConnectionWithProps(props2);
  569. PreparedStatement pstmt2 = conn2.prepareStatement(
  570. "INSERT INTO testUpdateNString (c1, c2) VALUES (?, ?)");
  571. pstmt2.setString(1, "1");
  572. pstmt2.setString(2, "aaa");
  573. pstmt2.execute();
  574. Statement stmt2 = conn2.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
  575. ResultSet rs3 = stmt2.executeQuery("SELECT c1, c2 FROM testUpdateNString");
  576. rs3.next();
  577. try {
  578. rs3.updateNString("c2", "bbb"); // field's charset isn't utf8
  579. fail();
  580. } catch (SQLException ex) {
  581. assertEquals("Can not call updateNString() when field's character set isn't UTF-8",
  582. ex.getMessage());
  583. }
  584. rs3.close();
  585. pstmt2.close();
  586. stmt2.close();
  587. conn2.close();
  588. }
  589. }