use of com.mysql.cj.jdbc.ClientPreparedStatement in project aws-mysql-jdbc by awslabs.
the class StatementsTest method testParameterBindings.
@Test
public void testParameterBindings() throws Exception {
Properties props = new Properties();
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
props.setProperty(PropertyKey.characterEncoding.getKeyName(), "UTF-8");
props.setProperty(PropertyKey.treatUtilDateAsTimestamp.getKeyName(), "false");
props.setProperty(PropertyKey.autoDeserialize.getKeyName(), "true");
for (boolean useSPS : new boolean[] { false, true }) {
props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), Boolean.toString(useSPS));
// Need to check character set stuff, so need a new connection
Connection utfConn = getConnectionWithProps(props);
java.util.Date now = new java.util.Date();
Object[] valuesToTest = new Object[] { new Byte(Byte.MIN_VALUE), new Short(Short.MIN_VALUE), new Integer(Integer.MIN_VALUE), // to test isNull
new Long(Long.MIN_VALUE), // to test isNull
new Double(Double.MIN_VALUE), // to test isNull
"\u4E2D\u6587", // to test isNull
new BigDecimal(Math.PI), // to test isNull
null, // to test serialization
now };
StringBuilder statementText = new StringBuilder("SELECT ?");
for (int i = 1; i < valuesToTest.length; i++) {
statementText.append(",?");
}
this.pstmt = utfConn.prepareStatement(statementText.toString());
for (int i = 0; i < valuesToTest.length; i++) {
this.pstmt.setObject(i + 1, valuesToTest[i]);
}
ParameterBindings bindings = ((ClientPreparedStatement) this.pstmt).getParameterBindings();
for (int i = 0; i < valuesToTest.length; i++) {
Object boundObject = bindings.getObject(i + 1);
if (boundObject == null || valuesToTest[i] == null) {
continue;
}
Class<?> boundObjectClass = boundObject.getClass();
Class<?> testObjectClass = valuesToTest[i].getClass();
if (boundObject instanceof Number) {
assertEquals(valuesToTest[i].toString(), boundObject.toString(), "For binding #" + (i + 1) + " of class " + boundObjectClass + " compared to " + testObjectClass);
} else if (boundObject instanceof Date) {
} else {
assertEquals(valuesToTest[i], boundObject, "For binding #" + (i + 1) + " of class " + boundObjectClass + " compared to " + testObjectClass);
}
}
}
}
use of com.mysql.cj.jdbc.ClientPreparedStatement in project aws-mysql-jdbc by awslabs.
the class StatementsTest method testSetNCharacterStream.
/**
* Tests for PreparedStatement.setNCharacterSteam()
*
* @throws Exception
*/
@Test
public void testSetNCharacterStream() throws Exception {
// suppose sql_mode don't include "NO_BACKSLASH_ESCAPES"
createTable("testSetNCharacterStream", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " + "c3 NATIONAL CHARACTER(10)) ENGINE=InnoDB");
Properties props1 = new Properties();
props1.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
props1.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
// use client-side prepared statement
props1.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "false");
// ensure charset isn't utf8 here
props1.setProperty(PropertyKey.characterEncoding.getKeyName(), "latin1");
Connection conn1 = getConnectionWithProps(props1);
ClientPreparedStatement pstmt1 = (ClientPreparedStatement) conn1.prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2, c3) VALUES (?, ?, ?)");
pstmt1.setNCharacterStream(1, null, 0);
pstmt1.setNCharacterStream(2, new StringReader("aaa"), 3);
pstmt1.setNCharacterStream(3, new StringReader("\'aaa\'"), 5);
pstmt1.execute();
ResultSet rs1 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNCharacterStream");
rs1.next();
assertEquals(null, rs1.getString(1));
assertEquals("aaa", rs1.getString(2));
assertEquals("\'aaa\'", rs1.getString(3));
rs1.close();
pstmt1.close();
conn1.close();
createTable("testSetNCharacterStream", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " + "c3 NATIONAL CHARACTER(10)) ENGINE=InnoDB");
Properties props2 = new Properties();
props2.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
props2.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
// use client-side prepared statement
props2.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "false");
// ensure charset is utf8 here
props2.setProperty(PropertyKey.characterEncoding.getKeyName(), "UTF-8");
Connection conn2 = getConnectionWithProps(props2);
ClientPreparedStatement pstmt2 = (ClientPreparedStatement) conn2.prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2, c3) VALUES (?, ?, ?)");
pstmt2.setNCharacterStream(1, null, 0);
pstmt2.setNCharacterStream(2, new StringReader("aaa"), 3);
pstmt2.setNCharacterStream(3, new StringReader("\'aaa\'"), 5);
pstmt2.execute();
ResultSet rs2 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNCharacterStream");
rs2.next();
assertEquals(null, rs2.getString(1));
assertEquals("aaa", rs2.getString(2));
assertEquals("\'aaa\'", rs2.getString(3));
rs2.close();
pstmt2.close();
conn2.close();
}
Aggregations