Java Code Examples for java.sql.PreparedStatement#setNCharacterStream()

The following examples show how to use java.sql.PreparedStatement#setNCharacterStream() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void testSetNCharacter() throws Exception {

  createTable("testSetNCharacter", "id int not null primary key, strm text", "CHARSET utf8");
  PreparedStatement stmt =
      sharedConnection.prepareStatement("insert into testSetNCharacter (id, strm) values (?,?)");
  String toInsert = "Øabcdefgh\njklmn\"";

  stmt.setInt(1, 1);
  stmt.setNCharacterStream(2, new StringReader(toInsert));
  stmt.execute();

  stmt.setInt(1, 2);
  stmt.setNCharacterStream(2, new StringReader(toInsert), 3);
  stmt.execute();

  ResultSet rs =
      sharedConnection.createStatement().executeQuery("select * from testSetNCharacter");
  assertTrue(rs.next());
  assertTrue(rs.getObject(2) instanceof String);
  assertTrue(rs.getCharacterStream(2) instanceof Reader);
  checkCharStream(rs.getCharacterStream(2), toInsert);

  assertTrue(rs.next());
  checkCharStream(rs.getCharacterStream(2), toInsert.substring(0, 3));
}
 
Example 2
@Override
public void setNCharacterStream(final int parameterIndex,
		final Reader value, final long length) throws SQLException {
	ParameterCallback callback = new ParameterCallbackAction(
			parameterIndex, value) {

		@Override
		public void call(PreparedStatement preparedStatement)
				throws SQLException {
			preparedStatement.setNCharacterStream(parameterIndex(),
					(Reader) value, length);
			;
		}
	};
	addParameterCallback(callback);
}
 
Example 3
Source Project: r-course   File: StatementsTest.java    License: MIT License 5 votes vote down vote up
/**
 * Tests for ServerPreparedStatement.setNCharacterSteam()
 * 
 * @throws Exception
 */
public void testSetNCharacterStreamServer() throws Exception {
    createTable("testSetNCharacterStreamServer", "(c1 NATIONAL CHARACTER(10)) ENGINE=InnoDB");
    Properties props1 = new Properties();
    props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
    props1.put("useUnicode", "true");
    props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
    Connection conn1 = getConnectionWithProps(props1);
    PreparedStatement pstmt1 = conn1.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
    try {
        pstmt1.setNCharacterStream(1, new StringReader("aaa"), 3);
        fail();
    } catch (SQLException e) {
        // ok
        assertEquals("Can not call setNCharacterStream() when connection character set isn't UTF-8", e.getMessage());
    }
    pstmt1.close();
    conn1.close();

    createTable("testSetNCharacterStreamServer", "(c1 LONGTEXT charset utf8) ENGINE=InnoDB");
    Properties props2 = new Properties();
    props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
    props2.put("useUnicode", "true");
    props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
    Connection conn2 = getConnectionWithProps(props2);
    PreparedStatement pstmt2 = conn2.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
    pstmt2.setNCharacterStream(1, new StringReader(new String(new char[81921])), 81921); // 10 Full Long Data Packet's chars + 1 char
    pstmt2.execute();
    ResultSet rs2 = this.stmt.executeQuery("SELECT c1 FROM testSetNCharacterStreamServer");
    rs2.next();
    assertEquals(new String(new char[81921]), rs2.getString(1));
    rs2.close();
    pstmt2.close();
    conn2.close();
}
 
Example 4
/**
 * Tests for ServerPreparedStatement.setNCharacterSteam()
 * 
 * @throws Exception
 */
public void testSetNCharacterStreamServer() throws Exception {
    createTable("testSetNCharacterStreamServer", "(c1 NATIONAL CHARACTER(10)) ENGINE=InnoDB");
    Properties props1 = new Properties();
    props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
    props1.put("useUnicode", "true");
    props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
    Connection conn1 = getConnectionWithProps(props1);
    PreparedStatement pstmt1 = conn1.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
    try {
        pstmt1.setNCharacterStream(1, new StringReader("aaa"), 3);
        fail();
    } catch (SQLException e) {
        // ok
        assertEquals("Can not call setNCharacterStream() when connection character set isn't UTF-8", e.getMessage());
    }
    pstmt1.close();
    conn1.close();

    createTable("testSetNCharacterStreamServer", "(c1 LONGTEXT charset utf8) ENGINE=InnoDB");
    Properties props2 = new Properties();
    props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
    props2.put("useUnicode", "true");
    props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
    Connection conn2 = getConnectionWithProps(props2);
    PreparedStatement pstmt2 = conn2.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
    pstmt2.setNCharacterStream(1, new StringReader(new String(new char[81921])), 81921); // 10 Full Long Data Packet's chars + 1 char
    pstmt2.execute();
    ResultSet rs2 = this.stmt.executeQuery("SELECT c1 FROM testSetNCharacterStreamServer");
    rs2.next();
    assertEquals(new String(new char[81921]), rs2.getString(1));
    rs2.close();
    pstmt2.close();
    conn2.close();
}
 
Example 5
@Override
public void setParam(PreparedStatement stmt) throws SQLException {
	if (values.length == 1) {
		stmt.setNCharacterStream(index, (Reader) values[0]);
	} else if (values.length == 2) {
		stmt.setNCharacterStream(index, (Reader) values[0], (Long) values[1]);
	}
}
 
Example 6
/**
 * Tests for ServerPreparedStatement.setNCharacterSteam()
 * 
 * @throws Exception
 */
public void testSetNCharacterStreamServer() throws Exception {
    createTable("testSetNCharacterStreamServer", "(c1 NATIONAL CHARACTER(10)) ENGINE=InnoDB");
    Properties props1 = new Properties();
    props1.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "true"); // use server-side prepared statement
    props1.setProperty(PropertyKey.characterEncoding.getKeyName(), "latin1"); // ensure charset isn't utf8 here
    Connection conn1 = getConnectionWithProps(props1);
    PreparedStatement pstmt1 = conn1.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
    try {
        pstmt1.setNCharacterStream(1, new StringReader("aaa"), 3);
        fail();
    } catch (SQLException e) {
        // ok
        assertEquals("Can not call setNCharacterStream() when connection character set isn't UTF-8", e.getMessage());
    }
    pstmt1.close();
    conn1.close();

    createTable("testSetNCharacterStreamServer", "(c1 LONGTEXT charset utf8) ENGINE=InnoDB");
    Properties props2 = new Properties();
    props2.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "true"); // use server-side prepared statement
    props2.setProperty(PropertyKey.characterEncoding.getKeyName(), "UTF-8"); // ensure charset is utf8 here
    Connection conn2 = getConnectionWithProps(props2);
    PreparedStatement pstmt2 = conn2.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
    pstmt2.setNCharacterStream(1, new StringReader(new String(new char[81921])), 81921); // 10 Full Long Data Packet's chars + 1 char
    pstmt2.execute();
    ResultSet rs2 = this.stmt.executeQuery("SELECT c1 FROM testSetNCharacterStreamServer");
    rs2.next();
    assertEquals(new String(new char[81921]), rs2.getString(1));
    rs2.close();
    pstmt2.close();
    conn2.close();
}
 
Example 7
@Override
public void setNCharacterStream(final int parameterIndex, final Reader value)
		throws SQLException {
	ParameterCallback callback = new ParameterCallbackAction(
			parameterIndex, value) {
		@Override
		public void call(PreparedStatement preparedStatement)
				throws SQLException {
			preparedStatement.setNCharacterStream(parameterIndex(),
					(Reader) value);
		}

	};
	addParameterCallback(callback);
}
 
Example 8
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertSetNCharacterStream() throws SQLException {
    for (PreparedStatement each : statements) {
        each.setNCharacterStream(1, new StringReader(""));
    }
}
 
Example 9
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertSetNCharacterStreamWithLength() throws SQLException {
    for (PreparedStatement each : statements) {
        each.setNCharacterStream(1, new StringReader(""), 1);
    }
}
 
Example 10
Source Project: r-course   File: StatementsTest.java    License: MIT License 4 votes vote down vote up
/**
 * Tests for ResultSet.updateNCharacterStream()
 * 
 * @throws Exception
 */
public void testUpdateNCharacterStream() throws Exception {
    createTable("testUpdateNCharacterStream", "(c1 CHAR(10) PRIMARY KEY, c2 NATIONAL CHARACTER(10)) default character set sjis");
    Properties props1 = new Properties();
    props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
    props1.put("characterEncoding", "UTF-8"); // ensure charset isn't utf8 here
    Connection conn1 = getConnectionWithProps(props1);
    PreparedStatement pstmt1 = conn1.prepareStatement("INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
    pstmt1.setString(1, "1");
    pstmt1.setNCharacterStream(2, new StringReader("aaa"), 3);
    pstmt1.execute();
    Statement stmt1 = conn1.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs1 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
    rs1.next();
    rs1.updateNCharacterStream("c2", new StringReader("bbb"), 3);
    rs1.updateRow();
    rs1.moveToInsertRow();
    rs1.updateString("c1", "2");
    rs1.updateNCharacterStream("c2", new StringReader("ccc"), 3);
    rs1.insertRow();
    ResultSet rs2 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
    rs2.next();
    assertEquals("1", rs2.getString("c1"));
    assertEquals("bbb", rs2.getNString("c2"));
    rs2.next();
    assertEquals("2", rs2.getString("c1"));
    assertEquals("ccc", rs2.getNString("c2"));
    pstmt1.close();
    stmt1.close();
    conn1.close();

    createTable("testUpdateNCharacterStream", "(c1 CHAR(10) PRIMARY KEY, c2 CHAR(10)) default character set sjis"); // sjis field
    Properties props2 = new Properties();
    props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
    props2.put("characterEncoding", "SJIS"); // ensure charset isn't utf8 here
    Connection conn2 = getConnectionWithProps(props2);
    PreparedStatement pstmt2 = conn2.prepareStatement("INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
    pstmt2.setString(1, "1");
    pstmt2.setString(2, "aaa");
    pstmt2.execute();
    Statement stmt2 = conn2.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs3 = stmt2.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
    rs3.next();
    try {
        rs3.updateNCharacterStream("c2", new StringReader("bbb"), 3); // field's charset isn't utf8
        fail();
    } catch (SQLException ex) {
        assertEquals("Can not call updateNCharacterStream() when field's character set isn't UTF-8", ex.getMessage());
    }
    rs3.close();
    pstmt2.close();
    stmt2.close();
    conn2.close();
}
 
Example 11
/**
 * Tests for ResultSet.updateNCharacterStream()
 * 
 * @throws Exception
 */
public void testUpdateNCharacterStream() throws Exception {
    createTable("testUpdateNCharacterStream", "(c1 CHAR(10) PRIMARY KEY, c2 NATIONAL CHARACTER(10)) default character set sjis");
    Properties props1 = new Properties();
    props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
    props1.put("characterEncoding", "UTF-8"); // ensure charset isn't utf8 here
    Connection conn1 = getConnectionWithProps(props1);
    PreparedStatement pstmt1 = conn1.prepareStatement("INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
    pstmt1.setString(1, "1");
    pstmt1.setNCharacterStream(2, new StringReader("aaa"), 3);
    pstmt1.execute();
    Statement stmt1 = conn1.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs1 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
    rs1.next();
    rs1.updateNCharacterStream("c2", new StringReader("bbb"), 3);
    rs1.updateRow();
    rs1.moveToInsertRow();
    rs1.updateString("c1", "2");
    rs1.updateNCharacterStream("c2", new StringReader("ccc"), 3);
    rs1.insertRow();
    ResultSet rs2 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
    rs2.next();
    assertEquals("1", rs2.getString("c1"));
    assertEquals("bbb", rs2.getNString("c2"));
    rs2.next();
    assertEquals("2", rs2.getString("c1"));
    assertEquals("ccc", rs2.getNString("c2"));
    pstmt1.close();
    stmt1.close();
    conn1.close();

    createTable("testUpdateNCharacterStream", "(c1 CHAR(10) PRIMARY KEY, c2 CHAR(10)) default character set sjis"); // sjis field
    Properties props2 = new Properties();
    props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
    props2.put("characterEncoding", "SJIS"); // ensure charset isn't utf8 here
    Connection conn2 = getConnectionWithProps(props2);
    PreparedStatement pstmt2 = conn2.prepareStatement("INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
    pstmt2.setString(1, "1");
    pstmt2.setString(2, "aaa");
    pstmt2.execute();
    Statement stmt2 = conn2.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs3 = stmt2.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
    rs3.next();
    try {
        rs3.updateNCharacterStream("c2", new StringReader("bbb"), 3); // field's charset isn't utf8
        fail();
    } catch (SQLException ex) {
        assertEquals("Can not call updateNCharacterStream() when field's character set isn't UTF-8", ex.getMessage());
    }
    rs3.close();
    pstmt2.close();
    stmt2.close();
    conn2.close();
}
 
Example 12
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertSetNCharacterStream() throws SQLException {
    for (PreparedStatement each : statements) {
        each.setNCharacterStream(1, new StringReader(""));
    }
}
 
Example 13
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertSetNCharacterStreamWithLength() throws SQLException {
    for (PreparedStatement each : statements) {
        each.setNCharacterStream(1, new StringReader(""), 1);
    }
}
 
Example 14
/**
 * Tests for ResultSet.updateNCharacterStream()
 * 
 * @throws Exception
 */
public void testUpdateNCharacterStream() throws Exception {
    createTable("testUpdateNCharacterStream", "(c1 CHAR(10) PRIMARY KEY, c2 NATIONAL CHARACTER(10)) default character set sjis");
    Properties props1 = new Properties();
    props1.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "true"); // use server-side prepared statement
    props1.setProperty(PropertyKey.characterEncoding.getKeyName(), "UTF-8"); // ensure charset isn't utf8 here
    Connection conn1 = getConnectionWithProps(props1);
    PreparedStatement pstmt1 = conn1.prepareStatement("INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
    pstmt1.setString(1, "1");
    pstmt1.setNCharacterStream(2, new StringReader("aaa"), 3);
    pstmt1.execute();
    Statement stmt1 = conn1.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs1 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
    rs1.next();
    rs1.updateNCharacterStream("c2", new StringReader("bbb"), 3);
    rs1.updateRow();
    rs1.moveToInsertRow();
    rs1.updateString("c1", "2");
    rs1.updateNCharacterStream("c2", new StringReader("ccc"), 3);
    rs1.insertRow();
    ResultSet rs2 = stmt1.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
    rs2.next();
    assertEquals("1", rs2.getString("c1"));
    assertEquals("bbb", rs2.getNString("c2"));
    rs2.next();
    assertEquals("2", rs2.getString("c1"));
    assertEquals("ccc", rs2.getNString("c2"));
    pstmt1.close();
    stmt1.close();
    conn1.close();

    createTable("testUpdateNCharacterStream", "(c1 CHAR(10) PRIMARY KEY, c2 CHAR(10)) default character set sjis"); // sjis field
    Properties props2 = new Properties();
    props2.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "true"); // use server-side prepared statement
    props2.setProperty(PropertyKey.characterEncoding.getKeyName(), "SJIS"); // ensure charset isn't utf8 here
    Connection conn2 = getConnectionWithProps(props2);
    PreparedStatement pstmt2 = conn2.prepareStatement("INSERT INTO testUpdateNCharacterStream (c1, c2) VALUES (?, ?)");
    pstmt2.setString(1, "1");
    pstmt2.setString(2, "aaa");
    pstmt2.execute();
    Statement stmt2 = conn2.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs3 = stmt2.executeQuery("SELECT c1, c2 FROM testUpdateNCharacterStream");
    rs3.next();
    try {
        rs3.updateNCharacterStream("c2", new StringReader("bbb"), 3); // field's charset isn't utf8
        fail();
    } catch (SQLException ex) {
        assertEquals("Can not call updateNCharacterStream() when field's character set isn't UTF-8", ex.getMessage());
    }
    rs3.close();
    pstmt2.close();
    stmt2.close();
    conn2.close();
}